-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathinit.test.ts
More file actions
313 lines (280 loc) · 11.1 KB
/
Copy pathinit.test.ts
File metadata and controls
313 lines (280 loc) · 11.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { describe, expect, it } from "vitest";
import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { applyResolutionPreset, injectTailwindBrowserScript } from "./init.js";
const cliEntry = resolve(fileURLToPath(import.meta.url), "..", "..", "cli.ts");
const tailwindScript =
'<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.2.4/dist/index.global.js" integrity="sha384-v5YF9xS+gLRWdvrQ0u/WRbCkjSIH0NjHIPe8tBL1ZRrmI7PiSH6LLdzs0aAIMCuh" crossorigin="anonymous"></script>';
// Spawns `bun` directly because the CLI entry is a .ts file that needs a
// TypeScript-aware runtime. vitest runs under node, so `process.execPath`
// would be node and couldn't load the entry. This repo hard-depends on bun
// (package.json scripts), so assuming it's on PATH is safe.
function runInit(args: string[]): { status: number; stdout: string; stderr: string } {
const res = spawnSync("bun", ["run", cliEntry, "init", ...args], {
encoding: "utf-8",
timeout: 30_000,
});
return {
status: res.status ?? -1,
stdout: res.stdout,
stderr: res.stderr,
};
}
describe("hyperframes init flag rename", () => {
it("--example blank scaffolds a bundled project with npm scripts", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([target, "--example", "blank", "--non-interactive", "--skip-skills"]);
expect(res.status).toBe(0);
expect(existsSync(join(target, "index.html"))).toBe(true);
expect(res.stdout).toContain("npm run dev");
expect(res.stdout).toContain("npm run check");
expect(res.stdout).toContain("npm run render");
const pkg = JSON.parse(readFileSync(join(target, "package.json"), "utf-8")) as {
private?: boolean;
type?: string;
scripts?: Record<string, string>;
};
expect(pkg.private).toBe(true);
expect(pkg.type).toBe("module");
expect(pkg.scripts).toMatchObject({
dev: "npx --yes hyperframes preview",
check:
"npx --yes hyperframes lint && npx --yes hyperframes validate && npx --yes hyperframes inspect",
render: "npx --yes hyperframes render",
publish: "npx --yes hyperframes publish",
});
expect(Object.keys(pkg.scripts ?? {}).sort()).toEqual(["check", "dev", "publish", "render"]);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("--tailwind enables Tailwind utilities in scaffolded HTML", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([
target,
"--example",
"blank",
"--tailwind",
"--non-interactive",
"--skip-skills",
]);
expect(res.status).toBe(0);
const html = readFileSync(join(target, "index.html"), "utf-8");
expect(html).toContain(tailwindScript);
expect(html).toContain("window.__tailwindReady");
const pkg = JSON.parse(readFileSync(join(target, "package.json"), "utf-8")) as {
scripts?: Record<string, string>;
};
expect(pkg.scripts).toMatchObject({
dev: "npx --yes hyperframes preview",
check:
"npx --yes hyperframes lint && npx --yes hyperframes validate && npx --yes hyperframes inspect",
render: "npx --yes hyperframes render",
publish: "npx --yes hyperframes publish",
});
expect(Object.keys(pkg.scripts ?? {}).sort()).toEqual(["check", "dev", "publish", "render"]);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("inserts Tailwind before uppercase closing head tags", () => {
const html = [
"<!doctype html>",
"<html>",
"<head>",
' <SCRIPT src="./runtime.global.js"></SCRIPT>',
"</HEAD>",
"</html>",
].join("\n");
const injected = injectTailwindBrowserScript(html);
expect(injected.indexOf(' <SCRIPT src="./runtime.global.js"></SCRIPT>')).toBeLessThan(
injected.indexOf(tailwindScript),
);
expect(injected.indexOf(tailwindScript)).toBeLessThan(injected.indexOf("</HEAD>"));
});
it("inserts Tailwind into single-line HTML heads", () => {
const html = "<!doctype html><html><head><title>x</title></head><body></body></html>";
expect(injectTailwindBrowserScript(html)).toContain(`${tailwindScript}\n</head>`);
});
it("does not duplicate Tailwind support when it is already present", () => {
const html = ["<!doctype html>", "<html>", "<head>", tailwindScript, "</head>", "</html>"].join(
"\n",
);
expect(injectTailwindBrowserScript(html)).toBe(html);
});
it("keeps the readiness shim free of render-loop APIs", () => {
const html = "<!doctype html><html><head></head><body></body></html>";
const injected = injectTailwindBrowserScript(html);
expect(injected).not.toContain("Date.now");
expect(injected).not.toContain("requestAnimationFrame");
expect(injected).not.toContain("setTimeout");
});
it("--template prints a rename hint and exits non-zero", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([target, "--template", "blank", "--non-interactive", "--skip-skills"]);
expect(res.status).toBe(1);
expect(res.stderr).toContain("--template flag was renamed to --example");
expect(res.stderr).toContain(`--example "blank"`);
expect(existsSync(target)).toBe(false);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("applyResolutionPreset", () => {
function withFixture(fn: (dir: string) => void): void {
const dir = mkdtempSync(join(tmpdir(), "hf-resolution-test-"));
try {
mkdirSync(dir, { recursive: true });
fn(dir);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
const sampleHtml = [
"<!doctype html>",
'<html lang="en">',
" <head>",
' <meta name="viewport" content="width=1920, height=1080" />',
" <style>",
" html, body { margin: 0; width: 1920px; height: 1080px; overflow: hidden; }",
" </style>",
" </head>",
" <body>",
' <div id="root" data-composition-id="main" data-width="1920" data-height="1080">',
" </div>",
" </body>",
"</html>",
].join("\n");
it("rewrites every dimension fingerprint for landscape-4k", () => {
withFixture((dir) => {
const file = join(dir, "index.html");
writeFileSync(file, sampleHtml, "utf-8");
applyResolutionPreset(dir, "landscape-4k");
const out = readFileSync(file, "utf-8");
expect(out).toContain('data-resolution="landscape-4k"');
expect(out).toContain('data-width="3840"');
expect(out).toContain('data-height="2160"');
expect(out).toContain("width: 3840px");
expect(out).toContain("height: 2160px");
expect(out).toContain('content="width=3840, height=2160"');
expect(out).not.toContain("1920");
expect(out).not.toContain("1080");
});
});
it("swaps to portrait dimensions for portrait-4k", () => {
withFixture((dir) => {
const file = join(dir, "index.html");
writeFileSync(file, sampleHtml, "utf-8");
applyResolutionPreset(dir, "portrait-4k");
const out = readFileSync(file, "utf-8");
expect(out).toContain('data-width="2160"');
expect(out).toContain('data-height="3840"');
expect(out).toContain('data-resolution="portrait-4k"');
});
});
it("scaffolds a 4k project end-to-end via --resolution 4k", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([
target,
"--example",
"blank",
"--resolution",
"4k",
"--non-interactive",
"--skip-skills",
]);
expect(res.status).toBe(0);
const html = readFileSync(join(target, "index.html"), "utf-8");
expect(html).toContain('data-width="3840"');
expect(html).toContain('data-height="2160"');
expect(html).toContain('data-resolution="landscape-4k"');
expect(html).toContain("width: 3840px");
expect(html).toContain("height: 2160px");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("rejects an unknown --resolution value", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([
target,
"--example",
"blank",
"--resolution",
"8k",
"--non-interactive",
"--skip-skills",
]);
expect(res.status).toBe(1);
expect(res.stderr).toContain("Invalid --resolution");
expect(existsSync(target)).toBe(false);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("rewrites height-before-width inline CSS", () => {
withFixture((dir) => {
const file = join(dir, "index.html");
// Reversed property order — same as the parser's stageMatchReverse path.
const reversedOrderHtml = sampleHtml.replace(
"html, body { margin: 0; width: 1920px; height: 1080px; overflow: hidden; }",
"html, body { margin: 0; height: 1080px; width: 1920px; overflow: hidden; }",
);
writeFileSync(file, reversedOrderHtml, "utf-8");
applyResolutionPreset(dir, "landscape-4k");
const out = readFileSync(file, "utf-8");
expect(out).toContain("height: 2160px");
expect(out).toContain("width: 3840px");
expect(out).not.toContain("1080px");
expect(out).not.toContain("1920px");
});
});
it("is a no-op on a file with no dimension fingerprint (does not error)", () => {
withFixture((dir) => {
const file = join(dir, "fragment.html");
// No data-width/height, no html/body block, no viewport — just markup.
const minimal = "<!doctype html><html><head></head><body><p>hi</p></body></html>";
writeFileSync(file, minimal, "utf-8");
expect(() => applyResolutionPreset(dir, "landscape-4k")).not.toThrow();
const out = readFileSync(file, "utf-8");
// The htmlOpenRe path adds `data-resolution="landscape-4k"` because
// the <html> tag is present. That's correct: an explicit signal of
// intended resolution survives even when no dim fields exist.
expect(out).toContain('data-resolution="landscape-4k"');
});
});
it("accepts uppercase --resolution value (4K)", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-init-test-"));
const target = join(dir, "proj");
try {
const res = runInit([
target,
"--example",
"blank",
"--resolution",
"4K",
"--non-interactive",
"--skip-skills",
]);
expect(res.status).toBe(0);
const html = readFileSync(join(target, "index.html"), "utf-8");
expect(html).toContain('data-width="3840"');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});