workspace.test.ts
5.23 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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { workspaceManager } from '../../src/utils/workspace';
import { WorkspaceStructure } from '../../src/types/workspace-types';
describe('workspaceManager', () => {
let tempRoot: string;
beforeEach(() => {
// Reset singleton internal state between tests
(workspaceManager as any).path = null;
tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'coderio-ws-'));
});
afterEach(() => {
fs.rmSync(tempRoot, { recursive: true, force: true });
(workspaceManager as any).path = null;
});
it('initWorkspace should compute workspace structure and cache it', () => {
const ws1 = workspaceManager.initWorkspace('proj', tempRoot, 'my-app');
const ws2 = workspaceManager.initWorkspace('other', '/should-not-apply', 'other-app');
expect(ws2).toBe(ws1);
expect(ws1.root).toBe(path.resolve(tempRoot, 'coderio', 'proj'));
expect(ws1.app).toBe(path.join(ws1.root, 'my-app'));
expect(ws1.process).toBe(path.join(ws1.root, 'process'));
expect(ws1.debug).toBe(path.join(ws1.root, 'debug'));
expect(ws1.reports).toBe(path.join(ws1.root, 'reports.html'));
expect(ws1.db).toBe(path.join(ws1.root, 'checkpoint', 'coderio-cli.db'));
expect(ws1.checkpoint).toBe(path.join(ws1.root, 'checkpoint', 'checkpoint.json'));
});
it('resolveAppSrc should join app/src with subpath', () => {
const paths: WorkspaceStructure = {
root: '/r',
app: '/r/my-app',
process: '/r/process',
debug: '/r/debug',
reports: '/r/reports.html',
db: '/r/checkpoint/db.sqlite',
checkpoint: '/r/checkpoint/checkpoint.json',
};
expect(workspaceManager.resolveAppSrc(paths, 'components/Button')).toBe(path.join(paths.app, 'src', 'components/Button'));
});
it('resolveComponentPath should normalize common alias variants', () => {
expect(workspaceManager.resolveComponentPath('@/components/Button')).toBe('components/Button/index.tsx');
expect(workspaceManager.resolveComponentPath('@/src/components/Button')).toBe('components/Button/index.tsx');
expect(workspaceManager.resolveComponentPath('components/Button')).toBe('components/Button/index.tsx');
expect(workspaceManager.resolveComponentPath('components/Button.tsx')).toBe('components/Button.tsx');
expect(workspaceManager.resolveComponentPath('components/Button/index.tsx')).toBe('components/Button/index.tsx');
});
it('deleteWorkspace should delete all entries inside workspace root', () => {
const root = path.join(tempRoot, 'workspace');
fs.mkdirSync(root, { recursive: true });
fs.writeFileSync(path.join(root, 'a.txt'), 'a');
fs.mkdirSync(path.join(root, 'dir'));
fs.writeFileSync(path.join(root, 'dir', 'b.txt'), 'b');
const ws: WorkspaceStructure = {
root,
app: '',
process: '',
debug: '',
reports: '',
db: '',
checkpoint: '',
};
workspaceManager.deleteWorkspace(ws);
expect(fs.existsSync(root)).toBe(true);
expect(fs.readdirSync(root)).toHaveLength(0);
});
it('deleteWorkspace should preserve specified files while deleting others', () => {
const root = path.join(tempRoot, 'workspace-preserve');
fs.mkdirSync(root, { recursive: true });
fs.writeFileSync(path.join(root, 'a.txt'), 'a');
fs.writeFileSync(path.join(root, 'delete-me.txt'), 'delete');
fs.mkdirSync(path.join(root, 'checkpoint'), { recursive: true });
fs.writeFileSync(path.join(root, 'checkpoint', 'coderio-cli.db'), 'db');
fs.writeFileSync(path.join(root, 'checkpoint', 'checkpoint.json'), 'json');
fs.mkdirSync(path.join(root, 'process'), { recursive: true });
fs.writeFileSync(path.join(root, 'process', 'data.txt'), 'data');
const ws: WorkspaceStructure = {
root,
app: '',
process: '',
debug: '',
reports: '',
db: '',
checkpoint: '',
};
// Preserve only the database file
workspaceManager.deleteWorkspace(ws, ['checkpoint/coderio-cli.db']);
// Root should still exist
expect(fs.existsSync(root)).toBe(true);
// checkpoint directory should still exist (contains preserved file)
expect(fs.existsSync(path.join(root, 'checkpoint'))).toBe(true);
// Database file should be preserved
expect(fs.existsSync(path.join(root, 'checkpoint', 'coderio-cli.db'))).toBe(true);
expect(fs.readFileSync(path.join(root, 'checkpoint', 'coderio-cli.db'), 'utf8')).toBe('db');
// checkpoint.json should be deleted
expect(fs.existsSync(path.join(root, 'checkpoint', 'checkpoint.json'))).toBe(false);
// Other files and directories should be deleted
expect(fs.existsSync(path.join(root, 'a.txt'))).toBe(false);
expect(fs.existsSync(path.join(root, 'delete-me.txt'))).toBe(false);
expect(fs.existsSync(path.join(root, 'process'))).toBe(false);
});
});