Repository navigation
Expand file tree
/
Copy path_fixture.js
More file actions
163 lines (152 loc) · 5.53 KB
/
Copy path_fixture.js
File metadata and controls
163 lines (152 loc) · 5.53 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
/**
* Test fixture: builds a temporary $COPILOT_HOME with a SQLite DB matching
* Copilot's real schema and per-session state directories. Returns helpers
* to seed sessions and clean up.
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const Database = require('better-sqlite3');
const yaml = require('js-yaml');
const SCHEMA_SQL = [
`CREATE TABLE schema_version (version INTEGER NOT NULL)`,
`CREATE TABLE sessions (
id TEXT PRIMARY KEY,
cwd TEXT,
repository TEXT,
host_type TEXT,
branch TEXT,
summary TEXT,
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now'))
)`,
`CREATE TABLE turns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL REFERENCES sessions(id),
turn_index INTEGER NOT NULL,
user_message TEXT,
assistant_response TEXT,
timestamp TEXT DEFAULT (datetime('now')),
UNIQUE(session_id, turn_index)
)`,
`CREATE TABLE checkpoints (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL REFERENCES sessions(id),
checkpoint_number INTEGER NOT NULL,
title TEXT,
overview TEXT,
history TEXT,
work_done TEXT,
technical_details TEXT,
important_files TEXT,
next_steps TEXT,
created_at TEXT DEFAULT (datetime('now')),
UNIQUE(session_id, checkpoint_number)
)`,
`CREATE TABLE session_files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL REFERENCES sessions(id),
file_path TEXT NOT NULL,
tool_name TEXT,
turn_index INTEGER,
first_seen_at TEXT DEFAULT (datetime('now')),
UNIQUE(session_id, file_path)
)`,
`CREATE TABLE session_refs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL REFERENCES sessions(id),
ref_type TEXT NOT NULL,
ref_value TEXT NOT NULL,
turn_index INTEGER,
created_at TEXT DEFAULT (datetime('now')),
UNIQUE(session_id, ref_type, ref_value)
)`,
`CREATE VIRTUAL TABLE search_index USING fts5(
content,
session_id UNINDEXED,
source_type UNINDEXED,
source_id UNINDEXED
)`,
];
function makeFixture() {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-starter-test-'));
fs.mkdirSync(path.join(home, 'session-state'), { recursive: true });
const dbPath = path.join(home, 'session-store.db');
const db = new Database(dbPath);
for (const sql of SCHEMA_SQL) db.exec(sql);
db.prepare('INSERT INTO schema_version (version) VALUES (?)').run(1);
const insSession = db.prepare(`INSERT INTO sessions (id, cwd, repository, branch, summary, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`);
const insTurn = db.prepare(`INSERT INTO turns (session_id, turn_index, user_message, assistant_response, timestamp)
VALUES (?, ?, ?, ?, ?)`);
const insCp = db.prepare(`INSERT INTO checkpoints (session_id, checkpoint_number, title, overview, created_at)
VALUES (?, ?, ?, ?, ?)`);
const insFile = db.prepare(`INSERT INTO session_files (session_id, file_path, tool_name, turn_index, first_seen_at)
VALUES (?, ?, ?, ?, ?)`);
const insRef = db.prepare(`INSERT INTO session_refs (session_id, ref_type, ref_value, turn_index, created_at)
VALUES (?, ?, ?, ?, ?)`);
const insFts = db.prepare(`INSERT INTO search_index (content, session_id, source_type, source_id)
VALUES (?, ?, ?, ?)`);
function seedSession(opts) {
const {
id,
cwd = '/tmp/proj',
repository = '',
branch = '',
summary = '',
createdAt = '2026-04-01T00:00:00.000Z',
updatedAt = createdAt,
name = null,
userNamed = false,
turns = [],
checkpoints = [],
files = [],
refs = [],
eventsBytes = 0,
lockPid = null,
writeWorkspace = true,
} = opts;
insSession.run(id, cwd, repository, branch, summary, createdAt, updatedAt);
let i = 0;
for (const t of turns) {
insTurn.run(id, i, t.user || '', t.assistant || '', t.timestamp || createdAt);
insFts.run((t.user || '') + ' ' + (t.assistant || ''), id, 'turn', String(i));
i += 1;
}
let cn = 1;
for (const c of checkpoints) {
insCp.run(id, cn, c.title || `cp${cn}`, c.overview || '', createdAt);
cn += 1;
}
for (const f of files) insFile.run(id, f.filePath, f.toolName || 'edit', f.turnIndex || 0, createdAt);
for (const r of refs) insRef.run(id, r.refType, r.refValue, r.turnIndex || 0, createdAt);
if (writeWorkspace) {
const dir = path.join(home, 'session-state', id);
fs.mkdirSync(dir, { recursive: true });
const ws = {
id,
cwd,
name: name || summary || `session ${id.slice(0, 8)}`,
user_named: userNamed,
summary,
summary_count: 0,
created_at: createdAt,
updated_at: updatedAt,
};
fs.writeFileSync(path.join(dir, 'workspace.yaml'), yaml.dump(ws), 'utf-8');
if (eventsBytes > 0) {
fs.writeFileSync(path.join(dir, 'events.jsonl'), 'x'.repeat(eventsBytes), 'utf-8');
}
if (lockPid) {
fs.writeFileSync(path.join(dir, `inuse.${lockPid}.lock`), String(lockPid), 'utf-8');
}
}
}
function cleanup() {
try { db.close(); } catch { /* noop */ }
fs.rmSync(home, { recursive: true, force: true });
}
return { home, dbPath, db, seedSession, cleanup };
}
module.exports = { makeFixture };