|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import test from "node:test"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { loadTheme } from "../scripts/injector.mjs"; |
| 8 | + |
| 9 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const windowsRoot = path.resolve(here, ".."); |
| 11 | +const sourceImage = path.join(windowsRoot, "assets", "dream-reference.jpg"); |
| 12 | + |
| 13 | +async function makeTheme(root, schemaVersion, includeSchema = true) { |
| 14 | + await fs.copyFile(sourceImage, path.join(root, "background.jpg")); |
| 15 | + const theme = { |
| 16 | + id: "schema-contract-fixture", |
| 17 | + name: "Schema contract fixture", |
| 18 | + image: "background.jpg", |
| 19 | + }; |
| 20 | + if (includeSchema) theme.schemaVersion = schemaVersion; |
| 21 | + await fs.writeFile(path.join(root, "theme.json"), `${JSON.stringify(theme, null, 2)}\n`); |
| 22 | +} |
| 23 | + |
| 24 | +async function withTheme(schemaVersion, includeSchema, callback) { |
| 25 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "dreamskin-schema-contract.")); |
| 26 | + try { |
| 27 | + await makeTheme(root, schemaVersion, includeSchema); |
| 28 | + await callback(root); |
| 29 | + } finally { |
| 30 | + await fs.rm(root, { recursive: true, force: true }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +test("Windows runtime accepts and preserves theme schemaVersion 1", async () => { |
| 35 | + await withTheme(1, true, async (root) => { |
| 36 | + const loaded = await loadTheme(root); |
| 37 | + assert.equal(loaded.theme.schemaVersion, 1); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +test("Windows runtime rejects missing and future theme schema versions", async () => { |
| 42 | + await withTheme(undefined, false, async (root) => { |
| 43 | + await assert.rejects(loadTheme(root), /must use schemaVersion 1/); |
| 44 | + }); |
| 45 | + await withTheme(2, true, async (root) => { |
| 46 | + await assert.rejects(loadTheme(root), /must use schemaVersion 1/); |
| 47 | + }); |
| 48 | + await withTheme("1", true, async (root) => { |
| 49 | + await assert.rejects(loadTheme(root), /must use schemaVersion 1/); |
| 50 | + }); |
| 51 | +}); |
0 commit comments