File size: 20,282 Bytes
1f21206 | 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | import { afterEach, describe, expect, it } from 'bun:test'
import * as fs from 'node:fs/promises'
import { execFileSync } from 'node:child_process'
import * as os from 'node:os'
import * as path from 'node:path'
import { WorkspaceService } from '../services/workspaceService.js'
const cleanupDirs = new Set<string>()
const ONE_MIB = 1024 * 1024
function trackDir(dir: string): string {
cleanupDirs.add(dir)
return dir
}
async function makeTempDir(prefix: string): Promise<string> {
return trackDir(await fs.mkdtemp(path.join(os.tmpdir(), prefix)))
}
function git(cwd: string, ...args: string[]): string {
return execFileSync('git', args, {
cwd,
encoding: 'utf8',
})
}
async function createGitWorkspace(): Promise<string> {
const repoDir = await makeTempDir('workspace-service-git-')
git(repoDir, 'init')
git(repoDir, 'config', 'user.email', 'workspace-service@example.com')
git(repoDir, 'config', 'user.name', 'Workspace Service')
await fs.writeFile(path.join(repoDir, 'tracked.txt'), 'before\n')
await fs.writeFile(path.join(repoDir, 'deleted.txt'), 'delete me\n')
await fs.writeFile(path.join(repoDir, 'clean.txt'), 'clean\n')
git(repoDir, 'add', 'tracked.txt', 'deleted.txt', 'clean.txt')
git(repoDir, 'commit', '-m', 'initial')
await fs.writeFile(path.join(repoDir, 'tracked.txt'), 'before\nafter\n')
await fs.writeFile(path.join(repoDir, 'new.txt'), 'new file\n')
git(repoDir, 'add', 'new.txt')
await fs.unlink(path.join(repoDir, 'deleted.txt'))
await fs.writeFile(path.join(repoDir, 'untracked.txt'), 'still untracked\n')
return repoDir
}
async function createNestedGitWorkspace(): Promise<{
repoDir: string
workDir: string
}> {
const repoDir = await makeTempDir('workspace-service-nested-git-')
const workDir = path.join(repoDir, 'subdir')
git(repoDir, 'init')
git(repoDir, 'config', 'user.email', 'workspace-service@example.com')
git(repoDir, 'config', 'user.name', 'Workspace Service')
await fs.mkdir(workDir)
await fs.writeFile(path.join(repoDir, 'root.txt'), 'root original\n')
await fs.writeFile(path.join(workDir, 'sub.txt'), 'sub original\n')
git(repoDir, 'add', 'root.txt', 'subdir/sub.txt')
git(repoDir, 'commit', '-m', 'initial')
await fs.writeFile(path.join(repoDir, 'root.txt'), 'root original\nroot changed\n')
await fs.writeFile(path.join(workDir, 'sub.txt'), 'sub original\nsub changed\n')
return { repoDir, workDir }
}
afterEach(async () => {
for (const dir of cleanupDirs) {
await fs.rm(dir, { recursive: true, force: true })
}
cleanupDirs.clear()
})
describe('WorkspaceService', () => {
it('returns git status for modified, added, deleted, and untracked files', async () => {
const repoDir = await createGitWorkspace()
const service = new WorkspaceService(async (sessionId) => sessionId === 'session-1' ? repoDir : null)
const result = await service.getStatus('session-1')
expect(result.state).toBe('ok')
expect(result.workDir).toBe(repoDir)
expect(result.isGitRepo).toBe(true)
expect(result.repoName).toBe(path.basename(repoDir))
expect(result.branch).toBeTruthy()
const files = new Map(result.changedFiles.map((file) => [file.path, file]))
expect(Array.from(files.keys()).sort()).toEqual([
'deleted.txt',
'new.txt',
'tracked.txt',
'untracked.txt',
])
expect(files.get('tracked.txt')?.status).toBe('modified')
expect(files.get('tracked.txt')?.additions).toBeGreaterThan(0)
expect(files.get('new.txt')?.status).toBe('added')
expect(files.get('new.txt')?.additions).toBeGreaterThan(0)
expect(files.get('deleted.txt')?.status).toBe('deleted')
expect(files.get('deleted.txt')?.deletions).toBeGreaterThan(0)
expect(files.get('untracked.txt')).toMatchObject({
status: 'untracked',
additions: 1,
deletions: 0,
})
})
it('scopes git status and diff paths to a nested workDir inside a repo', async () => {
const { repoDir, workDir } = await createNestedGitWorkspace()
const service = new WorkspaceService(async (sessionId) => sessionId === 'session-1' ? workDir : null)
const status = await service.getStatus('session-1')
expect(status.state).toBe('ok')
expect(status.workDir).toBe(workDir)
expect(status.repoName).toBe(path.basename(repoDir))
expect(status.changedFiles).toHaveLength(1)
expect(status.changedFiles[0]).toMatchObject({
path: 'sub.txt',
status: 'modified',
})
expect(status.changedFiles[0]?.additions).toBeGreaterThan(0)
expect(status.changedFiles[0]?.deletions).toBeGreaterThanOrEqual(0)
expect(status.changedFiles.some((file) => file.path === 'root.txt')).toBe(false)
const diff = await service.getDiff('session-1', 'sub.txt')
expect(diff.state).toBe('ok')
expect(diff.diff).toContain('subdir/sub.txt')
expect(diff.diff?.length).toBeGreaterThan(0)
})
it('returns explicit non-git and missing-workdir states', async () => {
const nonGitDir = await makeTempDir('workspace-service-non-git-')
const missingDir = path.join(await makeTempDir('workspace-service-missing-parent-'), 'missing')
const service = new WorkspaceService(async (sessionId) => {
if (sessionId === 'non-git') return nonGitDir
if (sessionId === 'missing') return missingDir
return null
})
await expect(service.getStatus('unknown')).rejects.toThrow('Session not found: unknown')
await expect(service.getStatus('non-git')).resolves.toMatchObject({
state: 'ok',
workDir: nonGitDir,
repoName: path.basename(nonGitDir),
isGitRepo: false,
changedFiles: [],
})
await expect(service.getStatus('missing')).resolves.toMatchObject({
state: 'missing_workdir',
workDir: missingDir,
isGitRepo: false,
changedFiles: [],
})
})
it('reports session tool edits without requiring a git repository', async () => {
const nonGitDir = await makeTempDir('workspace-service-session-changes-')
await fs.mkdir(path.join(nonGitDir, 'src'))
await fs.writeFile(path.join(nonGitDir, 'src/App.jsx'), 'export default function App() { return <main>New</main> }\n')
const service = new WorkspaceService(
async () => nonGitDir,
async () => [{
id: 'assistant-1',
type: 'tool_use',
timestamp: new Date().toISOString(),
content: [{
type: 'tool_use',
name: 'Edit',
input: {
file_path: 'src/App.jsx',
old_string: 'export default function App() { return <main>Old</main> }\n',
new_string: 'export default function App() { return <main>New</main> }\n',
},
}],
}],
)
const status = await service.getStatus('session-1')
expect(status).toMatchObject({
state: 'ok',
workDir: nonGitDir,
isGitRepo: false,
changedFiles: [{
path: 'src/App.jsx',
status: 'modified',
additions: 1,
deletions: 1,
}],
})
const diff = await service.getDiff('session-1', 'src/App.jsx')
expect(diff.state).toBe('ok')
expect(diff.diff).toContain('diff --session a/src/App.jsx b/src/App.jsx')
expect(diff.diff).toContain('-export default function App() { return <main>Old</main> }')
expect(diff.diff).toContain('+export default function App() { return <main>New</main> }')
})
it('reports file-history changes without requiring a git repository', async () => {
const nonGitDir = await makeTempDir('workspace-service-file-history-')
const generatedFile = path.join(nonGitDir, 'aacc', 'src', 'App.tsx')
await fs.mkdir(path.dirname(generatedFile), { recursive: true })
await fs.writeFile(generatedFile, 'export default function App() { return <main>Tetris</main> }\n')
const service = new WorkspaceService(
async () => nonGitDir,
async () => [],
async () => [{
messageId: '11111111-1111-4111-8111-111111111111',
timestamp: new Date('2026-01-01T00:00:00.000Z'),
trackedFileBackups: {
'aacc/src/App.tsx': {
backupFileName: null,
version: 1,
backupTime: new Date('2026-01-01T00:00:00.000Z'),
},
},
}],
)
const status = await service.getStatus('session-1')
expect(status).toMatchObject({
state: 'ok',
workDir: nonGitDir,
isGitRepo: false,
changedFiles: [{
path: 'aacc/src/App.tsx',
status: 'added',
additions: 1,
deletions: 0,
}],
})
const diff = await service.getDiff('session-1', 'aacc/src/App.tsx')
expect(diff.state).toBe('ok')
expect(diff.diff).toContain('diff --session /dev/null b/aacc/src/App.tsx')
expect(diff.diff).toContain('+export default function App() { return <main>Tetris</main> }')
})
it('matches Windows file-history paths case-insensitively inside the workspace', async () => {
if (process.platform !== 'win32') return
const nonGitDir = await makeTempDir('workspace-service-windows-paths-')
const targetFile = path.join(nonGitDir, 'Child', 'index.ts')
await fs.mkdir(path.dirname(targetFile), { recursive: true })
await fs.writeFile(targetFile, 'export const value = 1\n')
const lowerDrivePath = targetFile[0]?.toLowerCase() + targetFile.slice(1)
const service = new WorkspaceService(
async () => nonGitDir,
async () => [],
async () => [{
messageId: '22222222-2222-4222-8222-222222222222',
timestamp: new Date('2026-01-01T00:00:00.000Z'),
trackedFileBackups: {
[lowerDrivePath]: {
backupFileName: null,
version: 1,
backupTime: new Date('2026-01-01T00:00:00.000Z'),
},
},
}],
)
const status = await service.getStatus('session-1')
expect(status.changedFiles).toEqual([{
path: 'Child/index.ts',
oldPath: undefined,
status: 'added',
additions: 1,
deletions: 0,
}])
})
it('rejects traversal attempts for file, diff, and tree access', async () => {
const repoDir = await createGitWorkspace()
const service = new WorkspaceService(async () => repoDir)
await expect(service.readFile('session-1', '../outside.txt')).rejects.toThrow(/outside workspace/)
await expect(service.getDiff('session-1', '../outside.txt')).resolves.toMatchObject({
state: 'error',
path: '../outside.txt',
})
await expect(service.readTree('session-1', '../outside')).rejects.toThrow(/outside workspace/)
})
it('rejects symlink targets that escape the workspace root', async () => {
const workDir = await makeTempDir('workspace-service-symlink-')
const outsideDir = await makeTempDir('workspace-service-symlink-outside-')
const outsideFile = path.join(outsideDir, 'secret.txt')
await fs.writeFile(outsideFile, 'top secret\n')
await fs.symlink(outsideFile, path.join(workDir, 'escape.txt'))
const service = new WorkspaceService(async () => workDir)
await expect(service.readFile('session-1', 'escape.txt')).rejects.toThrow(/outside workspace/)
})
it('returns error for an untracked symlink that escapes the workspace root', async () => {
const repoDir = await makeTempDir('workspace-service-symlink-git-')
const outsideDir = await makeTempDir('workspace-service-symlink-git-outside-')
const outsideFile = path.join(outsideDir, 'secret.txt')
git(repoDir, 'init')
git(repoDir, 'config', 'user.email', 'workspace-service@example.com')
git(repoDir, 'config', 'user.name', 'Workspace Service')
await fs.writeFile(path.join(repoDir, 'tracked.txt'), 'tracked\n')
git(repoDir, 'add', 'tracked.txt')
git(repoDir, 'commit', '-m', 'initial')
await fs.writeFile(outsideFile, 'top secret\n')
await fs.symlink(outsideFile, path.join(repoDir, 'escape.txt'))
const service = new WorkspaceService(async () => repoDir)
const status = await service.getStatus('session-1')
expect(status.state).toBe('error')
expect(status.error).toMatch(/outside workspace/)
await expect(service.getDiff('session-1', 'escape.txt')).resolves.toMatchObject({
state: 'error',
path: 'escape.txt',
})
const diffOutcome = await service.getDiff('session-1', 'escape.txt')
expect(diffOutcome.error).toMatch(/outside workspace/)
})
it('returns explicit readFile states for text, binary, large, and missing targets', async () => {
const workDir = await makeTempDir('workspace-service-files-')
const service = new WorkspaceService(async () => workDir)
await fs.writeFile(path.join(workDir, 'note.ts'), 'export const answer = 42\n')
await fs.writeFile(path.join(workDir, 'binary.bin'), Buffer.from([0, 1, 2, 3]))
await fs.writeFile(path.join(workDir, 'image.png'), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00]))
await fs.writeFile(
path.join(workDir, 'large-image.png'),
Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47]), Buffer.alloc(ONE_MIB + 1, 0xff)]),
)
await fs.writeFile(path.join(workDir, 'large.txt'), Buffer.alloc(ONE_MIB + 1, 'a'))
await fs.mkdir(path.join(workDir, 'folder'))
await expect(service.readFile('session-1', 'note.ts')).resolves.toMatchObject({
state: 'ok',
language: 'typescript',
size: 25,
content: 'export const answer = 42\n',
})
await expect(service.readFile('session-1', 'binary.bin')).resolves.toMatchObject({
state: 'binary',
language: 'binary',
size: 4,
})
await expect(service.readFile('session-1', 'image.png')).resolves.toMatchObject({
state: 'ok',
previewType: 'image',
language: 'image',
mimeType: 'image/png',
dataUrl: 'data:image/png;base64,iVBORwA=',
size: 5,
})
const largeImage = await service.readFile('session-1', 'large-image.png')
expect(largeImage).toMatchObject({
state: 'ok',
previewType: 'image',
language: 'image',
mimeType: 'image/png',
size: ONE_MIB + 5,
})
expect(largeImage.dataUrl).toStartWith('data:image/png;base64,')
await expect(service.readFile('session-1', 'large.txt')).resolves.toMatchObject({
state: 'ok',
previewType: 'text',
language: 'text',
size: ONE_MIB + 1,
readBytes: ONE_MIB,
truncated: true,
content: 'a'.repeat(ONE_MIB),
})
await expect(service.readFile('session-1', 'missing.txt')).resolves.toMatchObject({
state: 'missing',
})
await expect(service.readFile('session-1', 'folder')).resolves.toMatchObject({
state: 'missing',
})
})
it('lists a single directory level with dotfiles excluded and directories first', async () => {
const workDir = await makeTempDir('workspace-service-tree-')
const service = new WorkspaceService(async () => workDir)
await fs.mkdir(path.join(workDir, 'b-dir'))
await fs.mkdir(path.join(workDir, 'a-dir'))
await fs.mkdir(path.join(workDir, 'a-dir', 'inner'))
await fs.writeFile(path.join(workDir, 'a-dir', 'note.txt'), 'nested\n')
await fs.writeFile(path.join(workDir, 'z-file.txt'), 'root file\n')
await fs.writeFile(path.join(workDir, '.hidden.txt'), 'ignore\n')
await expect(service.readTree('session-1')).resolves.toMatchObject({
state: 'ok',
path: '',
entries: [
{ name: 'a-dir', path: 'a-dir', isDirectory: true },
{ name: 'b-dir', path: 'b-dir', isDirectory: true },
{ name: 'z-file.txt', path: 'z-file.txt', isDirectory: false },
],
})
await expect(service.readTree('session-1', 'a-dir')).resolves.toMatchObject({
state: 'ok',
path: 'a-dir',
entries: [
{ name: 'inner', path: 'a-dir/inner', isDirectory: true },
{ name: 'note.txt', path: 'a-dir/note.txt', isDirectory: false },
],
})
})
it('returns diffs for modified, added, deleted, and untracked files', async () => {
const repoDir = await createGitWorkspace()
const service = new WorkspaceService(async (sessionId) => sessionId === 'session-1' ? repoDir : null)
const modified = await service.getDiff('session-1', 'tracked.txt')
expect(modified.state).toBe('ok')
expect(modified.diff).toContain('tracked.txt')
expect(modified.diff.length).toBeGreaterThan(0)
const added = await service.getDiff('session-1', 'new.txt')
expect(added.state).toBe('ok')
expect(added.diff).toContain('new.txt')
expect(added.diff.length).toBeGreaterThan(0)
const deleted = await service.getDiff('session-1', 'deleted.txt')
expect(deleted.state).toBe('ok')
expect(deleted.diff).toContain('deleted.txt')
expect(deleted.diff.length).toBeGreaterThan(0)
const untracked = await service.getDiff('session-1', 'untracked.txt')
expect(untracked.state).toBe('ok')
expect(untracked.diff).toContain('untracked.txt')
expect(untracked.diff.length).toBeGreaterThan(0)
await expect(service.getDiff('session-1', 'clean.txt')).resolves.toMatchObject({
state: 'missing',
path: 'clean.txt',
})
const nonGitDir = await makeTempDir('workspace-service-diff-non-git-')
const nonGitService = new WorkspaceService(async () => nonGitDir)
await expect(nonGitService.getDiff('session-1', 'whatever.txt')).resolves.toMatchObject({
state: 'not_git_repo',
path: 'whatever.txt',
})
})
it('returns explicit error state when git status fails instead of ok-empty', async () => {
const repoDir = await createGitWorkspace()
const service = new WorkspaceService(async () => repoDir) as WorkspaceService & {
runGit: (workDir: string, args: string[]) => Promise<{
stdout: string
stderr: string
code: number
}>
}
service.runGit = async (workDir, args) => {
if (args[0] === 'rev-parse' && args[1] === '--show-toplevel') {
return { stdout: `${workDir}\n`, stderr: '', code: 0 }
}
if (args[0] === 'rev-parse' && args[1] === '--abbrev-ref') {
return { stdout: 'main\n', stderr: '', code: 0 }
}
if (args[0] === 'status') {
return { stdout: '', stderr: 'fatal: synthetic git failure', code: 1 }
}
return { stdout: '', stderr: 'unexpected call', code: 1 }
}
await expect(service.getStatus('session-1')).resolves.toMatchObject({
state: 'error',
isGitRepo: true,
})
const result = await service.getStatus('session-1')
expect(result.state).toBe('error')
expect(result.changedFiles).toEqual([])
expect(result.error).toContain('Failed to read git status')
expect(result.error).toContain('synthetic git failure')
})
it('reads tracked diff stats in one bulk git call', async () => {
const repoDir = await makeTempDir('workspace-service-bulk-stats-')
await fs.writeFile(path.join(repoDir, 'a.txt'), 'a\n')
await fs.writeFile(path.join(repoDir, 'b.txt'), 'b\n')
const diffStatCalls: string[][] = []
const service = new WorkspaceService(async () => repoDir) as WorkspaceService & {
runGit: (workDir: string, args: string[]) => Promise<{
stdout: string
stderr: string
code: number
}>
}
service.runGit = async (_workDir, args) => {
if (args[0] === 'rev-parse' && args[1] === '--show-toplevel') {
return { stdout: `${repoDir}\n`, stderr: '', code: 0 }
}
if (args[0] === 'rev-parse' && args[1] === '--abbrev-ref') {
return { stdout: 'main\n', stderr: '', code: 0 }
}
if (args[0] === 'status') {
return { stdout: ' M a.txt\0 M b.txt\0', stderr: '', code: 0 }
}
if (args[0] === 'diff' && args.includes('--numstat')) {
diffStatCalls.push(args)
return { stdout: '1\t0\ta.txt\n2\t3\tb.txt\n', stderr: '', code: 0 }
}
return { stdout: '', stderr: `unexpected git call: ${args.join(' ')}`, code: 1 }
}
const result = await service.getStatus('session-1')
expect(result.state).toBe('ok')
expect(diffStatCalls).toHaveLength(1)
expect(result.changedFiles).toEqual([
{ path: 'a.txt', oldPath: undefined, status: 'modified', additions: 1, deletions: 0 },
{ path: 'b.txt', oldPath: undefined, status: 'modified', additions: 2, deletions: 3 },
])
})
})
|