Skip to content

Commit

Permalink
Add test and fix JS time
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Jan 20, 2025
1 parent 94c9d07 commit 53a033a
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 117 deletions.
294 changes: 191 additions & 103 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
documentation = "https://github.com/pubky/pubky-app-specs"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
serde = { version = "1.0.215", features = ["derive"] }
Expand All @@ -19,21 +19,24 @@ base32 = "0.5.1"
blake3 = "1.5.4"
mime = "0.3"
utoipa = { git = "https://github.com/juhaku/utoipa", rev = "d522f744259dc4fde5f45d187983fb68c8167029", optional = true }
wasm-bindgen = "0.2.99"
wasm-bindgen = "0.2.84"
serde-wasm-bindgen = "0.6.5"
js-sys = "0.3.76"

[dev-dependencies]
pubky = "0.3.0"
pubky-common = "0.1.0"
anyhow = "1.0.93"
wasm-bindgen-test = "0.3.34"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.41.1", features = ["full"] }
anyhow = "1.0.93"

[features]
openapi = ["utoipa"]

[profile.release]
opt-level = "z" # Requests maximum optimization for binary size (“z” stands for “size”), rather than speed.
opt-level = "s" # Requests maximum optimization for binary size (“z” stands for “size”), rather than speed.
lto = true # Enables link-time optimization, allowing the compiler to do more aggressive inlining/dead-code elimination across crates.
codegen-units = 1 # Forces compilation into a single code-generation unit, further helping inlining and dead-code elimination at link time.
panic = "abort" # Ensures panics do not generate extra stack-unwinding code, reducing binary bloat. Panics will simply abort execution.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ _Version 0.3.0_
>
> When we reach the first stable, long-term support version of the schemas, paths will adopt the format: `pubky.app/v1/` to indicate compatibility and stability.
### To build the JS package
### JS package

Build with

```bash
wasm-pack build --target bundler
```

Test with

```bash
wasm-pack test --headless --firefox
```

## Table of Contents

1. [Introduction](#introduction)
Expand Down
19 changes: 19 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash


echo "🦀 Testing WebAssembly package..."
wasm-pack test --headless --firefox

echo "🦀 Building WebAssembly package..."
wasm-pack build --target web --out-dir dist

echo "📋 Copying package.json and Readme files to dist..."
cp pkg/* dist/

# echo "📦 Installing npm dependencies..."
# cd dist && npm install

# echo "🧪 Running JavaScript tests..."
# npm test

echo "✨ Building and testing completed!"
27 changes: 19 additions & 8 deletions examples/create_user.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
/// cargo run --example create_user
use anyhow::Result;
use pubky::PubkyClient;
use pubky_app_specs::{
traits::{HasPath, Validatable},
PubkyAppUser, PROTOCOL,
};
use pubky_common::crypto::{Keypair, PublicKey};
use serde_json::to_vec;
#[cfg(target_arch = "wasm32")]
fn main() {
// Do nothing when compiled for wasm
}

#[cfg(not(target_arch = "wasm32"))]
use {
anyhow::Result,
pubky::PubkyClient,
pubky_app_specs::{
traits::{HasPath, Validatable},
PubkyAppUser, PROTOCOL,
},
pubky_common::crypto::{Keypair, PublicKey},
serde_json::to_vec,
};
// Replace this with your actual homeserver public key

#[cfg(not(target_arch = "wasm32"))]
const HOMESERVER: &str = "ufibwbmed6jeq9k4p583go95wofakh9fwpp4k734trq79pd9u1uy";

#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() -> Result<()> {
// Print an introduction for the developer
Expand Down
1 change: 1 addition & 0 deletions pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO
25 changes: 25 additions & 0 deletions pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "pubky-app-specs",
"type": "module",
"description": "Pubky.app Data Model Specifications",
"version": "0.3.0",
"license": "MIT",
"collaborators": [
"SHAcollision"
],
"repository": {
"type": "git",
"url": "https://github.com/pubky/pubky-app-specs"
},
"files": [
"pubky_app_specs_bg.wasm",
"pubky_app_specs.js",
"pubky_app_specs.d.ts"
],
"main": "pubky_app_specs.js",
"homepage": "https://pubky.app",
"types": "pubky_app_specs.d.ts",
"sideEffects": [
"./snippets/*"
]
}
16 changes: 15 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
#[cfg(target_arch = "wasm32")]
use js_sys::Date;

#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};

pub static VERSION: &str = "0.3.0";
pub static APP_PATH: &str = "/pub/pubky.app/";
pub static PROTOCOL: &str = "pubky://";

/// Returns the current timestamp in microseconds since the UNIX epoch.
#[cfg(target_arch = "wasm32")]
pub fn timestamp() -> i64 {
// Use JS Date.now() which returns ms since Unix epoch
let ms = Date::now() as i64;
// Convert to microseconds if you like
ms * 1_000
}

#[cfg(not(target_arch = "wasm32"))]
pub fn timestamp() -> i64 {
use std::time::{SystemTime, UNIX_EPOCH};
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.unwrap()
.as_micros() as i64
}
35 changes: 35 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// tests/wasm_follow_test.rs

#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

use js_sys::Object;
use pubky_app_specs::create_pubky_app_follow;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn test_create_pubky_app_follow() {
let pubky_id = "fake_pubky_id";

// Call the wasm-exported function
let result = create_pubky_app_follow(pubky_id.into()).unwrap();

// The returned `JsValue` is the JSON object { json, id, path }
// We can convert it to a JS object so we can extract fields
let result_obj = Object::try_from(&result).expect("expected a JS object");

// For example, check the `path` field
let path_val =
js_sys::Reflect::get(&result_obj, &JsValue::from_str("path")).expect("no path field");
let path_str = path_val.as_string().expect("path must be a string");

assert_eq!(
path_str,
format!("/pub/pubky.app/follows/{}", pubky_id),
"create_pubky_app_follow did not produce expected path"
);
}

0 comments on commit 53a033a

Please sign in to comment.