Files
editor/packages/mcp/src/templates/templates.test.ts
T
Adrian PerezandClaude Opus 4.7 e8d0b13ff5 feat(mcp,editor): Option A+B storage + 10 agent deliverables (Phase 7)
Ships the combined filesystem/Supabase storage adapter + MCP scene
lifecycle tools + Next.js API routes + editor /scene/[id] route, so
an MCP save is directly openable at /scene/<id> without any
injection hack. End-to-end verified: 10/10 e2e steps pass.

Storage (A1/A2/A3):
- SceneStore interface + error classes + slug helpers
- FilesystemSceneStore at $PASCAL_DATA_DIR (defaults XDG/~/.pascal)
  with atomic writes, .index sidecar, optimistic locking
- SupabaseSceneStore with scenes + scene_revisions tables, RLS
  migration SQL, mock-backed unit tests
- createSceneStore(env) auto-selects based on SUPABASE_URL +
  SUPABASE_SERVICE_ROLE_KEY

MCP tools (A4, A8, A9, A10):
- save_scene / load_scene / list_scenes / delete_scene / rename_scene
- list_templates / create_from_template (3 seed templates:
  empty-studio, two-bedroom, garden-house)
- generate_variants (7 mutation kinds, seeded RNG, save=true|false)
- photo_to_scene (vision sampling → scene graph → save)

Editor (A5, A6):
- /api/scenes + /api/scenes/[id] with RFC 7232 If-Match locking
- /scene/[id] and /scenes route pages with save button, SceneLoader
- Removed the window.__pascalScene dev injection hack

Security + UX edges (A7, A8):
- AssetUrl Zod validator: asset:// blob: data:image/ /path https:
  (http://localhost for dev) + PASCAL_ALLOWED_ASSET_ORIGINS env
  allowlist. Hardens scan.url, guide.url, item.asset.src,
  material.texture.url, MaterialMaps.*Map
- Auto-frame camera on empty→non-empty scene transition
  (camera-controls:fit-scene emitter event)

Shared utilities:
- rehydrateSiteChildren() extracted to packages/mcp/src/lib/ and
  used by both create-from-template and generate-variants to work
  around the SiteNode.children-as-objects vs. ids inconsistency
  (CROSS_CUTTING §2)
- Storage + MCP subpath exports added to packages/mcp/package.json
  (CROSS_CUTTING §4)

Tests: 293 pass / 0 fail across 40 files (was 142 pre-Phase-7).
Biome: clean.

Phase-7 e2e script at packages/mcp/test-reports/phase7-e2e.ts:
MCP HTTP + editor Next.js both point at $PASCAL_DATA_DIR =
/tmp/pascal-e2e, save_scene from MCP, GET /api/scenes/<id> from
editor server, /scenes list page renders all saved scenes, scene
page renders SceneLoader, delete_scene works.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 19:29:28 +02:00

86 lines
3.0 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { AnyNode } from '@pascal-app/core/schema'
import { TEMPLATES, type TemplateId } from './index'
describe('scene templates', () => {
const ids: TemplateId[] = Object.keys(TEMPLATES) as TemplateId[]
for (const id of ids) {
const entry = TEMPLATES[id]
test(`${id} has required metadata`, () => {
expect(entry.id).toBe(id)
expect(typeof entry.name).toBe('string')
expect(entry.name.length).toBeGreaterThan(0)
expect(typeof entry.description).toBe('string')
expect(entry.description.length).toBeGreaterThan(0)
})
test(`${id} template nodes all pass AnyNode.safeParse`, () => {
const { nodes, rootNodeIds } = entry.template
expect(rootNodeIds.length).toBeGreaterThan(0)
expect(Object.keys(nodes).length).toBeGreaterThan(0)
for (const [nodeId, node] of Object.entries(nodes)) {
const res = AnyNode.safeParse(node)
if (!res.success) {
// Surface the path/message of the first issue for debuggability.
const first = res.error.issues[0]
throw new Error(
`template ${id} node ${nodeId} failed AnyNode.safeParse at ${first?.path.join('.')}: ${first?.message}`,
)
}
expect(res.success).toBe(true)
}
})
test(`${id} root ids resolve and parent links point to existing nodes`, () => {
const { nodes, rootNodeIds } = entry.template
for (const rid of rootNodeIds) {
expect(nodes[rid]).toBeDefined()
}
for (const node of Object.values(nodes)) {
if (node.parentId && !(node.parentId in nodes)) {
throw new Error(
`template ${id} node ${node.id} has parentId ${node.parentId} which does not exist`,
)
}
}
})
}
test('empty-studio has 4 walls, 1 zone, 1 door, 1 window', () => {
const { nodes } = TEMPLATES['empty-studio'].template
const byType = groupByType(nodes)
expect(byType.wall ?? 0).toBe(4)
expect(byType.zone ?? 0).toBe(1)
expect(byType.door ?? 0).toBe(1)
expect(byType.window ?? 0).toBe(1)
})
test('two-bedroom has 9 walls, 4 zones, 4 doors, 5 windows', () => {
const { nodes } = TEMPLATES['two-bedroom'].template
const byType = groupByType(nodes)
expect(byType.wall ?? 0).toBe(9)
expect(byType.zone ?? 0).toBe(4)
expect(byType.door ?? 0).toBe(4)
expect(byType.window ?? 0).toBe(5)
})
test('garden-house has a fenced garden zone', () => {
const { nodes } = TEMPLATES['garden-house'].template
const byType = groupByType(nodes)
expect(byType.zone ?? 0).toBeGreaterThanOrEqual(2)
expect(byType.fence ?? 0).toBeGreaterThanOrEqual(3)
expect(byType.wall ?? 0).toBeGreaterThanOrEqual(4)
})
})
function groupByType(nodes: Record<string, { type: string }>): Record<string, number> {
const out: Record<string, number> = {}
for (const node of Object.values(nodes)) {
out[node.type] = (out[node.type] ?? 0) + 1
}
return out
}