@crewhaus/hangar-ui 0.5.7 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,289 @@
1
+ /**
2
+ * The embed-map completeness + asset-hygiene suite.
3
+ *
4
+ * DELIBERATELY NOT NAMED `*.test.ts`: this suite must run in its OWN
5
+ * process (`bun test ./src/embed-suite.ts`, the second half of the package
6
+ * test script). Importing `./index` loads every asset file as TEXT
7
+ * (`with { type: "text" }`), and bun's module registry keys a module by
8
+ * path alone — the import attribute is not part of the key — so in a
9
+ * process that ALSO imports an asset as an ES module (every view test),
10
+ * whichever load happens first poisons the other: the ESM side dies with
11
+ * "Export not found", or this side with "Missing 'default' export",
12
+ * depending on nothing but file order. Two processes, two registries, no
13
+ * collision — and `bun test src` skips this file because the name is off
14
+ * the test glob.
15
+ */
16
+ import { describe, expect, test } from "bun:test";
17
+ import { readFileSync, readdirSync, statSync } from "node:fs";
18
+ import { join } from "node:path";
19
+ import { CONTENT_TYPES, contentTypeFor, hangarAssets } from "./index";
20
+ const ASSETS_DIR = join(import.meta.dir, "..", "assets");
21
+ /** Walk assets/ and return every file's path relative to it (posix slashes). */
22
+ function walkAssets(dir = ASSETS_DIR, prefix = "") {
23
+ const out = [];
24
+ for (const name of readdirSync(dir).sort()) {
25
+ const full = join(dir, name);
26
+ const rel = prefix === "" ? name : `${prefix}/${name}`;
27
+ if (statSync(full).isDirectory())
28
+ out.push(...walkAssets(full, rel));
29
+ else
30
+ out.push(rel);
31
+ }
32
+ return out;
33
+ }
34
+ /** The serve path an asset file is expected to appear under. */
35
+ function servePathFor(rel) {
36
+ return rel === "index.html" ? "/" : `/assets/${rel}`;
37
+ }
38
+ describe("embed map completeness", () => {
39
+ const files = walkAssets();
40
+ test("assets tree is non-trivial", () => {
41
+ expect(files.length).toBeGreaterThanOrEqual(10);
42
+ expect(files).toContain("index.html");
43
+ expect(files).toContain("hangar.css");
44
+ expect(files).toContain("js/app.js");
45
+ });
46
+ test("every M2 screen ships as an embedded module with the js content type", () => {
47
+ const m2 = [
48
+ "/assets/js/supervision.js",
49
+ "/assets/js/views/proc.js",
50
+ "/assets/js/views/runs.js",
51
+ "/assets/js/views/control.js",
52
+ "/assets/js/views/schedulers.js",
53
+ "/assets/js/views/approvals.js",
54
+ "/assets/js/views/review.js",
55
+ "/assets/js/views/inbox.js",
56
+ "/assets/js/views/activity.js",
57
+ "/assets/js/views/jobs.js",
58
+ "/assets/js/views/deploy.js",
59
+ ];
60
+ for (const key of m2) {
61
+ const entry = hangarAssets[key];
62
+ expect(`${key}:${entry !== undefined}`).toBe(`${key}:true`);
63
+ expect(`${key}:${entry?.contentType}`).toBe(`${key}:text/javascript; charset=utf-8`);
64
+ expect(`${key}:${(entry?.body.length ?? 0) > 0}`).toBe(`${key}:true`);
65
+ }
66
+ });
67
+ test("every M3 area ships a view module (one per implementer area)", () => {
68
+ const m3 = [
69
+ "/assets/js/views/spec-edit.js",
70
+ "/assets/js/views/memory-fabric.js",
71
+ "/assets/js/views/evals-lab.js",
72
+ "/assets/js/views/data.js",
73
+ "/assets/js/views/feedback.js",
74
+ "/assets/js/views/creds.js",
75
+ "/assets/js/views/channels.js",
76
+ "/assets/js/views/security.js",
77
+ "/assets/js/views/thredz.js",
78
+ "/assets/js/views/inspect.js",
79
+ "/assets/js/views/runtime.js",
80
+ ];
81
+ for (const key of m3) {
82
+ const entry = hangarAssets[key];
83
+ expect(`${key}:${entry !== undefined}`).toBe(`${key}:true`);
84
+ expect(`${key}:${entry?.contentType}`).toBe(`${key}:text/javascript; charset=utf-8`);
85
+ expect(`${key}:${(entry?.body.length ?? 0) > 0}`).toBe(`${key}:true`);
86
+ }
87
+ });
88
+ test("every file under assets/ is embedded under its serve path with exact bytes", () => {
89
+ for (const rel of files) {
90
+ const key = servePathFor(rel);
91
+ const entry = hangarAssets[key];
92
+ expect(entry).toBeDefined();
93
+ const onDisk = readFileSync(join(ASSETS_DIR, rel), "utf8");
94
+ expect(entry?.body).toBe(onDisk);
95
+ }
96
+ });
97
+ test("the map holds nothing that is not on disk (no stale keys)", () => {
98
+ const expected = files.map(servePathFor).sort();
99
+ expect(Object.keys(hangarAssets).sort()).toEqual(expected);
100
+ });
101
+ test("content types are right per extension", () => {
102
+ expect(hangarAssets["/"]?.contentType).toBe("text/html; charset=utf-8");
103
+ expect(hangarAssets["/assets/hangar.css"]?.contentType).toBe("text/css; charset=utf-8");
104
+ for (const [key, entry] of Object.entries(hangarAssets)) {
105
+ if (key.endsWith(".js"))
106
+ expect(entry.contentType).toBe("text/javascript; charset=utf-8");
107
+ }
108
+ });
109
+ test("every body is non-empty", () => {
110
+ for (const entry of Object.values(hangarAssets)) {
111
+ expect(entry.body.length).toBeGreaterThan(0);
112
+ }
113
+ });
114
+ });
115
+ describe("contentTypeFor", () => {
116
+ test("known extensions", () => {
117
+ expect(contentTypeFor("/assets/js/app.js")).toBe("text/javascript; charset=utf-8");
118
+ expect(contentTypeFor("x.css")).toBe("text/css; charset=utf-8");
119
+ expect(contentTypeFor("index.html")).toBe("text/html; charset=utf-8");
120
+ expect(contentTypeFor("a.svg")).toBe("image/svg+xml");
121
+ });
122
+ test("unknown/none falls back to octet-stream", () => {
123
+ expect(contentTypeFor("noext")).toBe("application/octet-stream");
124
+ expect(contentTypeFor("weird.wasm")).toBe("application/octet-stream");
125
+ });
126
+ test("CONTENT_TYPES covers every embedded extension", () => {
127
+ for (const key of Object.keys(hangarAssets)) {
128
+ if (key === "/")
129
+ continue;
130
+ const dot = key.lastIndexOf(".");
131
+ expect(dot).toBeGreaterThan(0);
132
+ expect(CONTENT_TYPES[key.slice(dot)]).toBeDefined();
133
+ }
134
+ });
135
+ });
136
+ describe("shell wiring", () => {
137
+ test("index.html references the stylesheet and the module entry by serve path", () => {
138
+ const html = hangarAssets["/"]?.body ?? "";
139
+ expect(html).toContain('href="/assets/hangar.css"');
140
+ expect(html).toContain('src="/assets/js/app.js"');
141
+ expect(html).toContain('type="module"');
142
+ });
143
+ test("index.html defaults to the dark theme", () => {
144
+ expect(hangarAssets["/"]?.body ?? "").toContain('data-theme="dark"');
145
+ });
146
+ });
147
+ describe("js module hygiene", () => {
148
+ const jsEntries = Object.entries(hangarAssets).filter(([key]) => key.endsWith(".js"));
149
+ test("every js asset parses as a valid module (zero-build means no compile step to catch this)", () => {
150
+ const transpiler = new Bun.Transpiler({ loader: "js" });
151
+ for (const [key, entry] of jsEntries) {
152
+ let failed = "";
153
+ try {
154
+ transpiler.transformSync(entry.body);
155
+ }
156
+ catch (err) {
157
+ failed = `${key}: ${err instanceof Error ? err.message : String(err)}`;
158
+ }
159
+ expect(failed).toBe("");
160
+ }
161
+ });
162
+ test("no innerHTML anywhere (injection-safe DOM building only)", () => {
163
+ for (const [key, entry] of jsEntries) {
164
+ expect(`${key}:${entry.body.includes("innerHTML")}`).toBe(`${key}:false`);
165
+ }
166
+ });
167
+ test("no markup-string assignment of any kind (the ban is wider than innerHTML)", () => {
168
+ // Built from parts so this test file cannot itself trip a scanner.
169
+ const banned = [
170
+ "outer" + "HTML",
171
+ "insertAdjacent" + "HTML",
172
+ "document." + "write",
173
+ "createContextual" + "Fragment",
174
+ ];
175
+ for (const [key, entry] of jsEntries) {
176
+ for (const needle of banned) {
177
+ expect(`${key}:${needle}:${entry.body.includes(needle)}`).toBe(`${key}:${needle}:false`);
178
+ }
179
+ }
180
+ });
181
+ test("srcdoc appears in exactly one module, and only alongside a sandbox", () => {
182
+ // M4 (HM-179) introduces the ONE deliberate exception to the
183
+ // markup-string ban: a plugin pane's document is handed to an iframe via
184
+ // `srcdoc`. That is safe precisely because the string never joins THIS
185
+ // document — the frame gets an opaque origin (no `allow-same-origin`)
186
+ // and a CSP. Both halves are load-bearing, so the exception is pinned to
187
+ // one file and to the presence of the sandbox attribute beside it; a
188
+ // second module reaching for `srcdoc` fails here.
189
+ const users = jsEntries
190
+ .filter(([, entry]) => entry.body.includes("srcdoc"))
191
+ .map(([key]) => key);
192
+ expect(users).toEqual(["/assets/js/views/panes.js"]);
193
+ const panes = hangarAssets["/assets/js/views/panes.js"]?.body ?? "";
194
+ expect(panes).toContain("sandbox:");
195
+ // …and it is the ONLY module allowed to build an iframe at all: the
196
+ // sandbox is what makes the exception safe, so a frame appearing
197
+ // elsewhere would be one nobody had to think about.
198
+ const frames = jsEntries
199
+ .filter(([, entry]) => entry.body.includes('el("iframe"'))
200
+ .map(([key]) => key);
201
+ expect(frames).toEqual(["/assets/js/views/panes.js"]);
202
+ // The sandbox token list itself (never `allow-same-origin` alongside
203
+ // `allow-scripts`) is proven behaviourally in `m4-views.test.ts`, where
204
+ // `paneSandbox` is called with a payload that tries to widen it.
205
+ });
206
+ test("no module hardcodes a `harness scan-root` verb (the CLI has none)", () => {
207
+ // The console's CLI twins are its trust affordance: a command shown
208
+ // beside a button has to be one a terminal accepts. `crewhaus harness`
209
+ // takes list|show|add|remove|relocate|group|tag|pin|scan|preflight — a
210
+ // scan root is added by `harness scan --root <dir>`, which remembers the
211
+ // root before it walks it. A twin nobody can paste teaches the opposite
212
+ // of what the discipline is for, and the first-boot screen is where an
213
+ // operator learns whether to believe them.
214
+ for (const [key, entry] of jsEntries) {
215
+ expect(`${key}:${entry.body.includes("scan-root add")}`).toBe(`${key}:false`);
216
+ }
217
+ });
218
+ test("the evaluating notifications GET has exactly one caller, and the app runs it", () => {
219
+ // `GET /api/notifications` IS the rules evaluation (HM-183: the manager
220
+ // runs no timer). Every delivery it returns is added to the server's
221
+ // dedupe set and never returned again — so a second caller does not read
222
+ // the same state, it CONSUMES it, and the toast for whatever it ate can
223
+ // never appear again in that manager process. One caller, therefore, and
224
+ // every screen that needs the state goes through it.
225
+ const callers = jsEntries
226
+ .filter(([, entry]) => entry.body.includes("api.notifications("))
227
+ .map(([key]) => key);
228
+ expect(callers).toEqual(["/assets/js/notify.js"]);
229
+ // …and the app must actually START that loop: a poll nobody runs is the
230
+ // boot-time read this whole arrangement exists to replace.
231
+ expect(hangarAssets["/assets/js/app.js"]?.body ?? "").toContain("notifications.start()");
232
+ });
233
+ test("every relative import resolves to another embedded asset (import-graph closure)", () => {
234
+ const importRe = /import\s+[^"']*?["']([^"']+)["']/g;
235
+ for (const [key, entry] of jsEntries) {
236
+ const dir = key.slice(0, key.lastIndexOf("/"));
237
+ for (const match of entry.body.matchAll(importRe)) {
238
+ const spec = match[1] ?? "";
239
+ expect(spec.startsWith("./") || spec.startsWith("../")).toBe(true);
240
+ const resolved = resolvePosix(dir, spec);
241
+ expect(hangarAssets[resolved]).toBeDefined();
242
+ }
243
+ }
244
+ });
245
+ test("no orphaned module is shipped (every js asset is reachable from the shell entry)", () => {
246
+ // R-20: `pending.js` — the stub-era "not built yet" screen — outlived its
247
+ // last importer and was still embedded, served over HTTP and compiled
248
+ // into the single-binary CLI, with a docblock telling readers that
249
+ // eleven live screens "have no data behind them yet". A dead module in a
250
+ // zero-build tree has nothing to catch it: the transpiler, the
251
+ // completeness check and the import-graph closure above all pass on code
252
+ // nobody imports. Reachability is the check that does not.
253
+ const entry = "/assets/js/app.js";
254
+ const importRe = /import\s+[^"']*?["']([^"']+)["']/g;
255
+ const seen = new Set([entry]);
256
+ const queue = [entry];
257
+ while (queue.length > 0) {
258
+ const key = queue.pop();
259
+ const body = hangarAssets[key]?.body ?? "";
260
+ const dir = key.slice(0, key.lastIndexOf("/"));
261
+ for (const match of body.matchAll(importRe)) {
262
+ const resolved = resolvePosix(dir, match[1] ?? "");
263
+ if (seen.has(resolved))
264
+ continue;
265
+ seen.add(resolved);
266
+ queue.push(resolved);
267
+ }
268
+ }
269
+ const orphans = jsEntries.map(([key]) => key).filter((key) => !seen.has(key));
270
+ expect(orphans).toEqual([]);
271
+ });
272
+ test("browser modules never import bun/node builtins", () => {
273
+ for (const [key, entry] of jsEntries) {
274
+ expect(`${key}:${/from\s+["'](?:bun|node:)/.test(entry.body)}`).toBe(`${key}:false`);
275
+ }
276
+ });
277
+ });
278
+ function resolvePosix(fromDir, spec) {
279
+ const parts = fromDir.split("/").filter((p) => p !== "");
280
+ for (const seg of spec.split("/")) {
281
+ if (seg === "" || seg === ".")
282
+ continue;
283
+ if (seg === "..")
284
+ parts.pop();
285
+ else
286
+ parts.push(seg);
287
+ }
288
+ return `/${parts.join("/")}`;
289
+ }
package/dist/index.js CHANGED
@@ -37,6 +37,7 @@ import shapesJs from "../assets/js/shapes.js" with { type: "text" };
37
37
  import supervisionJs from "../assets/js/supervision.js" with { type: "text" };
