import { test } from "node:test"; import assert from "node:assert/strict"; import { promises as fs } from "node:fs"; import path from "node:path"; import os from "node:os"; import { loadRegisteredCapabilities } from "../loader.js"; import { getCapability, listCapabilities } from "../registry.js"; /** * Task #242 (B1) — architect review smoke test: * 验证 boot loader 真的能扫到 _registered 目录里的 manifest 文件并把它们注册 * 进 in-memory registry。否则即便 boot 调了 loadRegisteredCapabilities,真 * capability 落地后也不会被自动登记 — 整个生命周期机制就废了。 */ void test("capability/loader: scans manifest files and auto-registers them", async () => { // Make a sandbox dir with two valid manifests + 1 bogus file. const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "cap-loader-test-")); try { const m1 = path.join(tmp, "alpha.manifest.mjs"); const m2 = path.join(tmp, "beta.manifest.mjs"); const ignored = path.join(tmp, "readme.txt"); await fs.writeFile( m1, `export default { id: "cap_loader_test_alpha", name: "loader_test_alpha", domain: "test", boundNetworkName: null, manifestVersion: 1, };\n`, ); await fs.writeFile( m2, `export default { id: "cap_loader_test_beta", name: "loader_test_beta", domain: "test", boundNetworkName: null, manifestVersion: 1, };\n`, ); await fs.writeFile(ignored, "not a manifest\n"); const result = await loadRegisteredCapabilities(tmp); assert.equal(result.errors.length, 0, `unexpected errors: ${JSON.stringify(result.errors)}`); assert.deepEqual( [...result.loaded].sort(), ["cap_loader_test_alpha", "cap_loader_test_beta"], "both manifests should be loaded; the .txt file should be ignored", ); // Architect's real concern: did the manifests actually land in the // in-memory registry? Boot logging "loaded" without actually // registering would still ship a broken layer. const alpha = getCapability("cap_loader_test_alpha"); const beta = getCapability("cap_loader_test_beta"); assert.ok(alpha, "alpha capability missing from registry after boot scan"); assert.ok(beta, "beta capability missing from registry after boot scan"); assert.equal(alpha!.manifest.name, "loader_test_alpha"); assert.equal(beta!.manifest.name, "loader_test_beta"); // Sanity: listCapabilities returns at least these two. const ids = listCapabilities().map((m) => m.id); assert.ok(ids.includes("cap_loader_test_alpha")); assert.ok(ids.includes("cap_loader_test_beta")); } finally { await fs.rm(tmp, { recursive: true, force: true }); } }); void test("capability/loader: DOATLAS_CAPABILITY_LAYER_ENABLED=false short-circuits", async () => { const prev = process.env["DOATLAS_CAPABILITY_LAYER_ENABLED"]; process.env["DOATLAS_CAPABILITY_LAYER_ENABLED"] = "false"; try { const result = await loadRegisteredCapabilities("/this/path/does/not/exist"); assert.deepEqual(result, { loaded: [], errors: [] }); } finally { if (prev === undefined) delete process.env["DOATLAS_CAPABILITY_LAYER_ENABLED"]; else process.env["DOATLAS_CAPABILITY_LAYER_ENABLED"] = prev; } });