From 65b7495eca46a66e9a627e9b54de50149568ab2d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 01:50:49 +0000 Subject: [PATCH] docs: test gap analysis and tests for leak audit and DEV gates Add docs/test-gaps.md with per-package coverage summaries, ten highest-risk gaps, and follow-ups. Add unit tests for leakAudit, parseFrontmatter, publicEntries helpers, and isLoopbackHost without changing runtime behaviour. Co-authored-by: Charles Berret --- docs/test-gaps.md | 149 ++++++++++++++++++++++++++ packages/dev-api/src/contract.test.ts | 17 +++ packages/site/src/frontmatter.test.ts | 31 ++++++ packages/site/src/leak-audit.test.ts | 76 +++++++++++++ packages/site/src/public.test.ts | 34 ++++++ 5 files changed, 307 insertions(+) create mode 100644 docs/test-gaps.md create mode 100644 packages/dev-api/src/contract.test.ts create mode 100644 packages/site/src/frontmatter.test.ts create mode 100644 packages/site/src/leak-audit.test.ts create mode 100644 packages/site/src/public.test.ts diff --git a/docs/test-gaps.md b/docs/test-gaps.md new file mode 100644 index 0000000..fe146f6 --- /dev/null +++ b/docs/test-gaps.md @@ -0,0 +1,149 @@ +# Test gap analysis + +Sprint baseline: current `main` workspace packages and root scripts. Package tests use Node’s built-in test runner with `tsx` for TypeScript. Run `npm run build` before `npm test` so workspace packages resolve each other’s `dist/` outputs (CI does this in `.github/workflows/check.yml`). + +## Coverage by unit + +### Root + +| Unit | Test command | Summary | +| --- | --- | --- | +| **shellside (aggregate)** | `npm test` | Chains all seven workspace package test scripts in dependency-friendly order. | +| **scripts/** | `node --test scripts/*.test.mjs` | Also run from `npm run check` after reference build. | + +``` +$ npm test +# (final workspace in chain) +# tests 15 +# pass 15 +# fail 0 +``` + +``` +$ node --test scripts/*.test.mjs +# tests 3 +# suites 2 +# pass 3 +# fail 0 +``` + +### `@shellside/content-model` + +| Command | `npm run test -w @shellside/content-model` | +| Summary | `# tests 10` · `# suites 3` · `# pass 10` · `# fail 0` | + +**Source:** `visibility.ts` only. **Tests:** `visibility.test.ts` (normalize, public filter, legacy status, `contentContextFromSearch`). `index.ts` is re-exports. + +### `@shellside/edit-protocol` + +| Command | `npm run test -w @shellside/edit-protocol` | +| Summary | `# tests 16` · `# suites 5` · `# pass 16` · `# fail 0` | + +**Source:** `paths.ts`, `preview.ts`, `preview-bridge.ts`, `kinds.ts`, `allow.ts`, `files.ts`. **Tests:** `paths.test.ts`, `preview.test.ts`, `preview-bridge.test.ts`, `parity.test.ts` (kinds, allow, files parity with desk expectations). No isolated unit file for `allow.ts` / `kinds.ts` beyond parity. + +### `@shellside/look` + +| Command | `npm run test -w @shellside/look` | +| Summary | `# tests 72` · `# suites 11` · `# pass 72` · `# fail 0` | + +**Source:** field styles, tokens, roles, history, fs adapters, design tokens, presets. **Tests:** `field-styles.test.ts`, `field-styles-fs.test.ts`, `history-fs.test.ts`, `roles.test.ts`, `tokens.test.ts`. **Untested modules:** `options.ts`, `weights.ts` (constants consumed by tests indirectly), thin `fs.ts` barrel. + +### `@shellside/dev-api` + +| Command | `npm run test -w @shellside/dev-api` | +| Summary | `# tests 16` · `# suites 2` · `# pass 16` · `# fail 0` | + +**Source:** `content-editor.ts`, `handler.ts`, `contract.ts`. **Tests:** `editor.test.ts` (editor + HTTP handler), `contract.test.ts` (`isLoopbackHost`). `contract.ts` string notes are documentation only. + +### `@shellside/console-vfs` + +| Command | `npm run test -w @shellside/console-vfs` | +| Summary | `# tests 6` · `# pass 6` · `# fail 0` | + +**Source:** `vfs.ts`, `project.ts`. **Tests:** `vfs.test.ts` (mount, walk, `projectCollections` flat and grouped). High line coverage for a small package. + +### `@shellside/console-engine` + +| Command | `npm run test -w @shellside/console-engine` | +| Summary | `# tests 11` · `# pass 11` · `# fail 0` | + +**Source:** parse, path, io, pipeline, complete, history, `runner.ts`, `commands/*`. **Tests:** one file per subsystem under `src/` and `src/commands/` except **`runner.ts`** (covered indirectly via `pipeline.test.ts` `createRunner`). No browser/DOM tests for console UI (lives in `packages/site/pages/console.astro`). + +### `@shellside/site` + +| Command | `npm run test -w @shellside/site` | +| Summary | `# tests 15` · `# pass 15` · `# fail 0` | + +**Source:** Astro integration (`index.ts`), `config.ts`, `site-tree.ts`, `frontmatter.ts`, `public.ts`, `leak-audit.ts`, `endpoints/*.ts`, packaged `console.astro`. **Tests:** `index.test.ts`, `site-tree.test.ts`, `frontmatter.test.ts`, `public.test.ts`, `leak-audit.test.ts`. **No direct tests:** `endpoints/list-txt.ts`, `llms-txt.ts`, `section-txt.ts` (exercised only via `build:reference` / `check-public-output`). + +### Apps (no package test script) + +| Unit | Verification in CI | +| --- | --- | +| `apps/reference` | `npm run build:reference` + `node scripts/check-reference.mjs` | +| `apps/demo` | `astro build` + `scripts/check.mjs` (leak audit on demo dist) | + +### `scripts/` + +| Command | `node --test scripts/*.test.mjs` | +| Summary | `# tests 3` · `# suites 2` · `# pass 3` · `# fail 0` | + +**Tests:** `export-marquee-content.test.mjs` (redaction rules + export). **Untested scripts:** `check-reference.mjs`, `check-public-output.mjs`, `prepare-reference-content.mjs`, `sync-marquee-vendor.mjs`, `create-site.mjs` (integration-only). + +--- + +## Ten riskiest previously untested behaviours + +Ordered by impact if they regress (security and content leakage first). + +1. **`leakAudit` — draft HTML routes emitted under `dist/`** — Public build must not ship URLs for `visibility: draft` items; audit is the release gate for console + portfolio parity. +2. **`leakAudit` — `SHELLSIDE_*_SENTINEL` strings from draft bodies appearing in dist** — Canary text in draft fixtures; a vacuous audit would give false confidence. +3. **`leakAudit` — configured redaction needles** — Private export paths must not appear in built output. +4. **`publicEntries` / shared listing helpers** — Console, plaintext, and portfolio must apply the same `filterByVisibility` rules; mismatch leaks drafts on one surface only. +5. **`isLoopbackHost` — DEV write API host gate** — Mis-parsing allows non-loopback hosts while `import.meta.env.DEV` is true (see architecture docs on Host header trust). +6. **`parseFrontmatter` — scalar parsing for audit + site tree** — Wrong parsing hides draft metadata or breaks `site-tree` section detection. +7. **Plaintext endpoints (`list-txt`, `section-txt`, `llms-txt`)** — No unit tests; only full Astro builds. Regressions could omit drafts filter or wrong sort order on public text surfaces. +8. **`createDevContentHandler` with custom `hostOk`** — Default loopback is tested; injected `hostOk` bypass paths are not explicitly regression-tested. +9. **`projectCollections` skipping empty slug/body** — Partially covered; edge case of HTML fragments + `compactLinks` in production console mount is untested at site integration level. +10. **Marquee export + `check-public-output` script wiring** — Depends on `leakAudit` but script itself has no test; wrong `routeDirs` or needles config would silently weaken CI. + +Items **1–6** received new unit tests in this pass (see below). **7–10** remain integration or follow-up gaps. + +--- + +## Gap-filling tests added (this change) + +| Area | File | New cases | +| --- | --- | --- | +| Leak audit | `packages/site/src/leak-audit.test.ts` | Clean dist; draft route + sentinel failures; missing dist + redaction needle | +| Frontmatter | `packages/site/src/frontmatter.test.ts` | No fence; quoted scalars; unclosed delimiter | +| Public listings | `packages/site/src/public.test.ts` | `entryId`; `publicEntries` visibility matrix; `byTitle` sort | +| DEV API contract | `packages/dev-api/src/contract.test.ts` | `isLoopbackHost` accept/reject matrix | + +**Verify:** + +```bash +npm run build && npm test +``` + +Last lines (after build): + +``` +# tests 15 +# pass 15 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms 189.373618 +``` + +(`@shellside/site` and `@shellside/dev-api` counts increase; full `npm test` aggregate ends on site’s 15 tests in the workspace chain.) + +--- + +## Suggested follow-ups + +- Thin endpoint tests for plaintext handlers (mock `getCollection`, assert filtering and MIME). +- Explicit test that `hostOk: () => true` still requires `import.meta.env.DEV` in the handler. +- Optional coverage report (`c8` or `node --experimental-test-coverage`) once a threshold is agreed for the monorepo. diff --git a/packages/dev-api/src/contract.test.ts b/packages/dev-api/src/contract.test.ts new file mode 100644 index 0000000..4e5b780 --- /dev/null +++ b/packages/dev-api/src/contract.test.ts @@ -0,0 +1,17 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { isLoopbackHost } from "./contract.js"; + +test("isLoopbackHost accepts localhost and 127.0.0.1 with optional port", () => { + assert.equal(isLoopbackHost("localhost"), true); + assert.equal(isLoopbackHost("localhost:4321"), true); + assert.equal(isLoopbackHost("127.0.0.1"), true); + assert.equal(isLoopbackHost("127.0.0.1:3000"), true); +}); + +test("isLoopbackHost rejects non-loopback hosts", () => { + assert.equal(isLoopbackHost(""), false); + assert.equal(isLoopbackHost("example.com"), false); + assert.equal(isLoopbackHost("[::1]"), false); + assert.equal(isLoopbackHost("10.0.0.1"), false); +}); diff --git a/packages/site/src/frontmatter.test.ts b/packages/site/src/frontmatter.test.ts new file mode 100644 index 0000000..7835e51 --- /dev/null +++ b/packages/site/src/frontmatter.test.ts @@ -0,0 +1,31 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { parseFrontmatter } from "./frontmatter.js"; + +test("parseFrontmatter returns full text when frontmatter is absent", () => { + const body = "no delimiter here"; + assert.deepEqual(parseFrontmatter(body), { data: {}, body }); +}); + +test("parseFrontmatter parses scalars and strips quoted values", () => { + const text = `--- +title: Hello +visibility: draft +status: "quoted" +tag: 'single' +--- +Body line one +`; + const { data, body } = parseFrontmatter(text); + assert.equal(data.title, "Hello"); + assert.equal(data.visibility, "draft"); + assert.equal(data.status, "quoted"); + assert.equal(data.tag, "single"); + assert.equal(body.trim(), "Body line one"); +}); + +test("parseFrontmatter tolerates unclosed delimiter as plain body", () => { + const text = "---\ntitle: Orphan\nno closing fence"; + assert.deepEqual(parseFrontmatter(text).data, {}); + assert.equal(parseFrontmatter(text).body, text); +}); diff --git a/packages/site/src/leak-audit.test.ts b/packages/site/src/leak-audit.test.ts new file mode 100644 index 0000000..0e75004 --- /dev/null +++ b/packages/site/src/leak-audit.test.ts @@ -0,0 +1,76 @@ +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { test } from "node:test"; +import { leakAudit } from "./leak-audit.js"; + +function scaffold(): { contentDir: string; distDir: string } { + const root = mkdtempSync(path.join(tmpdir(), "leak-audit-")); + const contentDir = path.join(root, "content"); + const distDir = path.join(root, "dist"); + mkdirSync(contentDir, { recursive: true }); + mkdirSync(path.join(contentDir, "work")); + mkdirSync(distDir); + writeFileSync( + path.join(contentDir, "work", "public-one.md"), + "---\ntitle: Public Title\nvisibility: published\n---\nvisible prose", + ); + writeFileSync( + path.join(contentDir, "work", "hidden-draft.md"), + "---\ntitle: Hidden Draft\nvisibility: draft\n---\nSHELLSIDE_DRAFT_SENTINEL", + ); + writeFileSync(path.join(distDir, "index.html"), "Public Title\n"); + writeFileSync(path.join(distDir, "list.txt"), "Public Title\n"); + return { contentDir, distDir }; +} + +test("leakAudit passes when public titles appear and drafts stay out of dist", () => { + const { contentDir, distDir } = scaffold(); + const failures = leakAudit({ + contentDir, + distDir, + routeDirs: { work: "projects" }, + plaintextRoute: "list.txt", + }); + assert.deepEqual(failures, []); +}); + +test("leakAudit flags draft routes and sentinels in dist", () => { + const { contentDir, distDir } = scaffold(); + mkdirSync(path.join(distDir, "projects"), { recursive: true }); + writeFileSync(path.join(distDir, "projects", "hidden-draft.html"), "leaked"); + writeFileSync( + path.join(distDir, "trap.html"), + "draft link\nSHELLSIDE_DRAFT_SENTINEL", + ); + const failures = leakAudit({ + contentDir, + distDir, + routeDirs: { work: "projects" }, + plaintextRoute: false, + }); + assert.ok(failures.some((f) => f.startsWith("draft route"))); + assert.ok(failures.some((f) => f.includes("sentinel"))); +}); + +test("leakAudit fails on missing dist and forbidden redaction needles", () => { + const { contentDir } = scaffold(); + const missing = leakAudit({ + contentDir, + distDir: path.join(path.dirname(contentDir), "no-dist"), + routeDirs: { work: "projects" }, + }); + assert.equal(missing[0], `missing ${path.join(path.dirname(contentDir), "no-dist")} — build first`); + + const { distDir } = scaffold(); + writeFileSync(path.join(distDir, "leak.txt"), "enigma-archive/foo"); + const needleFailures = leakAudit({ + contentDir, + distDir, + routeDirs: { work: "projects" }, + needles: ["enigma-archive/"], + plaintextRoute: false, + }); + assert.ok(needleFailures.some((f) => f.startsWith("redaction needle"))); +}); diff --git a/packages/site/src/public.test.ts b/packages/site/src/public.test.ts new file mode 100644 index 0000000..4c78e08 --- /dev/null +++ b/packages/site/src/public.test.ts @@ -0,0 +1,34 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { byTitle, entryId, publicEntries } from "./public.js"; + +test("entryId strips markdown and yaml extensions", () => { + assert.equal(entryId({ id: "kettle.md" }), "kettle"); + assert.equal(entryId({ id: "notes.mdx" }), "notes"); + assert.equal(entryId({ id: "list.yaml" }), "list"); +}); + +test("publicEntries drops drafts and legacy hidden statuses", () => { + const items = [ + { id: "a", data: { visibility: "published", title: "A" } }, + { id: "b", data: { visibility: "draft", title: "B" } }, + { id: "c", data: { status: "draft", title: "C" } }, + { id: "d", data: { status: "forthcoming", title: "D" } }, + { id: "e", data: { status: "unpublished", title: "E" } }, + { id: "f", data: { visibility: "published", status: "draft", title: "F" } }, + ]; + const out = publicEntries(items); + assert.deepEqual( + out.map((i) => i.id).sort(), + ["a", "d", "f"], + ); +}); + +test("byTitle sorts on title then id", () => { + const items = [ + { id: "z", data: { title: "Zebra" } }, + { id: "a", data: { title: "Alpha" } }, + { id: "m", data: {} }, + ]; + assert.deepEqual(byTitle(items).map((i) => i.id), ["a", "m", "z"]); +});