38
38
  import utilJs from "../assets/js/util.js" with { type: "text" };
39
39
  import activityViewJs from "../assets/js/views/activity.js" with { type: "text" };
40
+ import advisorViewJs from "../assets/js/views/advisor.js" with { type: "text" };
40
41
  import approvalsViewJs from "../assets/js/views/approvals.js" with { type: "text" };
41
42
  import channelsViewJs from "../assets/js/views/channels.js" with { type: "text" };
42
43
  import controlViewJs from "../assets/js/views/control.js" with { type: "text" };
@@ -54,6 +55,7 @@ import jobsViewJs from "../assets/js/views/jobs.js" with { type: "text" };
54
55
  import libraryViewJs from "../assets/js/views/library.js" with { type: "text" };
55
56
  import memoryFabricViewJs from "../assets/js/views/memory-fabric.js" with { type: "text" };
56
57
  import memoryViewJs from "../assets/js/views/memory.js" with { type: "text" };
58
+ import modelsViewJs from "../assets/js/views/models.js" with { type: "text" };
57
59
  import onboardingViewJs from "../assets/js/views/onboarding.js" with { type: "text" };
58
60
  import overviewViewJs from "../assets/js/views/overview.js" with { type: "text" };
59
61
  import panesViewJs from "../assets/js/views/panes.js" with { type: "text" };
