forked from Fei-Away/Codex-Dream-Skin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer-inject.test.mjs
More file actions
543 lines (523 loc) · 20.4 KB
/
Copy pathrenderer-inject.test.mjs
File metadata and controls
543 lines (523 loc) · 20.4 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import vm from "node:vm";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const macosRoot = path.resolve(here, "..");
const template = await fs.readFile(path.join(macosRoot, "assets", "renderer-inject.js"), "utf8");
const css = await fs.readFile(path.join(macosRoot, "assets", "dream-skin.css"), "utf8");
assert.doesNotMatch(
css,
/main\.main-surface\s*>\s*header\.app-header-tint\s*\{[^}]*\b(?:position|z-index)\s*:/,
"The skin must preserve Codex's native fixed header so the side-panel toggle remains reachable.",
);
assert.doesNotMatch(
css,
/main\.main-surface:not\(\.dream-skin-home-shell\)\s*>\s*\*\s*\{[^}]*\bposition\s*:/,
"Task-route child layering must not overwrite the native header position.",
);
assert.doesNotMatch(
css,
/background-image:\s*var\(--dream-skin-art\),\s*var\(--dream-skin-art\)/,
"The home hero must not stack duplicate copies of the selected image.",
);
assert.match(
css,
/data-dream-art-safe="left"[\s\S]{0,140}--ds-art-position:\s*100% var\(--ds-focus-y\);/,
"A left text-safe image must preserve its right-side subject on narrower windows.",
);
assert.doesNotMatch(
css,
/background-size:\s*auto 100% !important;/,
"Wide home artwork must not leave an unpainted half-card by fitting only to height.",
);
assert.doesNotMatch(
css,
/background-size:\s*100% 100%,\s*100% 100%,\s*100% auto;/,
"Wide task artwork must cover the full route instead of ending above the composer.",
);
assert.match(
css,
/data-dream-art-task-mode="ambient"[\s\S]{0,500}body\s*\{[\s\S]{0,500}background-image:\s*var\(--dream-skin-art\) !important;[\s\S]{0,200}background-size:\s*cover !important;/,
"Wide ambient task artwork should cover the full application window.",
);
assert.match(
css,
/data-dream-task-mode="banner"[\s\S]{0,900}body\s*\{[\s\S]{0,500}background-image:\s*var\(--dream-skin-art\) !important;[\s\S]{0,200}background-size:\s*cover !important;/,
"Wide banner task artwork should use the same full-window wallpaper contract as ambient routes.",
);
assert.match(
css,
/data-dream-art-wide="true"\]:has\(main\.main-surface\.dream-skin-home-shell\)[\s\S]{0,100}body\s*\{[\s\S]{0,300}background-image:\s*var\(--dream-skin-art\) !important;/,
"Wide home artwork should use the same full-window image as utility routes.",
);
assert.match(
css,
/data-dream-art-wide="true"\]:has\(main\.main-surface\.dream-skin-home-shell\)[\s\S]{0,120}body\s*\{[\s\S]{0,260}background-position:\s*var\(--ds-art-position\) !important;/,
"Wide home artwork must honor the configured focal point instead of forcing a centered crop.",
);
assert.match(
css,
/data-dream-art-task-mode="ambient"[\s\S]{0,260}data-dream-art-wide="true"\]:has\(main\.main-surface:not\(\.dream-skin-home-shell\)\)[\s\S]{0,120}body\s*\{[\s\S]{0,260}background-position:\s*var\(--ds-art-position\) !important;/,
"Wide task artwork must retain the same focal point as the home route.",
);
assert.match(
css,
/data-dream-art-wide="true"\]\s+\.composer-surface-chrome\s*\{[\s\S]{0,500}backdrop-filter:\s*none !important;/,
"Wide artwork should use one uniform composer surface without a split blur layer.",
);
assert.match(
css,
/--ds-immersive-composer-solid:\s*rgb\(var\(--ds-panel-rgb\) \/ \.74\);/,
"The light composer should retain enough transparency to reveal the selected artwork.",
);
assert.match(
css,
/data-dream-shell="light"\]\[data-dream-art-wide="true"\][\s\S]{0,100}\.composer-surface-chrome\s*\{[\s\S]{0,400}backdrop-filter:\s*blur\(8px\) saturate\(102%\) !important;/,
"The translucent light composer should softly separate text from detailed artwork.",
);
assert.match(
template,
/\[class\*="_homeUtilityBar_"\][\s\S]{0,500}dream-skin-home-utility/,
"The renderer should give the current native home utility bar a stable theme class.",
);
assert.match(
css,
/\.dream-skin-home:has\(\.dream-skin-home-utility\)[\s\S]{0,120}\.composer-surface-chrome\s*\{[\s\S]{0,180}border-radius:\s*0 0 22px 22px !important;/,
"The home utility bar and composer should render as one continuous control.",
);
assert.match(
css,
/\.composer-surface-chrome button:not\(\[class~="bg-token-foreground"\]\)[\s\S]{0,100}color:\s*var\(--ds-muted\) !important;/,
"Composer controls must remain readable when Codex native tokens lag behind a forced dark appearance.",
);
assert.match(
css,
/\.composer-surface-chrome button:not\(\[class~="bg-token-foreground"\]\) \*\s*\{[\s\S]{0,80}color:\s*currentColor !important;/,
"Nested labels inside composer controls must inherit the corrected theme color.",
);
assert.match(
css,
/\.composer-surface-chrome p\.placeholder::after\s*\{[\s\S]{0,120}color:\s*rgb\(var\(--ds-muted-rgb\) \/ \.82\) !important;[\s\S]{0,80}opacity:\s*1 !important;/,
"Composer placeholder text must not inherit a stale native color with double opacity.",
);
assert.match(
css,
/header\.app-header-tint\s*\{[\s\S]{0,180}background:\s*transparent !important;/,
"Wide artwork should not paint a separate opaque header band.",
);
assert.match(
css,
/\.thread-scroll-container \.bg-gradient-to-t\.from-token-main-surface-primary\s*\{[\s\S]{0,100}background:\s*transparent !important;/,
"Wide artwork should remove the native opaque fade behind the sticky composer.",
);
assert.match(
css,
/div\.sticky:has\(input\[type="text"\]\)[\s\S]{0,100}background:\s*transparent !important;/,
"Search routes should not retain the native opaque sticky band.",
);
assert.match(
css,
/\[class~="bg-token-main-surface-primary"\]\[class~="h-full"\]\[class~="w-full"\][\s\S]{0,100}background:\s*transparent !important;/,
"Full-size utility route wrappers should not hide the selected artwork.",
);
function createStyleDeclaration() {
const values = new Map();
return {
values,
getPropertyValue(name) { return values.get(name) ?? ""; },
setProperty(name, value) { values.set(name, value); },
removeProperty(name) { values.delete(name); },
};
}
function createClassList(initial = []) {
const values = new Set(initial);
return {
values,
add(...names) { for (const name of names) values.add(name); },
remove(...names) { for (const name of names) values.delete(name); },
contains(name) { return values.has(name); },
toggle(name, enabled) {
if (enabled) values.add(name);
else values.delete(name);
},
};
}
function createFixture(theme, {
nativeShell = "light",
analysisFixture = null,
analysisCache = null,
} = {}) {
let fixtureShell = nativeShell;
const nodes = new Map();
const attributes = new Map();
const bodyAttributes = new Map();
const observers = [];
const resizeObservers = [];
const timers = new Map();
let nextTimer = 1;
let nextBlob = 1;
const rootStyle = createStyleDeclaration();
const root = {
className: nativeShell === "dark" ? "electron-dark" : "electron-light",
classList: createClassList(),
style: rootStyle,
appendChild(node) {
node.parentElement = root;
if (node.id) nodes.set(node.id, node);
},
getAttribute(name) { return attributes.get(name) ?? null; },
setAttribute(name, value) { attributes.set(name, String(value)); },
removeAttribute(name) { attributes.delete(name); },
};
const body = {
className: "",
appendChild(node) {
node.parentElement = body;
if (node.id) nodes.set(node.id, node);
},
getAttribute(name) { return bodyAttributes.get(name) ?? null; },
setAttribute(name, value) { bodyAttributes.set(name, String(value)); },
};
const shellBox = { left: 280, top: 36, width: 1000, height: 764 };
const shellMain = {
classList: createClassList(),
getBoundingClientRect() {
return { ...shellBox };
},
};
const createElement = (tagName) => {
if (tagName === "canvas" && analysisFixture) {
return {
width: 0,
height: 0,
getContext() {
return {
drawImage() {},
getImageData() { return { data: analysisFixture.pixels }; },
};
},
};
}
const childNodes = new Map();
const element = {
id: "",
dataset: {},
style: createStyleDeclaration(),
classList: createClassList(),
parentElement: null,
textContent: "",
innerHTML: "",
setAttribute() {},
querySelector(selector) {
if (!childNodes.has(selector)) childNodes.set(selector, { textContent: "" });
return childNodes.get(selector);
},
remove() { if (element.id) nodes.delete(element.id); },
};
return element;
};
const document = {
documentElement: root,
head: root,
body,
createElement,
getElementById(id) { return nodes.get(id) ?? null; },
querySelector(selector) {
if (selector === "main.main-surface" || selector === "main") return shellMain;
return null;
},
querySelectorAll() { return []; },
};
const mediaQuery = {
matches: false,
addEventListener() {},
removeEventListener() {},
};
const revokedUrls = [];
const window = {
addEventListener() {},
removeEventListener() {},
matchMedia() {
mediaQuery.matches = fixtureShell === "dark";
return mediaQuery;
},
};
if (analysisCache) window.__CODEX_DREAM_SKIN_ANALYSIS_CACHE__ = analysisCache;
if (analysisFixture) {
window.Image = class {
naturalWidth = analysisFixture.naturalWidth;
naturalHeight = analysisFixture.naturalHeight;
set src(_) { this.onload(); }
};
}
const context = {
window,
document,
MutationObserver: class {
constructor(callback) {
this.callback = callback;
observers.push(this);
}
observe() {}
disconnect() {}
},
ResizeObserver: class {
constructor(callback) {
this.callback = callback;
this.target = null;
resizeObservers.push(this);
}
observe(target) { this.target = target; }
disconnect() { this.target = null; }
},
URL: {
createObjectURL() { return `blob:fixture-${nextBlob++}`; },
revokeObjectURL(value) { revokedUrls.push(value); },
},
Blob,
Uint8Array,
atob,
getComputedStyle() {
const skinShell = root.classList.contains("codex-dream-skin")
? (attributes.get("data-dream-shell") || "dark") : fixtureShell;
return {
colorScheme: skinShell,
backgroundColor: fixtureShell === "dark" ? "rgb(24, 24, 27)" : "rgb(250, 250, 250)",
};
},
setInterval: () => 1,
clearInterval() {},
setTimeout(callback, delay) {
const id = ++nextTimer;
timers.set(id, { callback, delay });
return id;
},
clearTimeout(id) { timers.delete(id); },
cancelAnimationFrame() {},
};
const payloadFor = (nextTheme, cssText = ".fixture { color: blue; }") => template
.replace("__DREAM_SKIN_CSS_JSON__", JSON.stringify(cssText))
.replace("__DREAM_SKIN_ART_JSON__", JSON.stringify("data:image/png;base64,AA=="))
.replace("__DREAM_SKIN_THEME_JSON__", JSON.stringify(nextTheme))
.replace("__DREAM_SKIN_VERSION_JSON__", JSON.stringify("test"))
.replace("__DREAM_SKIN_STYLE_REVISION_JSON__", JSON.stringify(cssText));
const flushTimers = (maximumDelay = Infinity) => {
const pending = [...timers.entries()].filter(([, timer]) => timer.delay <= maximumDelay);
for (const [id, timer] of pending) {
timers.delete(id);
timer.callback();
}
};
return {
attributes,
body,
bodyAttributes,
context,
flushTimers,
nodes,
observers,
payload: payloadFor(theme),
payloadFor,
revokedUrls,
resizeObservers,
root,
rootStyle,
shellBox,
timers,
window,
setNativeShell(value) { fixtureShell = value; },
};
}
const defaults = createFixture({
id: "default-contract",
appearance: "auto",
art: { safeArea: "auto", taskMode: "auto" },
});
const defaultResult = vm.runInNewContext(defaults.payload, defaults.context);
assert.equal(defaultResult.installed, true);
assert.equal(defaults.attributes.get("data-dream-shell"), "light");
assert.equal(defaults.attributes.get("data-dream-art-safe-area"), "center");
assert.equal(defaults.attributes.get("data-dream-art-task-mode"), "ambient");
assert.equal(defaults.attributes.get("data-dream-art-ready"), "false");
assert.equal(defaults.rootStyle.values.get("--dream-art-position"), "50.00% 50.00%");
const defaultMetrics = defaults.window.__CODEX_DREAM_SKIN_STATE__.metrics;
assert.equal(defaultMetrics.rootPasses, 1);
assert.equal(defaultMetrics.routePasses, 1);
assert.equal(defaultMetrics.layoutReads, 1);
for (let index = 0; index < 50; index += 1) defaults.observers[0].callback([]);
assert.equal(defaults.timers.size, 1, "Mutation bursts should coalesce into one scheduled ensure.");
defaults.flushTimers(64);
assert.equal(defaultMetrics.rootPasses, 1, "Subtree mutations must not recompute root theme tokens.");
assert.equal(defaultMetrics.routePasses, 2);
assert.equal(defaultMetrics.layoutReads, 1, "Subtree mutations must not force shell layout reads.");
assert.equal(defaults.resizeObservers.length, 1);
assert.ok(defaults.resizeObservers[0].target);
defaults.shellBox.left = 196;
defaults.shellBox.width = 1084;
defaults.resizeObservers[0].callback([]);
defaults.flushTimers(64);
assert.equal(defaultMetrics.layoutReads, 2, "Shell ResizeObserver changes must refresh chrome geometry.");
const defaultChrome = defaults.nodes.get("codex-dream-skin-chrome");
assert.equal(defaultChrome.style.values.get("left"), "196px");
assert.equal(defaultChrome.style.values.get("width"), "1084px");
// Auto appearance must continue following the native shell after the skin is
// already installed. The fixture makes the injected root color-scheme win
// whenever our class remains on <html>, so a temporary native probe is needed
// for each light → dark → light transition.
const shellFollow = createFixture({
id: "shell-follow",
appearance: "auto",
art: { safeArea: "auto", taskMode: "auto" },
});
shellFollow.root.className = "";
vm.runInNewContext(shellFollow.payload, shellFollow.context);
assert.equal(shellFollow.attributes.get("data-dream-shell"), "light");
shellFollow.setNativeShell("dark");
shellFollow.window.__CODEX_DREAM_SKIN_STATE__.ensure();
assert.equal(shellFollow.attributes.get("data-dream-shell"), "dark");
shellFollow.setNativeShell("light");
shellFollow.window.__CODEX_DREAM_SKIN_STATE__.ensure();
assert.equal(shellFollow.attributes.get("data-dream-shell"), "light");
defaults.root.className = "";
defaults.body.setAttribute("data-theme", "dark");
defaults.observers[1].callback([{ type: "attributes", target: defaults.body }]);
defaults.flushTimers(64);
assert.equal(defaults.attributes.get("data-dream-shell"), "dark", "Body theme changes must apply without the fallback interval.");
const synchronousWide = createFixture({
id: "synchronous-wide",
appearance: "auto",
art: { safeArea: "auto", taskMode: "auto" },
artKey: "wide-art",
artMetadata: {
width: 2400,
height: 1350,
ratio: 2400 / 1350,
wide: true,
aspect: "wide",
taskMode: "ambient",
},
});
vm.runInNewContext(synchronousWide.payload, synchronousWide.context);
assert.equal(synchronousWide.attributes.get("data-dream-art-wide"), "true");
assert.equal(synchronousWide.attributes.get("data-dream-art-aspect"), "wide");
assert.equal(synchronousWide.attributes.get("data-dream-art-task-mode"), "ambient");
assert.equal(synchronousWide.attributes.get("data-dream-art-ready"), "false");
const cachedAnalysis = {
width: 2400,
height: 1350,
ratio: 2400 / 1350,
wide: true,
aspect: "wide",
taskMode: "ambient",
safeArea: "left",
focusX: 0.72,
focusY: 0.48,
accentRgb: { r: 180, g: 90, b: 110 },
};
const cached = createFixture({
id: "cached-wide",
appearance: "auto",
art: { safeArea: "auto", taskMode: "auto" },
artKey: "cached-art",
artMetadata: synchronousWide.window.__CODEX_DREAM_SKIN_STATE__.artMetadata,
}, { analysisCache: new Map([["cached-art", cachedAnalysis]]) });
vm.runInNewContext(cached.payload, cached.context);
assert.equal(cached.attributes.get("data-dream-art-ready"), "true");
assert.equal(cached.attributes.get("data-dream-art-safe-area"), "left");
assert.equal(cached.window.__CODEX_DREAM_SKIN_STATE__.metrics.analysisCacheHits, 1);
assert.equal(cached.window.__CODEX_DREAM_SKIN_STATE__.metrics.analysisRuns, 0);
const previousWideState = synchronousWide.window.__CODEX_DREAM_SKIN_STATE__;
const stableStyle = synchronousWide.nodes.get("codex-dream-skin-style");
vm.runInNewContext(synchronousWide.payloadFor({
id: "switched-wide",
appearance: "dark",
art: { safeArea: "right", taskMode: "ambient" },
artKey: "switched-art",
artMetadata: {
width: 2400,
height: 1350,
ratio: 2400 / 1350,
wide: true,
aspect: "wide",
taskMode: "ambient",
},
}, ".fixture { color: red; }"), synchronousWide.context);
assert.equal(synchronousWide.nodes.get("codex-dream-skin-style"), stableStyle);
assert.equal(stableStyle.textContent, ".fixture { color: red; }");
assert.equal(stableStyle.dataset.dreamSkinVersion, "test");
assert.equal(synchronousWide.rootStyle.values.get("--dream-skin-art"), 'url("blob:fixture-2")');
assert.deepEqual(synchronousWide.revokedUrls, ["blob:fixture-1"]);
assert.equal(previousWideState.cleanup(), false, "An old async cleanup must not remove the new theme.");
const brightPixels = new Uint8ClampedArray(96 * 32 * 4);
for (let offset = 0; offset < brightPixels.length; offset += 4) {
brightPixels[offset] = 245;
brightPixels[offset + 1] = 224;
brightPixels[offset + 2] = 224;
brightPixels[offset + 3] = 255;
}
const nativeDark = createFixture({
id: "native-dark-contract",
appearance: "auto",
art: { safeArea: "auto", taskMode: "auto" },
}, {
nativeShell: "dark",
analysisFixture: { naturalWidth: 2400, naturalHeight: 800, pixels: brightPixels },
});
vm.runInNewContext(nativeDark.payload, nativeDark.context);
await Promise.resolve();
await Promise.resolve();
nativeDark.window.__CODEX_DREAM_SKIN_STATE__.ensure();
assert.equal(nativeDark.window.__CODEX_DREAM_SKIN_STATE__.analysis.shell, "light");
assert.equal(nativeDark.attributes.get("data-dream-shell"), "dark");
assert.match(nativeDark.rootStyle.values.get("--ds-bg"), /^#[0-9a-f]{6}$/);
assert.ok(Number.parseInt(nativeDark.rootStyle.values.get("--ds-bg").slice(1), 16) < 0x303030);
const explicit = createFixture({
id: "explicit-contract",
appearance: "dark",
art: { focusX: 0.15, focusY: 0.8, safeArea: "none", taskMode: "off" },
});
const explicitResult = vm.runInNewContext(explicit.payload, explicit.context);
assert.equal(explicitResult.shell, "dark");
assert.equal(explicit.attributes.get("data-dream-shell"), "dark");
assert.equal(explicit.attributes.get("data-dream-art-safe-area"), "none");
assert.equal(explicit.attributes.get("data-dream-art-safe"), "none");
assert.equal(explicit.attributes.get("data-dream-art-task-mode"), "off");
assert.equal(explicit.rootStyle.values.get("--dream-art-position"), "15.00% 80.00%");
assert.equal(explicit.window.__CODEX_DREAM_SKIN_STATE__.analysis, null);
const banner = createFixture({
id: "banner-contract",
appearance: "auto",
art: { safeArea: "left", taskMode: "banner" },
artMetadata: {
width: 2560,
height: 1440,
ratio: 2560 / 1440,
wide: true,
aspect: "ultrawide",
taskMode: "banner",
safeArea: "left",
focusX: 0.72,
focusY: 0.44,
},
});
vm.runInNewContext(banner.payload, banner.context);
assert.equal(banner.attributes.get("data-dream-art-wide"), "true");
assert.equal(banner.attributes.get("data-dream-art-task-mode"), "banner");
assert.equal(banner.attributes.get("data-dream-task-mode"), "banner");
assert.equal(explicit.window.__CODEX_DREAM_SKIN_STATE__.cleanup(), true);
assert.equal(explicit.root.classList.contains("codex-dream-skin"), false);
assert.equal(explicit.attributes.has("data-dream-shell"), false);
assert.equal(explicit.attributes.has("data-dream-art-safe-area"), false);
assert.equal(explicit.attributes.has("data-dream-art-task-mode"), false);
assert.equal(explicit.rootStyle.values.has("--dream-art-position"), false);
assert.equal(explicit.nodes.has("codex-dream-skin-style"), false);
assert.equal(explicit.nodes.has("codex-dream-skin-chrome"), false);
assert.deepEqual(explicit.revokedUrls, ["blob:fixture-1"]);
await Promise.resolve();
await Promise.resolve();
assert.equal(explicit.root.classList.contains("codex-dream-skin"), false);
assert.equal(explicit.nodes.has("codex-dream-skin-style"), false);
assert.equal(explicit.window.__CODEX_DREAM_SKIN_STATE__, undefined);
console.log("PASS: renderer honors adaptive art metadata, fallback, and cleanup behavior.");