| 1 | import { describe, expect, it, spyOn } from "bun:test"; |
| 2 | import * as fs from "node:fs/promises"; |
| 3 | import { DEFAULT_PUBLISHER_CONFIG, loadConfig } from "../src/lib/config"; |
| 4 | |
| 5 | const REQUIRED_FIELDS = { |
| 6 | siteUrl: "https://example.com", |
| 7 | contentDir: "./content", |
| 8 | publicationUri: "at://did:plc:abc123/site.standard.publication/main", |
| 9 | }; |
| 10 | |
| 11 | describe("loadConfig", () => { |
| 12 | it("applies DEFAULT_PUBLISHER_CONFIG when optional fields are absent", async () => { |
| 13 | spyOn(fs, "readFile").mockResolvedValue( |
| 14 | JSON.stringify(REQUIRED_FIELDS) as unknown as Buffer, |
| 15 | ); |
| 16 | |
| 17 | const config = await loadConfig("/fake/sequoia.json"); |
| 18 | |
| 19 | expect(config.publicDir).toBe(DEFAULT_PUBLISHER_CONFIG.publicDir); |
| 20 | expect(config.outputDir).toBe(DEFAULT_PUBLISHER_CONFIG.outputDir); |
| 21 | expect(config.pathPrefix).toBe(DEFAULT_PUBLISHER_CONFIG.pathPrefix); |
| 22 | expect(config.publishContent).toBe(DEFAULT_PUBLISHER_CONFIG.publishContent); |
| 23 | expect(config.removeIndexFromSlug).toBe( |
| 24 | DEFAULT_PUBLISHER_CONFIG.removeIndexFromSlug, |
| 25 | ); |
| 26 | expect(config.stripDatePrefix).toBe( |
| 27 | DEFAULT_PUBLISHER_CONFIG.stripDatePrefix, |
| 28 | ); |
| 29 | expect(config.autoSync).toBe(DEFAULT_PUBLISHER_CONFIG.autoSync); |
| 30 | }); |
| 31 | |
| 32 | it("uses explicit values when all defaultable fields are set", async () => { |
| 33 | const custom = { |
| 34 | ...REQUIRED_FIELDS, |
| 35 | publicDir: "./static", |
| 36 | outputDir: "./build", |
| 37 | pathPrefix: "/articles", |
| 38 | publishContent: false, |
| 39 | removeIndexFromSlug: true, |
| 40 | stripDatePrefix: true, |
| 41 | autoSync: false, |
| 42 | }; |
| 43 | |
| 44 | spyOn(fs, "readFile").mockResolvedValue( |
| 45 | JSON.stringify(custom) as unknown as Buffer, |
| 46 | ); |
| 47 | |
| 48 | const config = await loadConfig("/fake/sequoia.json"); |
| 49 | |
| 50 | expect(config.publicDir).toBe("./static"); |
| 51 | expect(config.outputDir).toBe("./build"); |
| 52 | expect(config.pathPrefix).toBe("/articles"); |
| 53 | expect(config.publishContent).toBe(false); |
| 54 | expect(config.removeIndexFromSlug).toBe(true); |
| 55 | expect(config.stripDatePrefix).toBe(true); |
| 56 | expect(config.autoSync).toBe(false); |
| 57 | }); |
| 58 | }); |