|
| 1 | +// formFile must NOT emit a write form for entities that have no instantiable |
| 2 | +// write representation: |
| 3 | +// - abstract (`abstract: true`) → shape-via-inheritance only |
| 4 | +// - projection (read-only view) → instantiable for read, never for write |
| 5 | +// |
| 6 | +// A concrete writable entity still gets its form. Genericized `acme::*`. |
| 7 | + |
| 8 | +import { describe, test, expect } from "bun:test"; |
| 9 | +import { formFile } from "../src/form-file.js"; |
| 10 | +import { makeRenderContext, buildPkMap, buildRelationMap } from "@metaobjectsdev/codegen-ts"; |
| 11 | +import type { GenContext } from "@metaobjectsdev/codegen-ts"; |
| 12 | +import { MetaDataLoader, InMemoryStringSource } from "@metaobjectsdev/metadata"; |
| 13 | + |
| 14 | +async function loadFixture() { |
| 15 | + const json = JSON.stringify({ |
| 16 | + "metadata.root": { |
| 17 | + package: "acme", |
| 18 | + children: [ |
| 19 | + // Abstract base — no instance, no write form. |
| 20 | + { |
| 21 | + "object.entity": { |
| 22 | + name: "PartyBase", |
| 23 | + abstract: true, |
| 24 | + children: [ |
| 25 | + { "field.int": { name: "id" } }, |
| 26 | + { "field.string": { name: "displayName" } }, |
| 27 | + { "identity.primary": { "@fields": "id" } }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + }, |
| 31 | + // Concrete subclass — full write form. |
| 32 | + { |
| 33 | + "object.entity": { |
| 34 | + name: "Guest", |
| 35 | + extends: "PartyBase", |
| 36 | + children: [ |
| 37 | + { "source.rdb": { "@table": "guests" } }, |
| 38 | + { "field.string": { name: "email" } }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + }, |
| 42 | + // Base table for the projection to extend. |
| 43 | + { |
| 44 | + "object.entity": { |
| 45 | + name: "Host", |
| 46 | + children: [ |
| 47 | + { "source.rdb": { "@table": "hosts" } }, |
| 48 | + { "field.int": { name: "id" } }, |
| 49 | + { "field.string": { name: "name" } }, |
| 50 | + { "identity.primary": { "@fields": "id" } }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + }, |
| 54 | + // Read-only projection — read model only, no write form. |
| 55 | + { |
| 56 | + "object.entity": { |
| 57 | + name: "HostSummary", |
| 58 | + extends: "Host", |
| 59 | + children: [ |
| 60 | + { "source.rdb": { "@kind": "view", "@table": "v_host_summary" } }, |
| 61 | + { "identity.primary": { "@fields": "id" } }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + }); |
| 68 | + const { root, errors } = await new MetaDataLoader().load([new InMemoryStringSource(json)]); |
| 69 | + if (errors.length > 0) throw new Error(errors.map((e) => e.message).join("\n")); |
| 70 | + return root; |
| 71 | +} |
| 72 | + |
| 73 | +async function ctxFor() { |
| 74 | + const root = await loadFixture(); |
| 75 | + const renderContext = makeRenderContext({ |
| 76 | + dialect: "sqlite", loadedRoot: root, outDir: "/tmp", |
| 77 | + dbImport: "../db", extStyle: "none", |
| 78 | + pkMap: buildPkMap(root), relationMap: buildRelationMap(root), |
| 79 | + }); |
| 80 | + const gen = formFile(); |
| 81 | + const ctx: GenContext = { |
| 82 | + entities: root.objects(), |
| 83 | + loadedRoot: root, |
| 84 | + matches: (e) => gen.filter?.(e) ?? true, |
| 85 | + config: { outDir: "/tmp", extStyle: "none", dbImport: "../db", dialect: "sqlite" }, |
| 86 | + renderContext, |
| 87 | + warn: () => {}, |
| 88 | + }; |
| 89 | + return { gen, ctx }; |
| 90 | +} |
| 91 | + |
| 92 | +describe("formFile — abstract + projection skip", () => { |
| 93 | + test("does NOT emit a form for the abstract base", async () => { |
| 94 | + const { gen, ctx } = await ctxFor(); |
| 95 | + const files = await gen.generate(ctx); |
| 96 | + expect(files.find((f) => f.path === "PartyBase.form.tsx")).toBeUndefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + test("does NOT emit a form for the read-only projection", async () => { |
| 100 | + const { gen, ctx } = await ctxFor(); |
| 101 | + const files = await gen.generate(ctx); |
| 102 | + expect(files.find((f) => f.path === "HostSummary.form.tsx")).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + test("DOES emit a form for the concrete writable subclass", async () => { |
| 106 | + const { gen, ctx } = await ctxFor(); |
| 107 | + const files = await gen.generate(ctx); |
| 108 | + expect(files.find((f) => f.path === "Guest.form.tsx")).toBeDefined(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments