|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | += affinescript-deno-test |
| 3 | +:toc: |
| 4 | +:icons: font |
| 5 | + |
| 6 | +A Deno test-framework integration for AffineScript. Compiles `.affine` test |
| 7 | +files to WebAssembly via the standard `affinescript` compiler, loads them |
| 8 | +through the existing `@hyperpolymath/affine-js` bridge, and registers each |
| 9 | +as a `Deno.test()` case so `deno test` reports pass/fail in the usual way. |
| 10 | + |
| 11 | +Part of the https://github.com/hyperpolymath/developer-ecosystem[developer-ecosystem] |
| 12 | +monorepo. Sibling tools in the AffineScript ecosystem live alongside this |
| 13 | +component: `affinescript/` (the compiler — actually mirrored from upstream), |
| 14 | +`affinescriptiser/`, `affinescript-vite/`, `rattlescript/`. |
| 15 | + |
| 16 | +== Status |
| 17 | + |
| 18 | +*MVP v0.1.0.* Minimal working harness, one test per file. See |
| 19 | +<<mvp-constraints>> below for the specific limits and planned follow-ups. |
| 20 | + |
| 21 | +== Quick start |
| 22 | + |
| 23 | +[source,bash] |
| 24 | +---- |
| 25 | +# Compile + smoke-test the bundled example: |
| 26 | +deno task smoke:test |
| 27 | +# → hello ... ok (1 passed, 0 failed) |
| 28 | +---- |
| 29 | + |
| 30 | +== Writing a test |
| 31 | + |
| 32 | +Each test is a single `.affine` file. The entry point is `fn main() -> Bool`; |
| 33 | +return `true` to pass, `false` to fail. Filename ends in `_test.affine` or |
| 34 | +`.test.affine`. |
| 35 | + |
| 36 | +[source,affine] |
| 37 | +---- |
| 38 | +// example/addition_test.affine |
| 39 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 40 | +fn main() -> Bool { |
| 41 | + let result = 2 + 3; |
| 42 | + result == 5 |
| 43 | +} |
| 44 | +---- |
| 45 | + |
| 46 | +== Running tests |
| 47 | + |
| 48 | +The public API is `runAll(root)` in `mod.ts`: |
| 49 | + |
| 50 | +[source,typescript] |
| 51 | +---- |
| 52 | +// driver.ts |
| 53 | +import { runAll } from "@hyperpolymath/affinescript-deno-test"; |
| 54 | +
|
| 55 | +await runAll("./tests"); |
| 56 | +---- |
| 57 | + |
| 58 | +Invoke with `deno test`: |
| 59 | + |
| 60 | +[source,bash] |
| 61 | +---- |
| 62 | +deno test --allow-read --allow-run --allow-env driver.ts |
| 63 | +---- |
| 64 | + |
| 65 | +The runner: |
| 66 | + |
| 67 | +. Walks `root` for `*_test.affine` / `*.test.affine` files |
| 68 | +. Compiles each via `affinescript compile ... -o ....wasm` |
| 69 | +. Loads the WASM (with a minimal WASI `fd_write` stub — AffineScript codegen |
| 70 | + imports this unconditionally) |
| 71 | +. Registers a `Deno.test()` case named after the file (stripped of the |
| 72 | + `_test` suffix) that invokes `main` and checks the Bool return |
| 73 | + |
| 74 | +== CLI (smoke-check) |
| 75 | + |
| 76 | +`cli.ts` is a thin smoke-check that prints discovery + compile results |
| 77 | +without running assertions. Useful for CI pipelines that want to fail fast |
| 78 | +on compile errors before invoking `deno test`. |
| 79 | + |
| 80 | +[source,bash] |
| 81 | +---- |
| 82 | +deno run --allow-read --allow-run --allow-env cli.ts ./tests |
| 83 | +# Discovered 3 test file(s): |
| 84 | +# ✓ tests/foo_test.affine → tests/foo_test.wasm |
| 85 | +# ✓ tests/bar_test.affine → tests/bar_test.wasm |
| 86 | +# ... |
| 87 | +---- |
| 88 | + |
| 89 | +== Configuration |
| 90 | + |
| 91 | +* `AFFINESCRIPT_BIN` env var — absolute path to the `affinescript` compiler. |
| 92 | + Default: `/var/mnt/eclipse/repos/developer-ecosystem/nextgen-languages/affinescript/_build/install/default/bin/affinescript` |
| 93 | + (the local dev build). Override when the compiler is installed elsewhere |
| 94 | + or on `$PATH`. |
| 95 | + |
| 96 | +[[mvp-constraints]] |
| 97 | +== MVP constraints (v0.1.0) and planned follow-ups |
| 98 | + |
| 99 | +Three limits exist only because of the current AffineScript compiler's |
| 100 | +state. All are resolvable with small upstream changes. |
| 101 | + |
| 102 | +=== One test per file |
| 103 | + |
| 104 | +The AffineScript codegen (`lib/codegen.ml` line 1725) hardcodes the |
| 105 | +exportable-name allowlist to |
| 106 | + |
| 107 | +[source,ocaml] |
| 108 | +---- |
| 109 | +let export_names = ["main"; "init_state"; "step_state"; "get_state"; "mission_active"] |
| 110 | +---- |
| 111 | + |
| 112 | +with no `pub fn` / `@export` / `#[export]` keyword. Any function not on that |
| 113 | +list is internal to the module and cannot be invoked from JS. The harness |
| 114 | +therefore uses `main` as the one sanctioned test slot. |
| 115 | + |
| 116 | +*Planned follow-up:* extend the compiler with a `pub fn` keyword (parser + |
| 117 | +AST + codegen). Once in place, the harness switches to the original design |
| 118 | +of multi-test-per-file via a `test_*` naming convention. |
| 119 | + |
| 120 | +=== WASI `fd_write` imported unconditionally |
| 121 | + |
| 122 | +Every AffineScript WASM output imports `wasi_snapshot_preview1.fd_write` |
| 123 | +even when the program uses no IO. The harness provides a minimal no-op |
| 124 | +stub so tests instantiate cleanly. |
| 125 | + |
| 126 | +*Planned follow-up:* upstream compiler change so pure programs (no `effect |
| 127 | +IO`) skip the WASI import entirely. Until then, the stub is correct and |
| 128 | +harmless. |
| 129 | + |
| 130 | +=== `@hyperpolymath/affine-js` only supplies `env` imports |
| 131 | + |
| 132 | +The existing bridge's `AffineModule.fromBytes` merges custom imports under |
| 133 | +the `env` module key only. For WASM modules that import from a non-`env` |
| 134 | +module (e.g. `wasi_snapshot_preview1`), the harness bypasses `AffineModule` |
| 135 | +and uses raw `WebAssembly.instantiate` plus its own WASI stub. The Bool |
| 136 | +return is unmarshalled by hand (AffineScript compiles `Bool` → i32, 0/1). |
| 137 | + |
| 138 | +*Planned follow-up:* extend `affine-js` to accept a `namespacedImports: |
| 139 | +Record<string, Record<string, ImportValue>>` option, then unify both paths |
| 140 | +behind `AffineModule`. |
| 141 | + |
| 142 | +== Why AffineScript rather than ReScript? |
| 143 | + |
| 144 | +ReScript is the estate's default TypeScript replacement and has a mature |
| 145 | +Deno integration (see `fireflag`). The AffineScript path exists because |
| 146 | +AffineScript has stronger semantics for: |
| 147 | + |
| 148 | +* affine / quantitative types (resource lifecycle tracking) |
| 149 | +* algebraic effects (explicit side-effect boundaries) |
| 150 | +* row polymorphism (extensible records) |
| 151 | + |
| 152 | +Test suites that want to *demonstrate* these properties — e.g. "this |
| 153 | +resource is consumed exactly once", "this protocol sequence is honoured" — |
| 154 | +are a natural fit for AffineScript. Plain behavioural tests can continue |
| 155 | +in ReScript; there's no migration pressure either direction. |
| 156 | + |
| 157 | +The trade-off today: AffineScript's compiler matures while ReScript's is |
| 158 | +mature, so this harness carries the MVP constraints above. As the compiler |
| 159 | +gains `pub fn` / effect-conditional WASI imports / better bridge ergonomics, |
| 160 | +the harness sheds them. |
| 161 | + |
| 162 | +== License |
| 163 | + |
| 164 | +PMPL-1.0-or-later. See the repository root LICENSE file. |
| 165 | +Legal fallback: MPL-2.0 (automatic, per the hyperpolymath License Policy |
| 166 | +Rule 2 in `~/.claude/CLAUDE.md`). |
0 commit comments