|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +import { |
| 5 | + chooseBestExtensionCandidate, |
| 6 | + chooseZhCnBundleName, |
| 7 | + parseEntryBundleFromIndexHtml, |
| 8 | + readVersionFromExtensionDirName, |
| 9 | +} from "./target-resolution.mjs"; |
| 10 | + |
| 11 | +test("readVersionFromExtensionDirName extracts version", () => { |
| 12 | + assert.equal( |
| 13 | + readVersionFromExtensionDirName("openai.chatgpt-0.4.71-win32-x64"), |
| 14 | + "0.4.71" |
| 15 | + ); |
| 16 | +}); |
| 17 | + |
| 18 | +test("chooseBestExtensionCandidate picks unique highest semver", () => { |
| 19 | + const picked = chooseBestExtensionCandidate([ |
| 20 | + { dir: "A", version: "0.4.70", mtimeMs: 10 }, |
| 21 | + { dir: "B", version: "0.4.71", mtimeMs: 1 }, |
| 22 | + ]); |
| 23 | + assert.equal(picked.dir, "B"); |
| 24 | +}); |
| 25 | + |
| 26 | +test("chooseBestExtensionCandidate fails on highest-version tie in strict mode", () => { |
| 27 | + assert.throws( |
| 28 | + () => |
| 29 | + chooseBestExtensionCandidate([ |
| 30 | + { dir: "A", version: "0.4.71", mtimeMs: 5 }, |
| 31 | + { dir: "B", version: "0.4.71", mtimeMs: 10 }, |
| 32 | + ]), |
| 33 | + /Ambiguous extension target/ |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +test("chooseBestExtensionCandidate allows mtime fallback when strictTarget=false", () => { |
| 38 | + const picked = chooseBestExtensionCandidate( |
| 39 | + [ |
| 40 | + { dir: "A", version: "0.4.71", mtimeMs: 5 }, |
| 41 | + { dir: "B", version: "0.4.71", mtimeMs: 10 }, |
| 42 | + ], |
| 43 | + { strictTarget: false } |
| 44 | + ); |
| 45 | + assert.equal(picked.dir, "B"); |
| 46 | +}); |
| 47 | + |
| 48 | +test("chooseBestExtensionCandidate falls back by mtime when versions are unparsable and strictTarget=false", () => { |
| 49 | + const picked = chooseBestExtensionCandidate( |
| 50 | + [ |
| 51 | + { dir: "A", version: "bad", mtimeMs: 1 }, |
| 52 | + { dir: "B", version: null, mtimeMs: 10 }, |
| 53 | + ], |
| 54 | + { strictTarget: false } |
| 55 | + ); |
| 56 | + assert.equal(picked.dir, "B"); |
| 57 | +}); |
| 58 | + |
| 59 | +test("parseEntryBundleFromIndexHtml returns active entry bundle", () => { |
| 60 | + const html = '<script type="module" src="./assets/index-abc123.js"></script>'; |
| 61 | + assert.equal(parseEntryBundleFromIndexHtml(html), "index-abc123.js"); |
| 62 | +}); |
| 63 | + |
| 64 | +test("parseEntryBundleFromIndexHtml returns null when entry is missing", () => { |
| 65 | + assert.equal(parseEntryBundleFromIndexHtml("<html></html>"), null); |
| 66 | +}); |
| 67 | + |
| 68 | +test("chooseZhCnBundleName prefers bundle referenced by entry js", () => { |
| 69 | + const entryJs = 'const x = "./zh-CN-B4K5_9qj.js";'; |
| 70 | + const picked = chooseZhCnBundleName({ |
| 71 | + assetNames: ["zh-CN-AAAA.js", "zh-CN-B4K5_9qj.js"], |
| 72 | + entryJs, |
| 73 | + }); |
| 74 | + assert.equal(picked, "zh-CN-B4K5_9qj.js"); |
| 75 | +}); |
| 76 | + |
| 77 | +test("chooseZhCnBundleName falls back when only one zh-CN asset exists", () => { |
| 78 | + const picked = chooseZhCnBundleName({ |
| 79 | + assetNames: ["zh-CN-only.js"], |
| 80 | + entryJs: "const x = 1;", |
| 81 | + }); |
| 82 | + assert.equal(picked, "zh-CN-only.js"); |
| 83 | +}); |
| 84 | + |
| 85 | +test("chooseZhCnBundleName fails when ambiguous and no entry reference", () => { |
| 86 | + assert.throws( |
| 87 | + () => |
| 88 | + chooseZhCnBundleName({ |
| 89 | + assetNames: ["zh-CN-a.js", "zh-CN-b.js"], |
| 90 | + entryJs: "const x = 1;", |
| 91 | + }), |
| 92 | + /Ambiguous zh-CN bundle/ |
| 93 | + ); |
| 94 | +}); |
| 95 | + |
| 96 | +test("chooseZhCnBundleName fails when entry references multiple zh-CN bundles", () => { |
| 97 | + const entryJs = '"./zh-CN-a.js";"./zh-CN-b.js";'; |
| 98 | + assert.throws( |
| 99 | + () => |
| 100 | + chooseZhCnBundleName({ |
| 101 | + assetNames: ["zh-CN-a.js", "zh-CN-b.js"], |
| 102 | + entryJs, |
| 103 | + }), |
| 104 | + /Ambiguous zh-CN bundle/ |
| 105 | + ); |
| 106 | +}); |
| 107 | + |
| 108 | +test("chooseZhCnBundleName allows fallback when strictTarget=false", () => { |
| 109 | + const picked = chooseZhCnBundleName({ |
| 110 | + assetNames: ["zh-CN-b.js", "zh-CN-a.js"], |
| 111 | + entryJs: "const x = 1;", |
| 112 | + strictTarget: false, |
| 113 | + }); |
| 114 | + assert.equal(picked, "zh-CN-a.js"); |
| 115 | +}); |
| 116 | + |
| 117 | +test("chooseZhCnBundleName returns null when no zh-CN assets exist", () => { |
| 118 | + const picked = chooseZhCnBundleName({ |
| 119 | + assetNames: ["en-US.js"], |
| 120 | + entryJs: "const x = 1;", |
| 121 | + }); |
| 122 | + assert.equal(picked, null); |
| 123 | +}); |
0 commit comments