upgrade.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import * as fs from 'node:fs';
  3. import * as path from 'node:path';
  4. import * as os from 'node:os';
  5. import {
  6. detectInstallMethod,
  7. deriveInstallDir,
  8. parseSemver,
  9. compareVersions,
  10. isUpdateAvailable,
  11. normalizeVersion,
  12. stripV,
  13. parseLatestTagFromLocation,
  14. reindexAdvisory,
  15. runUpgrade,
  16. buildWindowsUpgradeScript,
  17. NPM_PACKAGE,
  18. type InstallMethod,
  19. type UpgradeDeps,
  20. } from '../src/upgrade';
  21. import { EXTRACTION_VERSION } from '../src/extraction/extraction-version';
  22. import { CodeGraph } from '../src';
  23. // ---------------------------------------------------------------------------
  24. // detectInstallMethod — structural detection from the running file's path
  25. // ---------------------------------------------------------------------------
  26. describe('detectInstallMethod', () => {
  27. // A bundle exists if a vendored node + launcher sit next to lib/.
  28. function bundleExists(present: Set<string>) {
  29. return (p: string) => present.has(p.replace(/\\/g, '/'));
  30. }
  31. it('detects a unix bundle and derives the install dir from the versions/ layout', () => {
  32. const root = '/home/u/.codegraph/versions/v0.9.9';
  33. const filename = `${root}/lib/dist/bin/codegraph.js`;
  34. const present = new Set([`${root}/node`, `${root}/bin/codegraph`, '/home/u/.codegraph']);
  35. const m = detectInstallMethod({
  36. filename,
  37. platform: 'linux',
  38. cwd: '/home/u/project',
  39. exists: bundleExists(present),
  40. });
  41. expect(m).toEqual({
  42. kind: 'bundle',
  43. os: 'unix',
  44. bundleRoot: root,
  45. installDir: '/home/u/.codegraph',
  46. });
  47. });
  48. it('detects a windows bundle and derives the install dir from current\\', () => {
  49. const root = 'C:/Users/u/AppData/Local/codegraph/current';
  50. const filename = `${root}/lib/dist/bin/codegraph.js`;
  51. const present = new Set([`${root}/node.exe`, `${root}/bin/codegraph.cmd`]);
  52. const m = detectInstallMethod({
  53. filename,
  54. platform: 'win32',
  55. cwd: 'C:/Users/u/project',
  56. exists: bundleExists(present),
  57. }) as Extract<InstallMethod, { kind: 'bundle' }>;
  58. expect(m.kind).toBe('bundle');
  59. expect(m.os).toBe('windows');
  60. // win32 path math emits backslashes; compare separator-independently.
  61. expect(m.installDir?.replace(/\\/g, '/')).toBe('C:/Users/u/AppData/Local/codegraph');
  62. });
  63. it('detects a global npm install', () => {
  64. const filename = '/usr/local/lib/node_modules/@colbymchenry/codegraph/dist/bin/codegraph.js';
  65. const m = detectInstallMethod({
  66. filename,
  67. platform: 'linux',
  68. cwd: '/home/u/project',
  69. exists: () => false,
  70. });
  71. expect(m).toEqual({ kind: 'npm', scope: 'global' });
  72. });
  73. it('detects a local (project) npm install as local', () => {
  74. const cwd = '/home/u/project';
  75. const filename = `${cwd}/node_modules/@colbymchenry/codegraph/dist/bin/codegraph.js`;
  76. const m = detectInstallMethod({ filename, platform: 'linux', cwd, exists: () => false });
  77. expect(m).toEqual({ kind: 'npm', scope: 'local' });
  78. });
  79. it('detects an npx run from the _npx cache', () => {
  80. const filename = '/home/u/.npm/_npx/abc123/node_modules/@colbymchenry/codegraph/dist/bin/codegraph.js';
  81. const m = detectInstallMethod({ filename, platform: 'linux', cwd: '/home/u', exists: () => false });
  82. expect(m).toEqual({ kind: 'npx' });
  83. });
  84. it('detects a source checkout via sibling package.json + .git', () => {
  85. const repo = '/home/u/dev/codegraph';
  86. const filename = `${repo}/dist/bin/codegraph.js`;
  87. const present = new Set([`${repo}/package.json`, `${repo}/.git`]);
  88. const m = detectInstallMethod({
  89. filename,
  90. platform: 'darwin',
  91. cwd: repo,
  92. exists: bundleExists(present),
  93. });
  94. expect(m).toEqual({ kind: 'source', root: repo });
  95. });
  96. it('returns unknown for an unrecognized layout', () => {
  97. const m = detectInstallMethod({
  98. filename: '/opt/weird/place/codegraph.js',
  99. platform: 'linux',
  100. cwd: '/tmp',
  101. exists: () => false,
  102. });
  103. expect(m.kind).toBe('unknown');
  104. });
  105. });
  106. describe('deriveInstallDir', () => {
  107. it('unix: returns the dir above versions/', () => {
  108. expect(deriveInstallDir('/a/b/.codegraph/versions/v1.2.3', 'unix', () => true)).toBe('/a/b/.codegraph');
  109. });
  110. it('unix: null when not under versions/', () => {
  111. expect(deriveInstallDir('/a/b/somewhere', 'unix', () => true)).toBeNull();
  112. });
  113. it('windows: returns the parent of current\\', () => {
  114. expect(deriveInstallDir('C:/x/codegraph/current', 'windows', () => true)?.replace(/\\/g, '/')).toBe('C:/x/codegraph');
  115. });
  116. it('windows: null when basename is not current', () => {
  117. expect(deriveInstallDir('C:/x/codegraph/v1', 'windows', () => true)).toBeNull();
  118. });
  119. });
  120. // ---------------------------------------------------------------------------
  121. // version helpers
  122. // ---------------------------------------------------------------------------
  123. describe('version helpers', () => {
  124. it('parseSemver handles v-prefix and prerelease', () => {
  125. expect(parseSemver('v1.2.3')).toEqual({ major: 1, minor: 2, patch: 3, pre: null });
  126. expect(parseSemver('1.2.3-rc.1')).toEqual({ major: 1, minor: 2, patch: 3, pre: 'rc.1' });
  127. expect(parseSemver('not-a-version')).toBeNull();
  128. });
  129. it('compareVersions orders correctly incl. prerelease < release', () => {
  130. expect(compareVersions('1.0.1', '1.0.0')).toBeGreaterThan(0);
  131. expect(compareVersions('1.0.0', '1.1.0')).toBeLessThan(0);
  132. expect(compareVersions('v2.0.0', '2.0.0')).toBe(0);
  133. expect(compareVersions('1.0.0-rc.1', '1.0.0')).toBeLessThan(0);
  134. });
  135. it('isUpdateAvailable compares, and falls back to string-inequality for unparseable', () => {
  136. expect(isUpdateAvailable('0.9.8', '0.9.9')).toBe(true);
  137. expect(isUpdateAvailable('0.9.9', '0.9.9')).toBe(false);
  138. expect(isUpdateAvailable('0.9.9', '0.9.8')).toBe(false);
  139. // dev sentinel can't parse → any difference means "update available"
  140. expect(isUpdateAvailable('0.0.0-unknown', '0.9.9')).toBe(true);
  141. });
  142. it('normalizeVersion / stripV round-trip', () => {
  143. expect(normalizeVersion('0.9.9')).toBe('v0.9.9');
  144. expect(normalizeVersion('v0.9.9')).toBe('v0.9.9');
  145. expect(stripV('v0.9.9')).toBe('0.9.9');
  146. expect(stripV('0.9.9')).toBe('0.9.9');
  147. });
  148. it('parseLatestTagFromLocation extracts the tag from a releases redirect', () => {
  149. expect(parseLatestTagFromLocation('https://github.com/colbymchenry/codegraph/releases/tag/v0.9.9')).toBe('v0.9.9');
  150. expect(parseLatestTagFromLocation('https://github.com/o/r/releases/tag/v1.2.3?foo=bar')).toBe('v1.2.3');
  151. expect(parseLatestTagFromLocation(undefined)).toBeNull();
  152. expect(parseLatestTagFromLocation('https://github.com/o/r/releases')).toBeNull();
  153. });
  154. it('reindexAdvisory mentions the refresh commands', () => {
  155. const a = reindexAdvisory();
  156. expect(a).toContain('codegraph sync');
  157. expect(a).toContain('codegraph index -f');
  158. });
  159. it('buildWindowsUpgradeScript targets the right asset per arch and renames-not-deletes the exe', () => {
  160. const arm = buildWindowsUpgradeScript('C:\\cg\\current', 'v1.2.3', 'arm64');
  161. expect(arm).toContain('releases/download/v1.2.3/codegraph-win32-arm64.zip');
  162. expect(arm).toContain("$dest='C:\\cg\\current'");
  163. expect(arm).toContain('Rename-Item'); // never Remove-Item on the locked exe
  164. expect(arm).not.toMatch(/Remove-Item[^;]*\$dest'?\s*;/); // doesn't delete current\
  165. const x64 = buildWindowsUpgradeScript('C:\\cg\\current', 'v1.2.3', 'x64');
  166. expect(x64).toContain('codegraph-win32-x64.zip');
  167. });
  168. });
  169. // ---------------------------------------------------------------------------
  170. // runUpgrade orchestration — mocked side-effects
  171. // ---------------------------------------------------------------------------
  172. interface Calls {
  173. runs: Array<{ cmd: string; args: string[]; env?: NodeJS.ProcessEnv }>;
  174. logs: string[];
  175. errors: string[];
  176. }
  177. function makeDeps(
  178. overrides: Partial<UpgradeDeps> & { method: InstallMethod; currentVersion: string },
  179. runExit = 0
  180. ): { deps: UpgradeDeps; calls: Calls } {
  181. const calls: Calls = { runs: [], logs: [], errors: [] };
  182. const deps: UpgradeDeps = {
  183. currentVersion: overrides.currentVersion,
  184. method: overrides.method,
  185. resolveLatest: overrides.resolveLatest ?? (async () => 'v0.9.9'),
  186. run: (cmd, args, env) => {
  187. calls.runs.push({ cmd, args, env });
  188. return runExit;
  189. },
  190. hasCommand: overrides.hasCommand ?? ((c) => c === 'curl'),
  191. log: (m) => calls.logs.push(m),
  192. warn: (m) => calls.logs.push(m),
  193. error: (m) => calls.errors.push(m),
  194. platform: overrides.platform ?? 'linux',
  195. };
  196. return { deps, calls };
  197. }
  198. /** Decode a `-EncodedCommand` base64 (UTF-16LE) payload back to its script. */
  199. function decodeEncodedCommand(args: string[]): string {
  200. const i = args.indexOf('-EncodedCommand');
  201. if (i < 0) throw new Error('no -EncodedCommand in args');
  202. return Buffer.from(args[i + 1]!, 'base64').toString('utf16le');
  203. }
  204. describe('runUpgrade', () => {
  205. it('does nothing when already up to date', async () => {
  206. const { deps, calls } = makeDeps({ method: { kind: 'npm', scope: 'global' }, currentVersion: '0.9.9' });
  207. const code = await runUpgrade({}, deps);
  208. expect(code).toBe(0);
  209. expect(calls.runs).toHaveLength(0);
  210. expect(calls.logs.join('\n')).toMatch(/up to date/i);
  211. });
  212. it('--check reports an available update without running anything', async () => {
  213. const { deps, calls } = makeDeps({
  214. method: { kind: 'npm', scope: 'global' },
  215. currentVersion: '0.9.8',
  216. });
  217. const code = await runUpgrade({ check: true }, deps);
  218. expect(code).toBe(0);
  219. expect(calls.runs).toHaveLength(0);
  220. expect(calls.logs.join('\n')).toMatch(/update is available/i);
  221. });
  222. it('unix bundle: runs the installer via sh with the derived install dir', async () => {
  223. const { deps, calls } = makeDeps({
  224. method: { kind: 'bundle', os: 'unix', bundleRoot: '/h/.codegraph/versions/v0.9.8', installDir: '/h/.codegraph' },
  225. currentVersion: '0.9.8',
  226. });
  227. const code = await runUpgrade({}, deps);
  228. expect(code).toBe(0);
  229. expect(calls.runs).toHaveLength(1);
  230. expect(calls.runs[0].cmd).toBe('sh');
  231. expect(calls.runs[0].args[0]).toBe('-c');
  232. expect(calls.runs[0].args[1]).toContain('curl -fsSL');
  233. expect(calls.runs[0].args[1]).toContain('| sh');
  234. expect(calls.runs[0].env?.CODEGRAPH_INSTALL_DIR).toBe('/h/.codegraph');
  235. expect(calls.logs.join('\n')).toMatch(/codegraph sync/); // re-index advisory printed
  236. });
  237. it('unix bundle: falls back to wget, and errors when neither downloader exists', async () => {
  238. const { deps, calls } = makeDeps({
  239. method: { kind: 'bundle', os: 'unix', bundleRoot: '/h/.codegraph/versions/v0.9.8', installDir: null },
  240. currentVersion: '0.9.8',
  241. hasCommand: () => false,
  242. });
  243. const code = await runUpgrade({}, deps);
  244. expect(code).toBe(1);
  245. expect(calls.runs).toHaveLength(0);
  246. expect(calls.errors.join('\n')).toMatch(/curl nor wget/i);
  247. });
  248. it('windows bundle: runs a synchronous in-place (rename + extract) powershell upgrade', async () => {
  249. const { deps, calls } = makeDeps({
  250. method: { kind: 'bundle', os: 'windows', bundleRoot: 'C:/x/codegraph/current', installDir: 'C:/x/codegraph' },
  251. currentVersion: '0.9.8',
  252. platform: 'win32',
  253. });
  254. const code = await runUpgrade({}, deps);
  255. expect(code).toBe(0);
  256. expect(calls.runs).toHaveLength(1);
  257. expect(calls.runs[0].cmd).toBe('powershell.exe');
  258. const decoded = decodeEncodedCommand(calls.runs[0].args);
  259. // Downloads the right asset, renames the locked exe aside, copies over current\.
  260. expect(decoded).toContain('releases/download/v0.9.9/codegraph-win32-');
  261. expect(decoded).toContain('Rename-Item');
  262. expect(decoded).toContain('node.exe.old-');
  263. expect(decoded).toContain('Copy-Item');
  264. });
  265. it('windows bundle: a non-zero installer exit is a failure', async () => {
  266. const { deps, calls } = makeDeps(
  267. {
  268. method: { kind: 'bundle', os: 'windows', bundleRoot: 'C:/x/codegraph/current', installDir: 'C:/x/codegraph' },
  269. currentVersion: '0.9.8',
  270. platform: 'win32',
  271. },
  272. 1
  273. );
  274. const code = await runUpgrade({}, deps);
  275. expect(code).toBe(1);
  276. expect(calls.errors.join('\n')).toMatch(/exited with code/i);
  277. });
  278. it('npm global: shells out to npm install -g @pkg@latest', async () => {
  279. const { deps, calls } = makeDeps({
  280. method: { kind: 'npm', scope: 'global' },
  281. currentVersion: '0.9.8',
  282. });
  283. const code = await runUpgrade({}, deps);
  284. expect(code).toBe(0);
  285. expect(calls.runs[0].cmd).toBe('npm');
  286. expect(calls.runs[0].args).toEqual(['install', '-g', `${NPM_PACKAGE}@latest`]);
  287. });
  288. it('npm on win32 uses npm.cmd', async () => {
  289. const { deps, calls } = makeDeps({
  290. method: { kind: 'npm', scope: 'global' },
  291. currentVersion: '0.9.8',
  292. platform: 'win32',
  293. });
  294. await runUpgrade({}, deps);
  295. expect(calls.runs[0].cmd).toBe('npm.cmd');
  296. });
  297. it('npm: a pinned version is passed through as @<version>', async () => {
  298. const { deps, calls } = makeDeps({
  299. method: { kind: 'npm', scope: 'global' },
  300. currentVersion: '0.9.9',
  301. });
  302. await runUpgrade({ version: '0.9.8' }, deps);
  303. // npm spec carries no leading "v".
  304. expect(calls.runs[0].args).toEqual(['install', '-g', `${NPM_PACKAGE}@0.9.8`]);
  305. });
  306. it('npm: surfaces a non-zero exit as failure', async () => {
  307. const { deps, calls } = makeDeps(
  308. { method: { kind: 'npm', scope: 'global' }, currentVersion: '0.9.8' },
  309. 1
  310. );
  311. const code = await runUpgrade({}, deps);
  312. expect(code).toBe(1);
  313. expect(calls.errors.join('\n')).toMatch(/npm exited/i);
  314. });
  315. it('npx: nothing to upgrade', async () => {
  316. const { deps, calls } = makeDeps({ method: { kind: 'npx' }, currentVersion: '0.9.8' });
  317. const code = await runUpgrade({}, deps);
  318. expect(code).toBe(0);
  319. expect(calls.runs).toHaveLength(0);
  320. expect(calls.logs.join('\n')).toMatch(/nothing to upgrade/i);
  321. });
  322. it('source: tells the user to git pull, runs nothing', async () => {
  323. const { deps, calls } = makeDeps({
  324. method: { kind: 'source', root: '/dev/codegraph' },
  325. currentVersion: '0.9.8',
  326. });
  327. const code = await runUpgrade({}, deps);
  328. expect(code).toBe(0);
  329. expect(calls.runs).toHaveLength(0);
  330. expect(calls.logs.join('\n')).toMatch(/git pull/);
  331. });
  332. });
  333. // ---------------------------------------------------------------------------
  334. // Re-index staleness — real index, real metadata stamp
  335. // ---------------------------------------------------------------------------
  336. describe('index extraction-version stamp / isIndexStale', () => {
  337. let dir: string;
  338. beforeEach(() => {
  339. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-upgrade-stamp-'));
  340. });
  341. afterEach(() => {
  342. fs.rmSync(dir, { recursive: true, force: true });
  343. });
  344. it('stamps the current extraction version on full index and is not stale', async () => {
  345. fs.writeFileSync(path.join(dir, 'a.ts'), 'export function hello() { return 1; }\n');
  346. const cg = await CodeGraph.init(dir, { index: false });
  347. // No index yet → not stale (nothing to refresh).
  348. expect(cg.isIndexStale()).toBe(false);
  349. await cg.indexAll();
  350. const info = cg.getIndexBuildInfo();
  351. expect(info.extractionVersion).toBe(EXTRACTION_VERSION);
  352. expect(typeof info.version).toBe('string');
  353. expect(cg.isIndexStale()).toBe(false);
  354. cg.destroy();
  355. });
  356. it('flags an index stamped by an older extraction version as stale', async () => {
  357. fs.writeFileSync(path.join(dir, 'a.ts'), 'export function hello() { return 1; }\n');
  358. const cg = await CodeGraph.init(dir, { index: false });
  359. await cg.indexAll();
  360. // Simulate an index built by an older engine.
  361. (cg as unknown as { queries: { setMetadata(k: string, v: string): void } }).queries.setMetadata(
  362. 'indexed_with_extraction_version',
  363. String(EXTRACTION_VERSION - 1)
  364. );
  365. expect(cg.isIndexStale()).toBe(true);
  366. cg.destroy();
  367. });
  368. });