-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdetect-changes.test.ts
More file actions
144 lines (120 loc) · 4.68 KB
/
Copy pathdetect-changes.test.ts
File metadata and controls
144 lines (120 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Unit tests for detectChanges pipeline stage.
*/
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { closeDb, initSchema, openDb } from '../../src/db/index.js';
import { PipelineContext } from '../../src/domain/graph/builder/context.js';
import { detectChanges } from '../../src/domain/graph/builder/stages/detect-changes.js';
import { writeJournalHeader } from '../../src/domain/graph/journal.js';
let tmpDir: string;
beforeAll(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-stage-detect-'));
fs.writeFileSync(path.join(tmpDir, 'a.js'), 'export const a = 1;');
});
afterAll(() => {
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
});
describe('detectChanges stage', () => {
it('treats all files as changed when file_hashes is empty', async () => {
const dbDir = path.join(tmpDir, '.codegraph');
fs.mkdirSync(dbDir, { recursive: true });
const db = openDb(path.join(dbDir, 'graph.db'));
initSchema(db);
const ctx = new PipelineContext();
ctx.rootDir = tmpDir;
ctx.db = db;
ctx.allFiles = [path.join(tmpDir, 'a.js')];
ctx.opts = {};
ctx.incremental = true;
ctx.forceFullRebuild = false;
ctx.config = {};
await detectChanges(ctx);
// Empty file_hashes = all files are new (incremental, not full build)
expect(ctx.isFullBuild).toBe(false);
expect(ctx.earlyExit).toBe(false);
expect(ctx.parseChanges.length).toBe(1);
closeDb(db);
});
it('detects early exit when no changes after initial build', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-stage-nochange-'));
const dbDir = path.join(dir, '.codegraph');
fs.mkdirSync(dbDir, { recursive: true });
fs.writeFileSync(path.join(dir, 'a.js'), 'export const a = 1;');
const db = openDb(path.join(dbDir, 'graph.db'));
initSchema(db);
// Seed file_hashes so incremental thinks file is unchanged
const content = fs.readFileSync(path.join(dir, 'a.js'), 'utf-8');
const { createHash } = await import('node:crypto');
const hash = createHash('md5').update(content).digest('hex');
const stat = fs.statSync(path.join(dir, 'a.js'));
db.prepare('INSERT INTO file_hashes (file, hash, mtime, size) VALUES (?, ?, ?, ?)').run(
'a.js',
hash,
Math.floor(stat.mtimeMs),
stat.size,
);
// Write journal header so journal check doesn't confuse things
writeJournalHeader(dir, Date.now());
const ctx = new PipelineContext();
ctx.rootDir = dir;
ctx.db = db;
ctx.allFiles = [path.join(dir, 'a.js')];
ctx.opts = {};
ctx.incremental = true;
ctx.forceFullRebuild = false;
ctx.config = {};
await detectChanges(ctx);
expect(ctx.earlyExit).toBe(true);
// DB should be closed by detectChanges on early exit
fs.rmSync(dir, { recursive: true, force: true });
});
it('skips change detection for scoped builds', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-stage-scope-'));
const dbDir = path.join(dir, '.codegraph');
fs.mkdirSync(dbDir, { recursive: true });
fs.writeFileSync(path.join(dir, 'a.js'), 'export const a = 1;');
const db = openDb(path.join(dbDir, 'graph.db'));
initSchema(db);
const ctx = new PipelineContext();
ctx.rootDir = dir;
ctx.db = db;
ctx.allFiles = [path.join(dir, 'a.js')];
ctx.opts = { scope: ['a.js'] };
ctx.incremental = true;
ctx.forceFullRebuild = false;
ctx.config = {};
ctx.parseChanges = [{ file: path.join(dir, 'a.js'), relPath: 'a.js' }];
ctx.removed = [];
ctx.isFullBuild = false;
await detectChanges(ctx);
// Should return without modifying isFullBuild
expect(ctx.isFullBuild).toBe(false);
expect(ctx.earlyExit).toBe(false);
closeDb(db);
fs.rmSync(dir, { recursive: true, force: true });
});
it('forces full rebuild when forceFullRebuild is set', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-stage-force-'));
const dbDir = path.join(dir, '.codegraph');
fs.mkdirSync(dbDir, { recursive: true });
fs.writeFileSync(path.join(dir, 'a.js'), 'export const a = 1;');
const db = openDb(path.join(dbDir, 'graph.db'));
initSchema(db);
const ctx = new PipelineContext();
ctx.rootDir = dir;
ctx.db = db;
ctx.allFiles = [path.join(dir, 'a.js')];
ctx.opts = {};
ctx.incremental = true;
ctx.forceFullRebuild = true;
ctx.config = {};
await detectChanges(ctx);
expect(ctx.isFullBuild).toBe(true);
expect(ctx.parseChanges.length).toBe(1);
closeDb(db);
fs.rmSync(dir, { recursive: true, force: true });
});
});