|
| 1 | +// webui/test/lib/engine/host-services.test.js |
| 2 | +// |
| 3 | +// The host-services window (placeholder batch PB-8) — shape and contract. |
| 4 | +// |
| 5 | +// The real-host evidence lives next door, in |
| 6 | +// `test/server/host-services-window.test.js`, because booting a runtime |
| 7 | +// costs seconds and belongs with the other real-host suites. What this |
| 8 | +// file owns is everything that can be decided without one, and one of |
| 9 | +// those is load-bearing: |
| 10 | +// |
| 11 | +// 1. Source shape — the window reaches the host through the facade's |
| 12 | +// `getEngineCatalogueHost` and through NOTHING else. The tempting |
| 13 | +// short-cut is to import `engine/providers/local-runtime-v2.js` |
| 14 | +// directly (it is right there, and it is where `services` is |
| 15 | +// forwarded). That would put the whole `@mavis/*` TypeScript tree |
| 16 | +// on the boot path, because `app.js` loads `engine/index.js` at |
| 17 | +// start — the exact regression M1 paid for once (209ms → 2700ms) |
| 18 | +// and the one `host-facade.test.js` exists to guard. A second |
| 19 | +// short-cut is to construct a host here, which is worse: two |
| 20 | +// runtimes over one dataDir is a split brain against the plugin |
| 21 | +// and local-disable tables. |
| 22 | +// |
| 23 | +// 2. Return contract — three distinct answers (`null` for no host, |
| 24 | +// `undefined` for a host without services, the object for a host |
| 25 | +// with them). The middle answer is the one a `??` away from |
| 26 | +// silently collapsing into the first, and it is the answer a |
| 27 | +// consumer gating on presence needs. |
| 28 | +// |
| 29 | +// 3. Scope — the window ships WITHOUT a route. The only thing this |
| 30 | +// batch adds to the HTTP surface is nothing, and that is asserted |
| 31 | +// rather than asserted-in-a-comment: the module exports one |
| 32 | +// function and no handler. |
| 33 | +// |
| 34 | +// Anti-mirroring note, since "改坏必红" is the discipline here: every |
| 35 | +// assertion below is about what the module does or names, not about a |
| 36 | +// restatement of its own implementation. The three ways to break this |
| 37 | +// window — return a hardcoded empty object, bypass the facade singleton, |
| 38 | +// drop the `await` — each turn at least one of these red. |
| 39 | + |
| 40 | +import { test, describe } from "node:test"; |
| 41 | +import assert from "node:assert/strict"; |
| 42 | +import { readFileSync, readdirSync } from "node:fs"; |
| 43 | +import { join } from "node:path"; |
| 44 | + |
| 45 | +const packageDir = join(import.meta.dirname, "..", "..", ".."); |
| 46 | +const serverDir = join(packageDir, "server"); |
| 47 | +const read = (rel) => readFileSync(join(serverDir, rel), "utf8"); |
| 48 | + |
| 49 | +/** Strip comments — this file's prose legitimately names the guarded modules. */ |
| 50 | +function codeOf(source) { |
| 51 | + return source |
| 52 | + .replace(/\/\*[\s\S]*?\*\//g, "") |
| 53 | + .split("\n") |
| 54 | + .map((line) => line.replace(/^\s*\/\/.*$/, "")) |
| 55 | + .join("\n"); |
| 56 | +} |
| 57 | + |
| 58 | +const WINDOW = "engine/host-services.js"; |
| 59 | + |
| 60 | +// --- 1. source shape ------------------------------------------------------- |
| 61 | + |
| 62 | +describe("the window reaches the host through the facade singleton only", () => { |
| 63 | + test(`${WINDOW} takes the host from the engine facade getter`, () => { |
| 64 | + const code = codeOf(read(WINDOW)); |
| 65 | + assert.match( |
| 66 | + code, |
| 67 | + /import\s*\{[^}]*getEngineCatalogueHost[^}]*\}\s*from\s*"\.\/host\.js"/, |
| 68 | + "the window must import getEngineCatalogueHost from ./host.js — the one process-wide host", |
| 69 | + ); |
| 70 | + assert.match(code, /await\s+getHost\(\)/, "the window must await the host getter"); |
| 71 | + }); |
| 72 | + |
| 73 | + test(`${WINDOW} never names a host-construction module`, () => { |
| 74 | + const code = codeOf(read(WINDOW)); |
| 75 | + for (const forbidden of [ |
| 76 | + "acp-client", |
| 77 | + "runtime-host", |
| 78 | + "providers/local-runtime-v2", |
| 79 | + "@mavis/", |
| 80 | + "@minimax/", |
| 81 | + "createCatalogueHost", |
| 82 | + "createLocalRuntimeHostV2", |
| 83 | + ]) { |
| 84 | + assert.ok( |
| 85 | + !code.includes(forbidden), |
| 86 | + `${WINDOW} must not mention ${forbidden} — that is the boot path / second-host rule the facade exists to hold`, |
| 87 | + ); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test("engine/index.js re-exports the window beside the host getter", () => { |
| 92 | + assert.match( |
| 93 | + read("engine/index.js"), |
| 94 | + /export\s*\{[^}]*getHostServices[^}]*\}\s*from\s*"\.\/host-services\.js"/, |
| 95 | + "engine/index.js must re-export getHostServices, or every consumer would import undefined", |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +// --- 2. return contract ---------------------------------------------------- |
| 101 | + |
| 102 | +describe("the window reports what the host actually carries", () => { |
| 103 | + test("returns the host's services object by reference, not a copy", async () => { |
| 104 | + const { getHostServices } = await import("../../../server/engine/host-services.js"); |
| 105 | + const services = { managedWorktrees: { list() {} }, cron: undefined }; |
| 106 | + const host = { services }; |
| 107 | + assert.equal( |
| 108 | + await getHostServices({ getHost: async () => host }), |
| 109 | + services, |
| 110 | + "the window must forward the runtime's own object — a clone would freeze the truth at open time", |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test("answers null — not undefined — when there is no host", async () => { |
| 115 | + const { getHostServices } = await import("../../../server/engine/host-services.js"); |
| 116 | + const answer = await getHostServices({ getHost: async () => null }); |
| 117 | + assert.equal( |
| 118 | + answer, |
| 119 | + null, |
| 120 | + "no runtime is `null`, matching getEngineCatalogueHost — a caller mapping null to RUNTIME_UNAVAILABLE must not fire for a host that merely lacks services", |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + test("answers undefined — not null — when a host carries no services", async () => { |
| 125 | + const { getHostServices } = await import("../../../server/engine/host-services.js"); |
| 126 | + const answer = await getHostServices({ getHost: async () => ({ cliService: {} }) }); |
| 127 | + assert.equal( |
| 128 | + answer, |
| 129 | + undefined, |
| 130 | + "a host without the member is a distinct state; `?? null` here would erase the difference a consumer gating on presence depends on", |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + test("propagates a throwing host getter unchanged", async () => { |
| 135 | + const { getHostServices } = await import("../../../server/engine/host-services.js"); |
| 136 | + const boom = new Error("runtime boot failed"); |
| 137 | + await assert.rejects( |
| 138 | + getHostServices({ |
| 139 | + getHost: async () => { |
| 140 | + throw boom; |
| 141 | + }, |
| 142 | + }), |
| 143 | + (error) => error === boom, |
| 144 | + "the window owns no failure mapping — the caller does, exactly as through getEngineCatalogueHost", |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + test("defaults to the facade getter when no seam is injected", () => { |
| 149 | + // Not a behaviour test: the default is what every real consumer uses, |
| 150 | + // and a window that only worked with an injected getter would be dead |
| 151 | + // code that its own unit tests pass. |
| 152 | + const code = codeOf(read(WINDOW)); |
| 153 | + assert.match( |
| 154 | + code, |
| 155 | + /const\s+getHost\s*=\s*deps\.getHost\s*\?\?\s*getEngineCatalogueHost/, |
| 156 | + "the default must be the real facade getter", |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +// --- 3. scope -------------------------------------------------------------- |
| 162 | + |
| 163 | +describe("the batch ships a window and no endpoint", () => { |
| 164 | + test(`${WINDOW} exports the window and nothing else`, async () => { |
| 165 | + const module = await import("../../../server/engine/host-services.js"); |
| 166 | + assert.deepEqual( |
| 167 | + Object.keys(module), |
| 168 | + ["getHostServices"], |
| 169 | + "a handler exported from this module would mean the batch attached a route; PB-3 / PB-7 own those", |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + test("no HTTP route file consumes the window yet", () => { |
| 174 | + const routeDir = join(serverDir, "routes"); |
| 175 | + const routes = readdirSync(routeDir).filter((name) => name.endsWith(".js")); |
| 176 | + const consumers = routes.filter((name) => codeOf(read(join("routes", name))).includes("getHostServices")); |
| 177 | + assert.deepEqual( |
| 178 | + consumers, |
| 179 | + [], |
| 180 | + "PB-8 is the prerequisite, not the delivery — a route here belongs to the batch that owns that capability", |
| 181 | + ); |
| 182 | + }); |
| 183 | +}); |
0 commit comments