|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/14/2026 |
| 5 | +GetStateVariantClassification.test.mjs |
| 6 | +*/ |
| 7 | +import assert from "node:assert/strict"; |
| 8 | +import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"; |
| 9 | +import path from "node:path"; |
| 10 | +import { fileURLToPath } from "node:url"; |
| 11 | +import { |
| 12 | + classifyGetStateVariantDomain, |
| 13 | + classifyGetStateVariantLayer, |
| 14 | + extractGetStateVariantNames, |
| 15 | + bucketGetStateVariants, |
| 16 | +} from "../../src/shared/state/index.js"; |
| 17 | + |
| 18 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); |
| 19 | +const TARGET_ROOTS = ["src", "games", "samples", "tools"]; |
| 20 | + |
| 21 | +function collectJsLikeFiles(rootDir) { |
| 22 | + const files = []; |
| 23 | + const stack = [rootDir]; |
| 24 | + while (stack.length > 0) { |
| 25 | + const current = stack.pop(); |
| 26 | + const entries = readdirSync(current, { withFileTypes: true }); |
| 27 | + entries.forEach((entry) => { |
| 28 | + const fullPath = path.join(current, entry.name); |
| 29 | + if (entry.isDirectory()) { |
| 30 | + stack.push(fullPath); |
| 31 | + return; |
| 32 | + } |
| 33 | + if (entry.isFile() && (entry.name.endsWith(".js") || entry.name.endsWith(".mjs"))) { |
| 34 | + files.push(fullPath); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + return files; |
| 39 | +} |
| 40 | + |
| 41 | +function toRepoRelative(filePath) { |
| 42 | + return path.relative(ROOT, filePath).replaceAll("\\", "/"); |
| 43 | +} |
| 44 | + |
| 45 | +function collectGetStateVariantEntries() { |
| 46 | + const entries = []; |
| 47 | + TARGET_ROOTS.forEach((root) => { |
| 48 | + const rootPath = path.join(ROOT, root); |
| 49 | + if (!existsSync(rootPath) || !statSync(rootPath).isDirectory()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + collectJsLikeFiles(rootPath).forEach((filePath) => { |
| 54 | + const sourceText = readFileSync(filePath, "utf8"); |
| 55 | + const names = extractGetStateVariantNames(sourceText); |
| 56 | + const rel = toRepoRelative(filePath); |
| 57 | + names.forEach((name) => { |
| 58 | + entries.push({ name, filePath: rel }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | + return entries; |
| 63 | +} |
| 64 | + |
| 65 | +export function run() { |
| 66 | + assert.equal(classifyGetStateVariantDomain("getSimulationState"), "simulation"); |
| 67 | + assert.equal(classifyGetStateVariantDomain("getReplayState"), "replay"); |
| 68 | + assert.equal(classifyGetStateVariantDomain("getEditorState"), "editor"); |
| 69 | + assert.equal(classifyGetStateVariantDomain("getState"), "other"); |
| 70 | + |
| 71 | + assert.equal(classifyGetStateVariantLayer("samples/phase-01/test.js"), "sample"); |
| 72 | + assert.equal(classifyGetStateVariantLayer("tools/shared/test.js"), "tool"); |
| 73 | + assert.equal(classifyGetStateVariantLayer("src/engine/test.js"), "runtime"); |
| 74 | + assert.equal(classifyGetStateVariantLayer("games/Asteroids/test.js"), "runtime"); |
| 75 | + |
| 76 | + const entries = collectGetStateVariantEntries(); |
| 77 | + assert.equal(entries.length > 0, true); |
| 78 | + |
| 79 | + const buckets = bucketGetStateVariants(entries); |
| 80 | + assert.equal(buckets.byDomain.simulation.length > 0, true); |
| 81 | + assert.equal(buckets.byDomain.replay.length > 0, true); |
| 82 | + assert.equal(buckets.byDomain.editor.length > 0, true); |
| 83 | + assert.equal(buckets.byDomain.other.length > 0, true); |
| 84 | + |
| 85 | + assert.equal(buckets.byLayer.sample.length > 0, true); |
| 86 | + assert.equal(buckets.byLayer.tool.length > 0, true); |
| 87 | + assert.equal(buckets.byLayer.runtime.length > 0, true); |
| 88 | + |
| 89 | + const simulationNames = new Set(buckets.byDomain.simulation.map((entry) => entry.name)); |
| 90 | + const replayNames = new Set(buckets.byDomain.replay.map((entry) => entry.name)); |
| 91 | + const editorNames = new Set(buckets.byDomain.editor.map((entry) => entry.name)); |
| 92 | + assert.equal(simulationNames.has("getSimulationState"), true); |
| 93 | + assert.equal(replayNames.has("getReplayState"), true); |
| 94 | + assert.equal(editorNames.has("getEditorState"), true); |
| 95 | +} |
| 96 | + |
| 97 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 98 | + run(); |
| 99 | +} |
0 commit comments