forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectConfig.test.ts
More file actions
126 lines (116 loc) · 3.96 KB
/
Copy pathprojectConfig.test.ts
File metadata and controls
126 lines (116 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { describe, expect, it } from "vitest";
import { mkdtempSync, rmSync, writeFileSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
DEFAULT_PROJECT_CONFIG,
loadProjectConfig,
normalizeConfig,
projectConfigPath,
readProjectConfig,
writeProjectConfig,
PROJECT_CONFIG_FILENAME,
} from "./projectConfig.js";
function tmp(): string {
return mkdtempSync(join(tmpdir(), "hf-cfg-test-"));
}
describe("projectConfig", () => {
describe("write + read round-trip", () => {
it("writes the default config and reads it back", () => {
const dir = tmp();
try {
writeProjectConfig(dir);
const read = readProjectConfig(dir);
expect(read).toEqual(DEFAULT_PROJECT_CONFIG);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("writes a custom config and reads it back verbatim", () => {
const dir = tmp();
try {
const custom = {
$schema: DEFAULT_PROJECT_CONFIG.$schema,
registry: "https://example.com/my-registry",
paths: { blocks: "src/blocks", components: "src/fx", assets: "media" },
};
writeProjectConfig(dir, custom);
const read = readProjectConfig(dir);
expect(read).toEqual(custom);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("normalizeConfig", () => {
it("fills in defaults for missing fields", () => {
const result = normalizeConfig({ registry: "https://alt.example.com" });
expect(result.registry).toBe("https://alt.example.com");
expect(result.paths).toEqual(DEFAULT_PROJECT_CONFIG.paths);
expect(result.$schema).toBe(DEFAULT_PROJECT_CONFIG.$schema);
});
it("preserves partial paths objects", () => {
const result = normalizeConfig({ paths: { blocks: "x" } as unknown as never });
expect(result.paths.blocks).toBe("x");
expect(result.paths.components).toBe(DEFAULT_PROJECT_CONFIG.paths.components);
expect(result.paths.assets).toBe(DEFAULT_PROJECT_CONFIG.paths.assets);
});
});
describe("readProjectConfig", () => {
it("returns undefined when the file is absent", () => {
const dir = tmp();
try {
expect(readProjectConfig(dir)).toBeUndefined();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("returns undefined when the file is corrupt", () => {
const dir = tmp();
try {
writeFileSync(projectConfigPath(dir), "{ not valid json", "utf-8");
expect(readProjectConfig(dir)).toBeUndefined();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("normalizes a partial on-disk config", () => {
const dir = tmp();
try {
writeFileSync(
projectConfigPath(dir),
JSON.stringify({ registry: "https://only-this.example.com" }),
"utf-8",
);
const read = readProjectConfig(dir);
expect(read?.registry).toBe("https://only-this.example.com");
expect(read?.paths).toEqual(DEFAULT_PROJECT_CONFIG.paths);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("loadProjectConfig", () => {
it("returns defaults when no config file exists", () => {
const dir = tmp();
try {
expect(loadProjectConfig(dir)).toEqual(DEFAULT_PROJECT_CONFIG);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("writeProjectConfig", () => {
it("writes to hyperframes.json at the project root", () => {
const dir = tmp();
try {
writeProjectConfig(dir);
const path = join(dir, PROJECT_CONFIG_FILENAME);
const parsed = JSON.parse(readFileSync(path, "utf-8"));
expect(parsed.registry).toBe(DEFAULT_PROJECT_CONFIG.registry);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
});