|
| 1 | +#!/usr/bin/env node |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const assert = require("node:assert/strict"); |
| 5 | +const fs = require("node:fs"); |
| 6 | +const os = require("node:os"); |
| 7 | +const path = require("node:path"); |
| 8 | +const test = require("node:test"); |
| 9 | +const vm = require("node:vm"); |
| 10 | + |
| 11 | +const { |
| 12 | + loadLinuxFeaturePatchDescriptors, |
| 13 | +} = require("../../scripts/lib/linux-features.js"); |
| 14 | +const { patchAssetFiles } = require("../../scripts/patches/lib/assets.js"); |
| 15 | +const { |
| 16 | + applyProjectGroupLastUpdatedSortPatch, |
| 17 | + descriptors, |
| 18 | +} = require("./patch.js"); |
| 19 | + |
| 20 | +const currentProjectSource = [ |
| 21 | + "function ue(e,t){let n=new Map(t.map((e,t)=>[e,t]));return[...e].sort((e,t)=>(n.get(e.projectId)??2**53-1)-(n.get(t.projectId)??2**53-1))}", |
| 22 | + "function Re(e,t){let n=e.projectUpdatedAt??0;for(let r of e.threadKeys)n=Math.max(n,t.get(r)??0);return n}", |
| 23 | + "function Fe({groups:e,items:t,projectOrder:n}){let r=new Map(t.map(e=>[e.task.key,e.recencyAt]));return ue(e.map((e,t)=>({group:e,index:t,recencyAt:Re(e,r)})).sort((e,t)=>t.recencyAt-e.recencyAt||e.index-t.index).map(({group:e})=>e),n)}", |
| 24 | + "const prioritySortId=`sidebarElectron.sortMenu.priority`;", |
| 25 | + "const updatedSortId=`sidebarElectron.sortMenu.updated`;", |
| 26 | + "const manualSortId=`sidebarElectron.sortMenu.manual`;", |
| 27 | + "T=Fe({groups:Pe({groups:S,items:c}),items:c,projectOrder:f(t,o.PROJECT_ORDER)});", |
| 28 | +].join(""); |
| 29 | + |
| 30 | +function captureWarns(fn) { |
| 31 | + const originalWarn = console.warn; |
| 32 | + const warnings = []; |
| 33 | + console.warn = (message) => warnings.push(message); |
| 34 | + try { |
| 35 | + return { value: fn(), warnings }; |
| 36 | + } finally { |
| 37 | + console.warn = originalWarn; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function applyPatchTwice(source) { |
| 42 | + const patched = applyProjectGroupLastUpdatedSortPatch(source); |
| 43 | + const { value: secondPass, warnings } = captureWarns(() => |
| 44 | + applyProjectGroupLastUpdatedSortPatch(patched), |
| 45 | + ); |
| 46 | + assert.equal(secondPass, patched); |
| 47 | + assert.deepEqual(warnings, []); |
| 48 | + return patched; |
| 49 | +} |
| 50 | + |
| 51 | +function withFeatureConfig(enabled, fn) { |
| 52 | + const originalConfig = process.env.CODEX_LINUX_FEATURES_CONFIG; |
| 53 | + const tempDir = fs.mkdtempSync( |
| 54 | + path.join(os.tmpdir(), "project-group-last-updated-sort-"), |
| 55 | + ); |
| 56 | + process.env.CODEX_LINUX_FEATURES_CONFIG = path.join(tempDir, "features.json"); |
| 57 | + try { |
| 58 | + fs.writeFileSync(process.env.CODEX_LINUX_FEATURES_CONFIG, JSON.stringify({ enabled })); |
| 59 | + return fn(); |
| 60 | + } finally { |
| 61 | + if (originalConfig == null) { |
| 62 | + delete process.env.CODEX_LINUX_FEATURES_CONFIG; |
| 63 | + } else { |
| 64 | + process.env.CODEX_LINUX_FEATURES_CONFIG = originalConfig; |
| 65 | + } |
| 66 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function evaluateGroupSorter(source) { |
| 71 | + const context = {}; |
| 72 | + const sorterSource = source.slice(0, source.indexOf("const prioritySortId")); |
| 73 | + vm.runInNewContext(`${sorterSource};globalThis.sortProjectGroups=Fe`, context); |
| 74 | + return context.sortProjectGroups; |
| 75 | +} |
| 76 | + |
| 77 | +test("feature is disabled until selected", () => { |
| 78 | + const featuresRoot = path.resolve(__dirname, ".."); |
| 79 | + withFeatureConfig([], () => { |
| 80 | + assert.equal( |
| 81 | + loadLinuxFeaturePatchDescriptors({ featuresRoot }).some( |
| 82 | + (descriptor) => |
| 83 | + descriptor.id === |
| 84 | + "feature:project-group-last-updated-sort:last-updated-project-groups", |
| 85 | + ), |
| 86 | + false, |
| 87 | + ); |
| 88 | + }); |
| 89 | + withFeatureConfig(["project-group-last-updated-sort"], () => { |
| 90 | + assert.equal( |
| 91 | + loadLinuxFeaturePatchDescriptors({ featuresRoot }).some( |
| 92 | + (descriptor) => |
| 93 | + descriptor.id === |
| 94 | + "feature:project-group-last-updated-sort:last-updated-project-groups", |
| 95 | + ), |
| 96 | + true, |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +test("Last updated sorts project groups by their newest task", () => { |
| 102 | + const patched = applyPatchTwice(currentProjectSource); |
| 103 | + const sortProjectGroups = evaluateGroupSorter(patched); |
| 104 | + const groups = [ |
| 105 | + { projectId: "nix", threadKeys: ["nix-task"] }, |
| 106 | + { projectId: "delta", threadKeys: ["delta-task"] }, |
| 107 | + { projectId: "multi", threadKeys: ["multi-task"] }, |
| 108 | + { projectId: "chezmoi", threadKeys: ["chezmoi-task"] }, |
| 109 | + { projectId: "tapas", threadKeys: ["tapas-task"] }, |
| 110 | + ]; |
| 111 | + const items = [ |
| 112 | + { task: { key: "nix-task" }, recencyAt: 1 }, |
| 113 | + { task: { key: "delta-task" }, recencyAt: 2 }, |
| 114 | + { task: { key: "multi-task" }, recencyAt: 3 }, |
| 115 | + { task: { key: "chezmoi-task" }, recencyAt: 4 }, |
| 116 | + { task: { key: "tapas-task" }, recencyAt: 5 }, |
| 117 | + ]; |
| 118 | + const projectOrder = ["nix", "delta", "multi", "chezmoi", "tapas"]; |
| 119 | + |
| 120 | + assert.deepEqual( |
| 121 | + Array.from( |
| 122 | + sortProjectGroups({ groups, items, projectOrder, sortMode: "updated_at" }), |
| 123 | + (group) => group.projectId, |
| 124 | + ), |
| 125 | + ["tapas", "chezmoi", "multi", "delta", "nix"], |
| 126 | + ); |
| 127 | +}); |
| 128 | + |
| 129 | +test("non-updated modes preserve the upstream saved project order", () => { |
| 130 | + const patched = applyPatchTwice(currentProjectSource); |
| 131 | + const sortProjectGroups = evaluateGroupSorter(patched); |
| 132 | + const groups = [ |
| 133 | + { projectId: "newer", threadKeys: ["newer-task"] }, |
| 134 | + { projectId: "older", threadKeys: ["older-task"] }, |
| 135 | + ]; |
| 136 | + const items = [ |
| 137 | + { task: { key: "newer-task" }, recencyAt: 2 }, |
| 138 | + { task: { key: "older-task" }, recencyAt: 1 }, |
| 139 | + ]; |
| 140 | + const projectOrder = ["older", "newer"]; |
| 141 | + |
| 142 | + for (const sortMode of ["manual", "priority"]) { |
| 143 | + assert.deepEqual( |
| 144 | + Array.from( |
| 145 | + sortProjectGroups({ groups, items, projectOrder, sortMode }), |
| 146 | + (group) => group.projectId, |
| 147 | + ), |
| 148 | + ["older", "newer"], |
| 149 | + ); |
| 150 | + } |
| 151 | +}); |
| 152 | + |
| 153 | +test("patch passes the selected project sort mode into the group sorter", () => { |
| 154 | + const patched = applyPatchTwice(currentProjectSource); |
| 155 | + assert.ok( |
| 156 | + patched.includes( |
| 157 | + "projectOrder:f(t,o.PROJECT_ORDER),sortMode:t(C).projectSortMode", |
| 158 | + ), |
| 159 | + ); |
| 160 | +}); |
| 161 | + |
| 162 | +test("drift leaves the asset byte-identical", () => { |
| 163 | + const source = currentProjectSource.replace( |
| 164 | + "function Fe({groups:e,items:t,projectOrder:n})", |
| 165 | + "function Fe({groups:e,items:t,projectOrder:n,unknown:o})", |
| 166 | + ); |
| 167 | + const { value, warnings } = captureWarns(() => |
| 168 | + applyProjectGroupLastUpdatedSortPatch(source), |
| 169 | + ); |
| 170 | + |
| 171 | + assert.equal(value, source); |
| 172 | + assert.equal(warnings.length, 1); |
| 173 | + assert.match(warnings[0], /project group sorting insertion points/); |
| 174 | +}); |
| 175 | + |
| 176 | +test("missing current call site leaves the asset byte-identical", () => { |
| 177 | + const source = currentProjectSource.replace( |
| 178 | + "projectOrder:f(t,o.PROJECT_ORDER)", |
| 179 | + "projectOrder:unknownProjectOrder", |
| 180 | + ); |
| 181 | + const { value, warnings } = captureWarns(() => |
| 182 | + applyProjectGroupLastUpdatedSortPatch(source), |
| 183 | + ); |
| 184 | + |
| 185 | + assert.equal(value, source); |
| 186 | + assert.equal(warnings.length, 1); |
| 187 | + assert.match(warnings[0], /project group sorting insertion points/); |
| 188 | +}); |
| 189 | + |
| 190 | +test("mixed patched and clean helpers are rejected byte-identically", () => { |
| 191 | + const mixed = `${applyProjectGroupLastUpdatedSortPatch( |
| 192 | + currentProjectSource, |
| 193 | + )}${currentProjectSource}`; |
| 194 | + const { value, warnings } = captureWarns(() => |
| 195 | + applyProjectGroupLastUpdatedSortPatch(mixed), |
| 196 | + ); |
| 197 | + |
| 198 | + assert.equal(value, mixed); |
| 199 | + assert.equal(warnings.length, 1); |
| 200 | + assert.match(warnings[0], /project group sorting insertion points/); |
| 201 | +}); |
| 202 | + |
| 203 | +test("descriptor targets and patches only the current project sidebar chunk", () => { |
| 204 | + const tempDir = fs.mkdtempSync( |
| 205 | + path.join(os.tmpdir(), "project-group-last-updated-sort-assets-"), |
| 206 | + ); |
| 207 | + try { |
| 208 | + const assetsDir = path.join(tempDir, "webview", "assets"); |
| 209 | + const assetPath = path.join( |
| 210 | + assetsDir, |
| 211 | + "app-initial~app-main~onboarding-page~projects-index-page~quick-chat-window-page~codex-micro~iqsnin5k-demo.js", |
| 212 | + ); |
| 213 | + fs.mkdirSync(assetsDir, { recursive: true }); |
| 214 | + fs.writeFileSync(assetPath, currentProjectSource); |
| 215 | + |
| 216 | + const result = patchAssetFiles( |
| 217 | + tempDir, |
| 218 | + descriptors[0].pattern, |
| 219 | + descriptors[0].apply, |
| 220 | + "missing", |
| 221 | + ); |
| 222 | + |
| 223 | + assert.deepEqual(result, { matched: 1, changed: 1 }); |
| 224 | + assert.notEqual(fs.readFileSync(assetPath, "utf8"), currentProjectSource); |
| 225 | + assert.equal( |
| 226 | + descriptors[0].pattern.test( |
| 227 | + "app-initial~app-main~projects-index-page~remote-conversation-page-old.js", |
| 228 | + ), |
| 229 | + false, |
| 230 | + ); |
| 231 | + } finally { |
| 232 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 233 | + } |
| 234 | +}); |
0 commit comments