@@ -110,6 +112,7 @@ export const hangarAssets = {
110
112
  "/assets/js/supervision.js": asset(supervisionJs, "supervision.js"),
111
113
  "/assets/js/util.js": asset(utilJs, "util.js"),
112
114
  "/assets/js/views/activity.js": asset(activityViewJs, "activity.js"),
115
+ "/assets/js/views/advisor.js": asset(advisorViewJs, "advisor.js"),
113
116
  "/assets/js/views/approvals.js": asset(approvalsViewJs, "approvals.js"),
114
117
  "/assets/js/views/channels.js": asset(channelsViewJs, "channels.js"),
115
118
  "/assets/js/views/control.js": asset(controlViewJs, "control.js"),
@@ -127,6 +130,7 @@ export const hangarAssets = {
127
130
  "/assets/js/views/library.js": asset(libraryViewJs, "library.js"),
128
131
  "/assets/js/views/memory-fabric.js": asset(memoryFabricViewJs, "memory-fabric.js"),
129
132
  "/assets/js/views/memory.js": asset(memoryViewJs, "memory.js"),
133
+ "/assets/js/views/models.js": asset(modelsViewJs, "models.js"),
130
134
  "/assets/js/views/onboarding.js": asset(onboardingViewJs, "onboarding.js"),
131
135
  "/assets/js/views/overview.js": asset(overviewViewJs, "overview.js"),
132
136
  "/assets/js/views/panes.js": asset(panesViewJs, "panes.js"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/hangar-ui",
3
- "version": "0.5.7",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "The Hangar web console's static UI — zero-build, dependency-free browser ES modules embedded as text assets for the single-binary CLI to hand to the hangar server",
6
6
  "main": "dist/index.js",
@@ -12,7 +12,7 @@
12
12
  }
13
13
  },
14
14
  "scripts": {
15
- "test": "bun test src"
15
+ "test": "bun test src && bun test ./src/embed-suite.ts"
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "author": {