|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import vm from "node:vm"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | +const windowsRoot = path.resolve(here, ".."); |
| 9 | +const template = await fs.readFile(path.join(windowsRoot, "assets", "renderer-inject.js"), "utf8"); |
| 10 | +const payload = template |
| 11 | + .replace("__DREAM_CSS_JSON__", JSON.stringify(".fixture { color: blue; }")) |
| 12 | + .replace("__DREAM_ART_JSON__", JSON.stringify("data:image/png;base64,AA==")); |
| 13 | + |
| 14 | +function createFixture({ shellPresent, staleSkin = false }) { |
| 15 | + const nodes = new Map(); |
| 16 | + const rootClasses = new Set(staleSkin ? ["codex-dream-skin"] : []); |
| 17 | + const rootStyles = new Map(staleSkin ? [["--dream-art", "url(\"blob:stale\")"]] : []); |
| 18 | + const revokedUrls = []; |
| 19 | + let hasShell = shellPresent; |
| 20 | + |
| 21 | + const makeClassList = (classes = new Set()) => ({ |
| 22 | + add(value) { classes.add(value); }, |
| 23 | + remove(value) { classes.delete(value); }, |
| 24 | + toggle(value, enabled) { |
| 25 | + if (enabled) classes.add(value); |
| 26 | + else classes.delete(value); |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const root = { |
| 31 | + classList: makeClassList(rootClasses), |
| 32 | + style: { |
| 33 | + setProperty(key, value) { rootStyles.set(key, value); }, |
| 34 | + removeProperty(key) { rootStyles.delete(key); }, |
| 35 | + }, |
| 36 | + appendChild(node) { |
| 37 | + node.parentElement = root; |
| 38 | + nodes.set(node.id, node); |
| 39 | + }, |
| 40 | + }; |
| 41 | + const body = { |
| 42 | + appendChild(node) { |
| 43 | + node.parentElement = body; |
| 44 | + nodes.set(node.id, node); |
| 45 | + }, |
| 46 | + }; |
| 47 | + const shellMain = { |
| 48 | + classList: makeClassList(), |
| 49 | + getBoundingClientRect() { |
| 50 | + return { left: 290, top: 36, width: 990, height: 784 }; |
| 51 | + }, |
| 52 | + }; |
| 53 | + const staleHome = { classList: makeClassList(new Set(["dream-home"])) }; |
| 54 | + const staleShell = { classList: makeClassList(new Set(["dream-home-shell"])) }; |
| 55 | + |
| 56 | + const createElement = () => ({ |
| 57 | + id: "", |
| 58 | + dataset: {}, |
| 59 | + style: {}, |
| 60 | + classList: makeClassList(), |
| 61 | + parentElement: null, |
| 62 | + textContent: "", |
| 63 | + innerHTML: "", |
| 64 | + setAttribute() {}, |
| 65 | + remove() { nodes.delete(this.id); }, |
| 66 | + }); |
| 67 | + if (staleSkin) { |
| 68 | + const style = createElement(); |
| 69 | + style.id = "codex-dream-skin-style"; |
| 70 | + nodes.set(style.id, style); |
| 71 | + const chrome = createElement(); |
| 72 | + chrome.id = "codex-dream-skin-chrome"; |
| 73 | + nodes.set(chrome.id, chrome); |
| 74 | + } |
| 75 | + |
| 76 | + const document = { |
| 77 | + documentElement: root, |
| 78 | + head: root, |
| 79 | + body, |
| 80 | + createElement, |
| 81 | + getElementById(id) { return nodes.get(id) ?? null; }, |
| 82 | + querySelector(selector) { |
| 83 | + if (selector === "main.main-surface") return hasShell ? shellMain : null; |
| 84 | + if (selector === "aside.app-shell-left-panel") return hasShell ? {} : null; |
| 85 | + return null; |
| 86 | + }, |
| 87 | + querySelectorAll(selector) { |
| 88 | + if (!staleSkin) return []; |
| 89 | + if (selector === ".dream-home") return [staleHome]; |
| 90 | + if (selector === ".dream-home-shell") return [staleShell]; |
| 91 | + return []; |
| 92 | + }, |
| 93 | + }; |
| 94 | + const context = { |
| 95 | + window: {}, |
| 96 | + document, |
| 97 | + MutationObserver: class { |
| 98 | + observe() {} |
| 99 | + disconnect() {} |
| 100 | + }, |
| 101 | + URL: { |
| 102 | + createObjectURL() { return "blob:fixture"; }, |
| 103 | + revokeObjectURL(value) { revokedUrls.push(value); }, |
| 104 | + }, |
| 105 | + Blob, |
| 106 | + Uint8Array, |
| 107 | + atob, |
| 108 | + setInterval: () => 1, |
| 109 | + clearInterval: () => {}, |
| 110 | + setTimeout: () => 2, |
| 111 | + clearTimeout: () => {}, |
| 112 | + }; |
| 113 | + |
| 114 | + return { |
| 115 | + context, |
| 116 | + nodes, |
| 117 | + rootClasses, |
| 118 | + rootStyles, |
| 119 | + revokedUrls, |
| 120 | + setShellPresent(value) { hasShell = value; }, |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +const main = createFixture({ shellPresent: true }); |
| 125 | +const mainResult = vm.runInNewContext(payload, main.context); |
| 126 | +assert.equal(mainResult.installed, true); |
| 127 | +assert.equal(main.rootClasses.has("codex-dream-skin"), true); |
| 128 | +assert.equal(main.rootStyles.get("--dream-art"), 'url("blob:fixture")'); |
| 129 | +assert.equal(main.nodes.has("codex-dream-skin-style"), true); |
| 130 | +assert.equal(main.nodes.has("codex-dream-skin-chrome"), true); |
| 131 | +assert.equal(main.context.window.__CODEX_DREAM_SKIN_STATE__.cleanup(), true); |
| 132 | +assert.equal(main.rootClasses.has("codex-dream-skin"), false); |
| 133 | +assert.equal(main.nodes.has("codex-dream-skin-style"), false); |
| 134 | +assert.equal(main.nodes.has("codex-dream-skin-chrome"), false); |
| 135 | +assert.deepEqual(main.revokedUrls, ["blob:fixture"]); |
| 136 | + |
| 137 | +const auxiliary = createFixture({ shellPresent: false, staleSkin: true }); |
| 138 | +const auxiliaryResult = vm.runInNewContext(payload, auxiliary.context); |
| 139 | +assert.equal(auxiliaryResult.installed, true); |
| 140 | +assert.equal(auxiliary.rootClasses.has("codex-dream-skin"), false); |
| 141 | +assert.equal(auxiliary.rootStyles.has("--dream-art"), false); |
| 142 | +assert.equal(auxiliary.nodes.has("codex-dream-skin-style"), false); |
| 143 | +assert.equal(auxiliary.nodes.has("codex-dream-skin-chrome"), false); |
| 144 | + |
| 145 | +auxiliary.setShellPresent(true); |
| 146 | +auxiliary.context.window.__CODEX_DREAM_SKIN_STATE__.ensure(); |
| 147 | +assert.equal(auxiliary.rootClasses.has("codex-dream-skin"), true); |
| 148 | +assert.equal(auxiliary.nodes.has("codex-dream-skin-style"), true); |
| 149 | +assert.equal(auxiliary.nodes.has("codex-dream-skin-chrome"), true); |
| 150 | + |
| 151 | +console.log("PASS: renderer themes the Codex shell and preserves transparent auxiliary windows."); |
0 commit comments