|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import vm from "node:vm"; |
| 5 | +import { execFileSync } from "node:child_process"; |
| 6 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | +const repoRoot = path.resolve(__dirname, "..", ".."); |
| 11 | +const toolPath = path.join(repoRoot, "tools", "asset-manager-v2", "index.js"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "pr_11_316_asset_manager_add_remove_results.json"); |
| 13 | + |
| 14 | +function checkSyntax(filePath) { |
| 15 | + execFileSync(process.execPath, ["--check", filePath], { |
| 16 | + cwd: repoRoot, |
| 17 | + stdio: ["ignore", "pipe", "pipe"] |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function createElementRecord(id = "") { |
| 22 | + return { |
| 23 | + id, |
| 24 | + value: "", |
| 25 | + textContent: "", |
| 26 | + hidden: false, |
| 27 | + type: "", |
| 28 | + children: [], |
| 29 | + listeners: {}, |
| 30 | + append(...nodes) { |
| 31 | + this.children.push(...nodes); |
| 32 | + }, |
| 33 | + appendChild(node) { |
| 34 | + this.children.push(node); |
| 35 | + return node; |
| 36 | + }, |
| 37 | + replaceChildren(...nodes) { |
| 38 | + this.children = [...nodes]; |
| 39 | + }, |
| 40 | + addEventListener(eventName, handler) { |
| 41 | + this.listeners[eventName] = handler; |
| 42 | + }, |
| 43 | + click() { |
| 44 | + if (typeof this.listeners.click === "function") { |
| 45 | + this.listeners.click(); |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +function createHarness() { |
| 52 | + const elementRecords = new Map(); |
| 53 | + const storage = new Map(); |
| 54 | + const validSession = { |
| 55 | + version: "v2", |
| 56 | + toolId: "asset-manager-v2", |
| 57 | + payloadJson: { |
| 58 | + assetCatalog: { |
| 59 | + name: "Catalog", |
| 60 | + entries: [ |
| 61 | + { id: "ship-1", label: "Ship", kind: "svg", path: "assets/ship.svg" } |
| 62 | + ] |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + storage.set("ctx-1", JSON.stringify(validSession)); |
| 67 | + |
| 68 | + const document = { |
| 69 | + title: "", |
| 70 | + body: { |
| 71 | + dataset: {} |
| 72 | + }, |
| 73 | + getElementById(id) { |
| 74 | + if (!elementRecords.has(id)) { |
| 75 | + elementRecords.set(id, createElementRecord(id)); |
| 76 | + } |
| 77 | + return elementRecords.get(id); |
| 78 | + }, |
| 79 | + createElement(tagName) { |
| 80 | + const element = createElementRecord(tagName); |
| 81 | + element.tagName = tagName; |
| 82 | + return element; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const window = { |
| 87 | + location: { |
| 88 | + href: "https://example.test/tools/asset-manager-v2/index.html?hostContextId=ctx-1" |
| 89 | + }, |
| 90 | + addEventListener() {}, |
| 91 | + sessionStorage: { |
| 92 | + getItem(key) { |
| 93 | + return storage.has(key) ? storage.get(key) : null; |
| 94 | + }, |
| 95 | + setItem(key, value) { |
| 96 | + storage.set(key, String(value)); |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + const context = vm.createContext({ |
| 102 | + window, |
| 103 | + document, |
| 104 | + URL, |
| 105 | + JSON, |
| 106 | + Error, |
| 107 | + console: { log() {}, error() {} } |
| 108 | + }); |
| 109 | + |
| 110 | + const rawSource = fs.readFileSync(toolPath, "utf8"); |
| 111 | + const classSource = rawSource.replace(/\nnew AssetBrowserV2\(\);\s*$/u, "\n"); |
| 112 | + vm.runInContext(`${classSource}\nthis.AssetBrowserV2 = AssetBrowserV2;`, context); |
| 113 | + const instance = new context.AssetBrowserV2(); |
| 114 | + |
| 115 | + return { instance, elementRecords, storage, rawSource }; |
| 116 | +} |
| 117 | + |
| 118 | +function setFormValues(elementRecords, values) { |
| 119 | + if (!elementRecords.has("assetManagerV2AddId")) elementRecords.set("assetManagerV2AddId", createElementRecord("assetManagerV2AddId")); |
| 120 | + if (!elementRecords.has("assetManagerV2AddLabel")) elementRecords.set("assetManagerV2AddLabel", createElementRecord("assetManagerV2AddLabel")); |
| 121 | + if (!elementRecords.has("assetManagerV2AddKind")) elementRecords.set("assetManagerV2AddKind", createElementRecord("assetManagerV2AddKind")); |
| 122 | + if (!elementRecords.has("assetManagerV2AddPath")) elementRecords.set("assetManagerV2AddPath", createElementRecord("assetManagerV2AddPath")); |
| 123 | + elementRecords.get("assetManagerV2AddId").value = values.id; |
| 124 | + elementRecords.get("assetManagerV2AddLabel").value = values.label; |
| 125 | + elementRecords.get("assetManagerV2AddKind").value = values.kind; |
| 126 | + elementRecords.get("assetManagerV2AddPath").value = values.path; |
| 127 | +} |
| 128 | + |
| 129 | +export function run() { |
| 130 | + checkSyntax(toolPath); |
| 131 | + const { instance, elementRecords, storage, rawSource } = createHarness(); |
| 132 | + if (!elementRecords.has("assetManagerV2ActionStatus")) { |
| 133 | + elementRecords.set("assetManagerV2ActionStatus", createElementRecord("assetManagerV2ActionStatus")); |
| 134 | + } |
| 135 | + const actionStatus = elementRecords.get("assetManagerV2ActionStatus"); |
| 136 | + const hostContextId = "ctx-1"; |
| 137 | + |
| 138 | + setFormValues(elementRecords, { |
| 139 | + id: "dup-asset", |
| 140 | + label: "Duplicate", |
| 141 | + kind: "png", |
| 142 | + path: "assets/duplicate.png" |
| 143 | + }); |
| 144 | + instance.addAssetEntry(); |
| 145 | + const afterFirstAdd = JSON.parse(storage.get(hostContextId)); |
| 146 | + setFormValues(elementRecords, { |
| 147 | + id: "dup-asset", |
| 148 | + label: "Duplicate Again", |
| 149 | + kind: "png", |
| 150 | + path: "assets/duplicate-again.png" |
| 151 | + }); |
| 152 | + instance.addAssetEntry(); |
| 153 | + const duplicateMessage = actionStatus.textContent; |
| 154 | + const afterDuplicateAttempt = JSON.parse(storage.get(hostContextId)); |
| 155 | + |
| 156 | + setFormValues(elementRecords, { |
| 157 | + id: " ", |
| 158 | + label: "Label", |
| 159 | + kind: "svg", |
| 160 | + path: "assets/x.svg" |
| 161 | + }); |
| 162 | + instance.addAssetEntry(); |
| 163 | + const whitespaceMessage = actionStatus.textContent; |
| 164 | + const afterWhitespaceAttempt = JSON.parse(storage.get(hostContextId)); |
| 165 | + |
| 166 | + instance.removeAssetEntryById("missing-asset-id"); |
| 167 | + const missingRemoveMessage = actionStatus.textContent; |
| 168 | + const afterMissingRemoveAttempt = JSON.parse(storage.get(hostContextId)); |
| 169 | + |
| 170 | + setFormValues(elementRecords, { |
| 171 | + id: "new-asset", |
| 172 | + label: "New Asset", |
| 173 | + kind: "png", |
| 174 | + path: "assets/new.png" |
| 175 | + }); |
| 176 | + instance.addAssetEntry(); |
| 177 | + const afterValidAdd = JSON.parse(storage.get(hostContextId)); |
| 178 | + |
| 179 | + instance.removeAssetEntryById("new-asset"); |
| 180 | + const afterValidRemove = JSON.parse(storage.get(hostContextId)); |
| 181 | + |
| 182 | + const summary = { |
| 183 | + generatedAt: new Date().toISOString(), |
| 184 | + checks: { |
| 185 | + duplicateIdRejected: duplicateMessage === "Add blocked. Asset id 'dup-asset' already exists.", |
| 186 | + duplicateIdDidNotMutateEntries: afterDuplicateAttempt.payloadJson.assetCatalog.entries.length === afterFirstAdd.payloadJson.assetCatalog.entries.length, |
| 187 | + whitespaceOnlyFieldRejected: whitespaceMessage === "Add blocked. Missing required field(s): id.", |
| 188 | + whitespaceRejectDidNotMutateEntries: afterWhitespaceAttempt.payloadJson.assetCatalog.entries.length === afterDuplicateAttempt.payloadJson.assetCatalog.entries.length, |
| 189 | + removeMissingIdRejected: missingRemoveMessage === "Remove blocked. Asset id 'missing-asset-id' was not found.", |
| 190 | + removeMissingDidNotMutateEntries: afterMissingRemoveAttempt.payloadJson.assetCatalog.entries.length === afterWhitespaceAttempt.payloadJson.assetCatalog.entries.length, |
| 191 | + validAddStillPersists: afterValidAdd.payloadJson.assetCatalog.entries.some((entry) => entry.id === "new-asset"), |
| 192 | + validRemoveStillPersists: !afterValidRemove.payloadJson.assetCatalog.entries.some((entry) => entry.id === "new-asset"), |
| 193 | + hasNoWhitespaceFallbackPath: !rawSource.includes("payloadJson: {}") |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 198 | + fs.writeFileSync(resultsPath, `${JSON.stringify(summary, null, 2)}\n`, "utf8"); |
| 199 | + |
| 200 | + for (const [key, value] of Object.entries(summary.checks)) { |
| 201 | + assert.equal(value, true, `${key} failed`); |
| 202 | + } |
| 203 | + |
| 204 | + return summary; |
| 205 | +} |
| 206 | + |
| 207 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 208 | + try { |
| 209 | + const summary = run(); |
| 210 | + console.log(JSON.stringify(summary, null, 2)); |
| 211 | + } catch (error) { |
| 212 | + console.error(error); |
| 213 | + process.exitCode = 1; |
| 214 | + } |
| 215 | +} |
0 commit comments