Files
snapcad-studio/tests/unit/sceneSchema.test.ts
T
BloodSteynandClaude Opus 4.8 dee2765538 Add AI scene bridge (V1): MCP -> /api/scene -> editor
Additive fork feature letting an MCP server (driven by Claude) push parametric
WorkplaneShape[] scenes into the running SketchForge editor. The Node side never
writes browser storage; it stages a command file that an in-page poller applies
through the editor's existing updateProjectShapes() save path.

- Phase 1: apps/web/src/app/api/scene/route.ts — stages/serves commands behind the
  existing same-origin guard, with its own monotonic stagingRevision (separate from
  the app's project revision).
- Phase 2: apps/web/src/lib/sceneBridge.ts (SceneTransport seam + polling transport)
  and a useSceneBridge poller hook in page.tsx (gated on ?bridge=1), which creates
  unknown projects and hydrates via updateProjectShapes; defers while dragging.
- Phase 3: apps/mcp/ MCP server — set_scene/get_scene/add_shapes/list_kinds. zod
  validates the ShapeKind union enum + base numerics, then reuses the app's own
  sceneShape/canonicalizeShape for defaults/canonicalisation (no hand-authored ranges).
- Phase 4: docs/scene-bridge/README.md plus vitest cases for the route handler and
  the schema validator.

Staged scenes live under .codex/pending-scenes/ (gitignored). MCP registered via
.mcp.json. npm run test: 29 passing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-26 13:34:44 +02:00

58 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { SHAPE_KINDS, shapesArraySchema, toWorkplaneShapes, type ShapeInput } from "../../apps/mcp/src/shapes";
describe("MCP scene schema + normalisation", () => {
it("exposes all 18 shape kinds", () => {
expect(SHAPE_KINDS).toHaveLength(18);
expect(SHAPE_KINDS).toContain("box");
expect(SHAPE_KINDS).toContain("mesh");
});
it("accepts a minimal valid shape and fills defaults via the app's defaulter", () => {
const parsed = shapesArraySchema.parse([{ kind: "box", width: 40, depth: 40, height: 10 }]);
const [shape] = toWorkplaneShapes(parsed as ShapeInput[]);
expect(shape.kind).toBe("box");
expect(typeof shape.color).toBe("string"); // auto-filled
expect(shape.color.length).toBeGreaterThan(0);
expect(typeof shape.size).toBe("number"); // derived by sceneShape
expect(typeof shape.id).toBe("string");
expect(shape.name).toBeTruthy();
});
it("preserves a cutter (hole: true) and grouped parts", () => {
const [shape] = toWorkplaneShapes(
shapesArraySchema.parse([
{
kind: "box",
width: 40,
depth: 40,
height: 10,
groupedShapes: [{ kind: "cylinder", hole: true, width: 8, depth: 8, height: 12 }],
},
]) as ShapeInput[],
);
expect(shape.groupedShapes).toHaveLength(1);
expect(shape.groupedShapes?.[0]?.hole).toBe(true);
});
it("rejects an invalid ShapeKind", () => {
const result = shapesArraySchema.safeParse([{ kind: "banana", width: 1, depth: 1, height: 1 }]);
expect(result.success).toBe(false);
if (!result.success) {
expect(JSON.stringify(result.error.issues)).toContain("invalid_enum_value");
}
});
it("rejects a malformed numeric field", () => {
const result = shapesArraySchema.safeParse([{ kind: "box", width: "wide", depth: 10, height: 10 }]);
expect(result.success).toBe(false);
});
it("accepts an extreme shape and canonicalises rotation instead of rejecting", () => {
const parsed = shapesArraySchema.parse([{ kind: "box", width: 100000, depth: 100000, height: 100000, rotation: 725 }]);
const [shape] = toWorkplaneShapes(parsed as ShapeInput[]);
expect(shape.rotation).toBe(5); // 725 mod 360 via canonicalizeShape, not rejected
expect(shape.width).toBe(100000); // magnitude not range-rejected
});
});