session-diff-fallback.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { execFileSync } from 'node:child_process';
  2. import { mkdtemp, mkdir, writeFile, appendFile, rm } from 'node:fs/promises';
  3. import { tmpdir } from 'node:os';
  4. import path from 'node:path';
  5. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
  6. import { closeLocalRuntimeDb } from '../../src/persistence/db.js';
  7. import { SqliteLocalTurnDiffStore } from '../../src/persistence/sqlite-persistence.js';
  8. import { readLocalSessionDiff } from '../../src/turns/diff-api.js';
  9. let root: string;
  10. let workspaceDir: string;
  11. let dataDir: string;
  12. let diffStore: SqliteLocalTurnDiffStore;
  13. function git(...args: string[]): string {
  14. return execFileSync('git', args, { cwd: workspaceDir, encoding: 'utf8' });
  15. }
  16. async function write(file: string, content: string | Buffer): Promise<void> {
  17. await mkdir(path.dirname(path.join(workspaceDir, file)), { recursive: true });
  18. await writeFile(path.join(workspaceDir, file), content);
  19. }
  20. function commit(): void {
  21. git('add', '--all');
  22. git('commit', '-qm', 'Synthetic baseline');
  23. }
  24. async function read(messageId?: string) {
  25. return readLocalSessionDiff({
  26. diffStore,
  27. session: { sessionId: 'test-session', workspaceDir },
  28. messageId,
  29. });
  30. }
  31. beforeEach(async () => {
  32. root = await mkdtemp(path.join(tmpdir(), 'session-diff-'));
  33. workspaceDir = path.join(root, 'workspace');
  34. dataDir = path.join(root, 'data');
  35. await mkdir(workspaceDir);
  36. // Keep user hooks, attributes, diff drivers and quoting defaults out of fixtures.
  37. vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');
  38. vi.stubEnv('GIT_CONFIG_GLOBAL', path.join(root, 'no-global-config'));
  39. for (const key of ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_PREFIX']) {
  40. vi.stubEnv(key, undefined);
  41. }
  42. git('init', '-q');
  43. git('config', 'user.name', 'Diff Test');
  44. git('config', 'user.email', 'diff-test@example.com');
  45. git('config', 'core.autocrlf', 'false');
  46. git('config', 'diff.renames', 'true');
  47. await write('baseline.txt', 'unchanged\n');
  48. commit();
  49. diffStore = new SqliteLocalTurnDiffStore(dataDir);
  50. });
  51. afterEach(async () => {
  52. closeLocalRuntimeDb(dataDir);
  53. vi.unstubAllEnvs();
  54. await rm(root, { recursive: true, force: true });
  55. });
  56. const portableNames = ['中文.txt', 'with spaces.txt', 'dir/{before after}.txt'];
  57. const posixNames = [
  58. 'tab\tname.txt',
  59. 'line\nname.txt',
  60. 'literal => arrow.txt',
  61. ' quote"slash\\.txt',
  62. ' trailing.txt ',
  63. ];
  64. describe.each(['true', 'false'])('session diff fallback with core.quotePath=%s', (quotePath) => {
  65. beforeEach(() => {
  66. git('config', 'core.quotePath', quotePath);
  67. });
  68. it('preserves Unicode paths and attaches each tracked patch through the public API', async () => {
  69. for (const file of portableNames) await write(file, 'first\n');
  70. commit();
  71. for (const [index, file] of portableNames.entries()) {
  72. await appendFile(path.join(workspaceDir, file), `added-${index}\n`);
  73. }
  74. const result = await read();
  75. expect(result.diffs).toHaveLength(portableNames.length);
  76. for (const [index, file] of portableNames.entries()) {
  77. expect(result.diffs.find((entry) => entry.file === file)).toMatchObject({
  78. file,
  79. additions: 1,
  80. deletions: 0,
  81. status: 'modified',
  82. diff: expect.stringContaining(`+added-${index}\n`),
  83. });
  84. }
  85. });
  86. it('reports rename edits against the destination path', async () => {
  87. await write('before.txt', 'a\nb\nc\nd\ne\nf\n');
  88. commit();
  89. git('mv', 'before.txt', 'after.txt');
  90. await appendFile(path.join(workspaceDir, 'after.txt'), 'added\n');
  91. expect((await read('missing-message')).diffs).toEqual([
  92. {
  93. file: 'after.txt',
  94. additions: 1,
  95. deletions: 0,
  96. status: 'renamed',
  97. diff: expect.stringContaining('+added\n'),
  98. },
  99. ]);
  100. });
  101. it('keeps additions, deletions, binary changes and untracked paths distinct', async () => {
  102. await write('deleted 中文.txt', 'remove\n');
  103. await write('binary.bin', Buffer.from([0, 1, 2]));
  104. commit();
  105. await rm(path.join(workspaceDir, 'deleted 中文.txt'));
  106. await write('binary.bin', Buffer.from([0, 3, 4]));
  107. await write('staged 中文.txt', 'staged\n');
  108. git('add', '--', 'staged 中文.txt');
  109. for (const file of portableNames) await write(file, 'untracked\n++content\n');
  110. await write('untracked.bin', Buffer.from([0, 5, 6]));
  111. await write('empty.txt', '');
  112. const { diffs } = await read();
  113. expect(diffs).toHaveLength(portableNames.length + 5);
  114. expect(diffs.find((entry) => entry.file === 'deleted 中文.txt')).toMatchObject({
  115. additions: 0,
  116. deletions: 1,
  117. status: 'deleted',
  118. diff: expect.stringContaining('-remove\n'),
  119. });
  120. expect(diffs.find((entry) => entry.file === 'staged 中文.txt')).toMatchObject({
  121. additions: 1,
  122. deletions: 0,
  123. status: 'added',
  124. diff: expect.stringContaining('+staged\n'),
  125. });
  126. for (const file of ['binary.bin', 'untracked.bin']) {
  127. expect(diffs.find((entry) => entry.file === file)).toMatchObject({
  128. additions: 0,
  129. deletions: 0,
  130. diff: expect.stringContaining('Binary files '),
  131. });
  132. }
  133. for (const file of portableNames) {
  134. expect(diffs.find((entry) => entry.file === file)).toMatchObject({
  135. additions: 2,
  136. deletions: 0,
  137. status: 'added',
  138. diff: expect.stringContaining('+++content\n'),
  139. });
  140. }
  141. expect(diffs.find((entry) => entry.file === 'empty.txt')).toMatchObject({
  142. additions: 0,
  143. deletions: 0,
  144. status: 'added',
  145. });
  146. });
  147. it.skipIf(process.platform === 'win32')(
  148. 'preserves POSIX whitespace, escape and arrow names for tracked and untracked files',
  149. async () => {
  150. for (const file of posixNames) await write(file, 'first\n');
  151. commit();
  152. for (const [index, file] of posixNames.entries()) {
  153. await appendFile(path.join(workspaceDir, file), `tracked-${index}\n`);
  154. await write(`untracked/${file}`, `untracked-${index}\n`);
  155. }
  156. const { diffs } = await read();
  157. expect(diffs).toHaveLength(posixNames.length * 2);
  158. for (const [index, file] of posixNames.entries()) {
  159. expect(diffs.find((entry) => entry.file === file)).toMatchObject({
  160. additions: 1,
  161. deletions: 0,
  162. diff: expect.stringContaining(`+tracked-${index}\n`),
  163. });
  164. expect(diffs.find((entry) => entry.file === `untracked/${file}`)).toMatchObject({
  165. additions: 1,
  166. deletions: 0,
  167. diff: expect.stringContaining(`+untracked-${index}\n`),
  168. });
  169. }
  170. },
  171. );
  172. it('retains a rename-only patch with zero line changes', async () => {
  173. await write('old 中文.txt', 'same\n');
  174. commit();
  175. git('mv', 'old 中文.txt', 'new 中文.txt');
  176. expect((await read()).diffs).toEqual([
  177. {
  178. file: 'new 中文.txt',
  179. additions: 0,
  180. deletions: 0,
  181. status: 'renamed',
  182. diff: expect.stringContaining('similarity index 100%'),
  183. },
  184. ]);
  185. });
  186. it.skipIf(process.platform === 'win32')(
  187. 'consumes both rename paths without splitting tabs or literal arrows',
  188. async () => {
  189. const source = 'dir/old\tline\n{left => right}.txt';
  190. const destination = 'dir/new\tline\n{right => left}.txt';
  191. await write(source, 'a\nb\nc\nd\ne\nf\n');
  192. commit();
  193. git('mv', source, destination);
  194. await appendFile(path.join(workspaceDir, destination), 'renamed\n');
  195. expect((await read()).diffs).toEqual([
  196. {
  197. file: destination,
  198. additions: 1,
  199. deletions: 0,
  200. status: 'renamed',
  201. diff: expect.stringContaining('+renamed\n'),
  202. },
  203. ]);
  204. },
  205. );
  206. it('handles brace-compressed renames and copies with destination stats and patches', async () => {
  207. await write('dir/old 中文.txt', 'a\nb\nc\nd\ne\nf\n');
  208. await write('copy-source.txt', 'unique\ncopy\nsource\nlines\nhere\nend\n');
  209. commit();
  210. git('mv', 'dir/old 中文.txt', 'dir/new 中文.txt');
  211. await appendFile(path.join(workspaceDir, 'dir/new 中文.txt'), 'renamed\n');
  212. await appendFile(path.join(workspaceDir, 'copy-source.txt'), 'source-edit\n');
  213. await write('copy 中文.txt', 'unique\ncopy\nsource\nlines\nhere\nend\ncopied\n');
  214. git('add', '--', 'copy 中文.txt');
  215. git('config', 'diff.renames', 'copies');
  216. const { diffs } = await read();
  217. expect(diffs).toHaveLength(3);
  218. expect(diffs.find((entry) => entry.file === 'dir/new 中文.txt')).toMatchObject({
  219. additions: 1,
  220. deletions: 0,
  221. status: 'renamed',
  222. diff: expect.stringContaining('+renamed\n'),
  223. });
  224. expect(diffs.find((entry) => entry.file === 'copy 中文.txt')).toMatchObject({
  225. additions: 1,
  226. deletions: 0,
  227. status: 'copied',
  228. diff: expect.stringContaining('+copied\n'),
  229. });
  230. expect(diffs.find((entry) => entry.file === 'copy-source.txt')).toMatchObject({
  231. additions: 1,
  232. deletions: 0,
  233. status: 'modified',
  234. diff: expect.stringContaining('+source-edit\n'),
  235. });
  236. });
  237. });
  238. it('returns an empty diff for a clean workspace', async () => {
  239. expect(await read()).toEqual({ diffs: [] });
  240. });
  241. it('prefers stored session/message diffs and falls back only when the selected record is absent', async () => {
  242. await write('live.txt', 'live\n');
  243. const fileChanges = [{ file: 'stored.txt', additions: 7, deletions: 2, diff: 'stored patch' }];
  244. await diffStore.upsert({
  245. sessionId: 'test-session',
  246. turnId: 'test-turn',
  247. changeSetId: 'test-change',
  248. assistantMessageId: 'test-message',
  249. workspaceDir,
  250. capturedAtMs: Date.now(),
  251. status: 'active',
  252. fileChanges,
  253. });
  254. for (const messageId of [undefined, 'test-message']) {
  255. expect(await read(messageId)).toEqual({ diffs: fileChanges, changeSetId: 'test-change' });
  256. }
  257. expect((await read('absent-message')).diffs).toEqual([
  258. {
  259. file: 'live.txt',
  260. additions: 1,
  261. deletions: 0,
  262. status: 'added',
  263. diff: expect.stringContaining('+live\n'),
  264. },
  265. ]);
  266. });
  267. it('associates patches without relying on color or path prefixes', async () => {
  268. await write('中文.txt', 'first\n');
  269. commit();
  270. await appendFile(path.join(workspaceDir, '中文.txt'), 'added\n');
  271. git('config', 'color.ui', 'always');
  272. git('config', 'diff.noprefix', 'true');
  273. expect((await read()).diffs).toEqual([
  274. {
  275. file: '中文.txt',
  276. additions: 1,
  277. deletions: 0,
  278. status: 'modified',
  279. diff: expect.stringContaining('+added\n'),
  280. },
  281. ]);
  282. });