55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 1 | # drives macltok in a headless firefox worker against a throwaway continuwuity |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 2 | # run from the web devshell, `nix develop .#web -c nu harness/web/run.nu` |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 3 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 4 | const crate = path self . |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 5 | const root = path self ../.. |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 6 | const username = "harness" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 7 | const password = "harness-password" |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 8 | const room_name = "harness" |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 9 | const seeded = "seeded by run.nu" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 10 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 11 | # the shared build wrapper sets CARGO_TARGET_DIR, the nix sandbox does not |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 12 | def target [] { $env.CARGO_TARGET_DIR? | default ($root | path join target) } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 13 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 14 | def scratch [] { target | path join harness } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 15 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 16 | def start-homeserver [] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 17 | let dir = scratch |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 18 | rm -rf $dir |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 19 | mkdir $dir |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 20 | let port = 6167 |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 21 | let config = $dir | path join homeserver.toml |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 22 | let log = $dir | path join homeserver.log |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 23 | { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 24 | global: { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 25 | server_name: "localhost" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 26 | database_path: ($dir | path join db) |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 27 | address: ["127.0.0.1"] |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 28 | port: $port |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 29 | allow_federation: false |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 30 | new_user_displayname_suffix: "" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 31 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 32 | } | to toml | save $config |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 33 | # the first account otherwise needs a one-time token that is only ever printed to the log |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 34 | let create = $"users create-user ($username) ($password)" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 35 | let pid = ^bash -c 'conduwuit -c "$1" --execute "$2" > "$3" 2>&1 & echo $!' _ $config $create $log | str trim |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 36 | let url = $'http://127.0.0.1:($port)' |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 37 | for _ in 1..100 { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 38 | if (try { login $url | ignore; true } catch { false }) { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 39 | return { pid: $pid, url: $url } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 40 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 41 | sleep 100ms |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 42 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 43 | try { stop $pid } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 44 | print (open --raw $log | lines | last 20 | str join "\n") |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 45 | error make { msg: "homeserver did not come up" } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 46 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 47 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 48 | def login [url: string] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 49 | let body = { type: "m.login.password", identifier: { type: "m.id.user", user: $username }, password: $password } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 50 | http post --content-type application/json $'($url)/_matrix/client/v3/login' $body |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 51 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 52 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 53 | def seed [url: string] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 54 | let auth = [Authorization $'Bearer ((login $url).access_token)'] |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 55 | let room = http post --headers $auth --content-type application/json $'($url)/_matrix/client/v3/createRoom' { name: $room_name } |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 56 | let room_id = $room.room_id | url encode --all |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 57 | let message = { msgtype: "m.text", body: $seeded } |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 58 | http put --headers $auth --content-type application/json $'($url)/_matrix/client/v3/rooms/($room_id)/send/m.room.message/seed' $message | ignore |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 59 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 60 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 61 | def stop [pid: string] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 62 | ^kill -9 $pid |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 63 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 64 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 65 | def main [] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 66 | let server = start-homeserver |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 67 | let result = try { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 68 | let versions = http get $'($server.url)/_matrix/client/versions' |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 69 | if not ($versions.unstable_features | get -o 'org.matrix.simplified_msc3575' | default false) { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 70 | error make { msg: "homeserver does not advertise simplified sliding sync" } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 71 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 72 | seed $server.url |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 73 | cd $crate |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 74 | with-env { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 75 | MACLTOK_HOMESERVER: $server.url |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 76 | MACLTOK_USERNAME: $username |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 77 | MACLTOK_PASSWORD: $password |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 78 | MACLTOK_ROOM_NAME: $room_name |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 79 | MACLTOK_SEEDED: $seeded |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 80 | GECKODRIVER: (which geckodriver | get 0.path) |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 81 | WASM_BINDGEN_TEST_TIMEOUT: "180" |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 82 | } { |
7aa42f589ee2 | atagen | 2026-09-26 12:30 UTC | 83 | ^cargo test --target wasm32-unknown-unknown --test worker --test shared_worker |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 84 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 85 | null |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 86 | } catch {|error| $error } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 87 | stop $server.pid |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 88 | if $result != null { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 89 | print $'homeserver log at (scratch)/homeserver.log' |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 90 | error make { msg: $result.msg } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 91 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 92 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 93 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 94 | # release build of the same slice, sized raw, after wasm-opt, and compressed |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 95 | def "main size" [] { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 96 | let target = target |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 97 | let out = scratch | path join size |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 98 | rm -rf $out |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 99 | mkdir $out |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 100 | cd $crate |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 101 | ^cargo build --release --lib --target wasm32-unknown-unknown |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 102 | ^wasm-bindgen --target web --out-dir $out ($target | path join wasm32-unknown-unknown release macltok_web.wasm) |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 103 | let raw = $out | path join macltok_web_bg.wasm |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 104 | let opt = $out | path join macltok_web_opt.wasm |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 105 | ^wasm-opt -Oz --enable-bulk-memory --enable-nontrapping-float-to-int --enable-sign-ext --enable-mutable-globals --enable-reference-types --enable-multivalue -o $opt $raw |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 106 | ^gzip -9 -k $opt |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 107 | ^brotli -q 11 -k $opt |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 108 | [$raw $opt $'($opt).gz' $'($opt).br'] | each {|file| { file: ($file | path basename), size: (ls $file | get 0.size) } } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 109 | } |