Files
zip/js/bundle_test.go
zeekayandHanzo Dev 5dfa4ef616 plugin: a repo declares its routes, and the host discovers them
HIP-0106's plugin contract, in the framework rather than in 116 repositories.

Declaration projects the LIVE ROUTER — every method+pattern, complete by
construction, so a route a plugin serves and does not publish is still
declared. That is the analytics-ingestion outage class: the published paths
were listed, the four ingestion doors were not, and every product beacon in
the fleet answered 405.

  Declaration/Route/(*App).Declaration   what a host discovers
  /.well-known/zip/plugin.json           the same value off a running child
  (*App).Described                       `<bin> openapi <f>` / `<bin> declare <f>`
  Tenant                                 the tenancy read, with the real rule
  Delegate                               a continuation for another org
  Ask                                    dial a peer by name, invoke one op
  (*App).Peer                            peer ops on their own app, own socket
  (*App).Ops                             /healthz + /readyz + /metrics
  Config.Eager                           the one fact a router cannot show
  App.claim                              a second claim of a prefix is an ERROR
  zip/runtime -> zip/js                  a language runtime is an adaptor

Four things the reference plugin falsified while being built:

- Describe was already taken. zip.Describe(methodPath, Doc) is the doc
  registry generated code calls. The projection writer is (*App).Described,
  one call, because argv is read in one place and a main cannot forget to
  forward it.

- zip's control plane is NOT all under /.well-known/. /docs and the MCP path
  are not, so a prefix filter let a child declare them — paths outside /v1/
  that every plugin serves and whose first claimant takes the HOST's document
  and agent door. App.control now registers and records in one call, so
  leaking one is impossible rather than unlikely.

- OPS_PORT appeared in no Go file in the fleet. HIP-0119 named the paths and
  nobody implemented them, so conformance meant 116 repos hand-rolling three
  routes and a listener. It is one sibling app now, brought up by Listen.

- There is deliberately NO default ops port. A default made every second app
  in a process die on "address already in use" — a plugin has two apps in it
  and a test process has many. The deployment states OPS_PORT; a host states
  none on a child, so a child correctly owns none.

Load's duplicate claim used to return nil while the first registration kept
answering: the losing plugin's whole surface served another plugin's 404 and
nothing said so. It is now an error naming both claimants, all-or-nothing
across a plugin's prefixes, taken before anything is spawned.

a.ops was the op registry and is now a.registry — the ONE registry the
OpenAPI document, the MCP tool, the CLI command and the plane op are all
projected from. a.ops is the ops sibling.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-29 17:54:28 -07:00

128 lines
3.5 KiB
Go

package js
import (
"strings"
"testing"
)
// A two-file CommonJS tree (entry.js requires ./util.js) bundles to a
// single ES2015 string that runs in goja and produces the expected value.
func TestBundleToES2015_TwoFileCommonJS(t *testing.T) {
files := map[string][]byte{
"util.js": []byte(`
module.exports.triple = function (n) { return n * 3; };
`),
"entry.js": []byte(`
const util = require("./util.js");
module.exports.run = function () { return util.triple(14); };
`),
}
out, err := BundleToES2015("entry.js", files, BundleOptions{Format: "cjs"})
if err != nil {
t.Fatal(err)
}
rt, _ := NewJSRuntime(JSOptions{PoolSize: 1})
if err := rt.LoadModule("bundle", string(out)); err != nil {
t.Fatal(err)
}
v, err := rt.Eval(`require("bundle").run()`)
if err != nil {
t.Fatal(err)
}
if got, ok := v.(int64); !ok || got != 42 {
t.Fatalf("entry.run() = %v (%T), want int64(42)", v, v)
}
t.Logf("two-file bundle: entry.run() == %d (want 42)", v)
}
// TypeScript across files bundles and strips types.
func TestBundleToES2015_TypeScriptTree(t *testing.T) {
files := map[string][]byte{
"math.ts": []byte(`export const square = (n: number): number => n * n;`),
"entry.ts": []byte(`
import { square } from "./math";
module.exports.run = (): number => square(7);
`),
}
out, err := BundleToES2015("entry.ts", files, BundleOptions{Format: "cjs"})
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), ": number") {
t.Fatalf("types not stripped: %s", out)
}
rt, _ := NewJSRuntime(JSOptions{PoolSize: 1})
if err := rt.LoadModule("b", string(out)); err != nil {
t.Fatal(err)
}
v, err := rt.Eval(`require("b").run()`)
if err != nil {
t.Fatal(err)
}
if got := v.(int64); got != 49 {
t.Fatalf("run() = %d, want 49", got)
}
}
// An external dependency must stay as a require("external") call in the
// output rather than being inlined (it is not in the files map).
func TestBundleToES2015_ExternalStaysRequire(t *testing.T) {
files := map[string][]byte{
"entry.js": []byte(`
const ext = require("external");
module.exports.run = function () { return ext.value; };
`),
}
out, err := BundleToES2015("entry.js", files, BundleOptions{
Format: "cjs",
External: []string{"external"},
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), `require("external")`) {
t.Fatalf("external not preserved as require: %s", out)
}
t.Logf(`external preserved: output contains require("external")`)
// And it must actually resolve through goja's require() to the host
// module registered under the same name.
rt, _ := NewJSRuntime(JSOptions{
PoolSize: 1,
Modules: map[string]string{"external": `module.exports = { value: 99 };`},
})
if err := rt.LoadModule("bundle", string(out)); err != nil {
t.Fatal(err)
}
v, err := rt.Eval(`require("bundle").run()`)
if err != nil {
t.Fatal(err)
}
if got := v.(int64); got != 99 {
t.Fatalf("run() = %d, want 99 (from external module)", got)
}
}
// A missing entry is a clear error, not a panic.
func TestBundleToES2015_MissingEntry(t *testing.T) {
_, err := BundleToES2015("nope.js", map[string][]byte{"a.js": []byte("1")}, BundleOptions{})
if err == nil {
t.Fatal("expected error for missing entry, got nil")
}
}
// An unresolved relative import is reported as an error.
func TestBundleToES2015_UnresolvedImport(t *testing.T) {
files := map[string][]byte{
"entry.js": []byte(`require("./missing.js");`),
}
_, err := BundleToES2015("entry.js", files, BundleOptions{})
if err == nil {
t.Fatal("expected error for unresolved import, got nil")
}
}