|
| 1 | +import type { Kysely } from "kysely"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import { exportSeed } from "../../../src/cli/commands/export-seed.js"; |
| 5 | +import { ContentRepository } from "../../../src/database/repositories/content.js"; |
| 6 | +import type { Database } from "../../../src/database/types.js"; |
| 7 | +import { setI18nConfig } from "../../../src/i18n/config.js"; |
| 8 | +import { SchemaRegistry } from "../../../src/schema/registry.js"; |
| 9 | +import { setupTestDatabase, teardownTestDatabase } from "../../utils/test-db.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Regression for #1329: `emdash export-seed --with-content all` exported |
| 13 | + * nothing because the literal string "all" was treated as a collection name. |
| 14 | + * Only the bare flag and `--with-content=true` were honoured as the |
| 15 | + * "include every collection" sentinel, contradicting the args help text: |
| 16 | + * |
| 17 | + * "with-content": { |
| 18 | + * description: "Include content (all or comma-separated collection names)", |
| 19 | + * } |
| 20 | + */ |
| 21 | +describe("exportSeed: --with-content sentinel handling", () => { |
| 22 | + let db: Kysely<Database>; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + setI18nConfig(null); |
| 26 | + db = await setupTestDatabase(); |
| 27 | + |
| 28 | + const registry = new SchemaRegistry(db); |
| 29 | + await registry.createCollection({ slug: "posts", label: "Posts" }); |
| 30 | + await registry.createField("posts", { slug: "title", label: "Title", type: "string" }); |
| 31 | + await registry.createCollection({ slug: "pages", label: "Pages" }); |
| 32 | + await registry.createField("pages", { slug: "title", label: "Title", type: "string" }); |
| 33 | + |
| 34 | + const contentRepo = new ContentRepository(db); |
| 35 | + await contentRepo.create({ |
| 36 | + type: "posts", |
| 37 | + slug: "hello-post", |
| 38 | + status: "published", |
| 39 | + data: { title: "Hello Post" }, |
| 40 | + }); |
| 41 | + await contentRepo.create({ |
| 42 | + type: "pages", |
| 43 | + slug: "hello-page", |
| 44 | + status: "published", |
| 45 | + data: { title: "Hello Page" }, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await teardownTestDatabase(db); |
| 51 | + setI18nConfig(null); |
| 52 | + }); |
| 53 | + |
| 54 | + it("treats `all` as a synonym for include-every-collection", async () => { |
| 55 | + const seed = await exportSeed(db, "all"); |
| 56 | + |
| 57 | + expect(seed.content).toBeDefined(); |
| 58 | + expect(Object.keys(seed.content ?? {}).toSorted()).toEqual(["pages", "posts"]); |
| 59 | + expect(seed.content?.posts?.[0]?.slug).toBe("hello-post"); |
| 60 | + expect(seed.content?.pages?.[0]?.slug).toBe("hello-page"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("matches the bare flag's behaviour (empty string)", async () => { |
| 64 | + const seedAll = await exportSeed(db, "all"); |
| 65 | + const seedBare = await exportSeed(db, ""); |
| 66 | + |
| 67 | + expect(Object.keys(seedAll.content ?? {}).toSorted()).toEqual( |
| 68 | + Object.keys(seedBare.content ?? {}).toSorted(), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("matches the explicit `true` sentinel's behaviour", async () => { |
| 73 | + const seedAll = await exportSeed(db, "all"); |
| 74 | + const seedTrue = await exportSeed(db, "true"); |
| 75 | + |
| 76 | + expect(Object.keys(seedAll.content ?? {}).toSorted()).toEqual( |
| 77 | + Object.keys(seedTrue.content ?? {}).toSorted(), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("still treats a comma-separated list as a collection-name filter", async () => { |
| 82 | + const seed = await exportSeed(db, "posts"); |
| 83 | + |
| 84 | + expect(seed.content).toBeDefined(); |
| 85 | + expect(Object.keys(seed.content ?? {})).toEqual(["posts"]); |
| 86 | + expect(seed.content?.posts?.[0]?.slug).toBe("hello-post"); |
| 87 | + }); |
| 88 | +}); |
0 commit comments