@studio-foundation/cli 0.3.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +40 -0
- package/LICENSE +663 -0
- package/README.md +165 -0
- package/dist/commands/api.d.ts +16 -0
- package/dist/commands/api.d.ts.map +1 -0
- package/dist/commands/api.js +118 -0
- package/dist/commands/api.js.map +1 -0
- package/dist/commands/config.d.ts +32 -0
- package/dist/commands/config.d.ts.map +1 -0
- package/dist/commands/config.js +390 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/init.d.ts +93 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +728 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/install.d.ts +2 -0
- package/dist/commands/install.d.ts.map +1 -0
- package/dist/commands/install.js +22 -0
- package/dist/commands/install.js.map +1 -0
- package/dist/commands/integrations.d.ts +14 -0
- package/dist/commands/integrations.d.ts.map +1 -0
- package/dist/commands/integrations.js +282 -0
- package/dist/commands/integrations.js.map +1 -0
- package/dist/commands/list.d.ts +9 -0
- package/dist/commands/list.d.ts.map +1 -0
- package/dist/commands/list.js +161 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/logs.d.ts +7 -0
- package/dist/commands/logs.d.ts.map +1 -0
- package/dist/commands/logs.js +98 -0
- package/dist/commands/logs.js.map +1 -0
- package/dist/commands/ollama.d.ts +6 -0
- package/dist/commands/ollama.d.ts.map +1 -0
- package/dist/commands/ollama.js +161 -0
- package/dist/commands/ollama.js.map +1 -0
- package/dist/commands/project.d.ts +38 -0
- package/dist/commands/project.d.ts.map +1 -0
- package/dist/commands/project.js +137 -0
- package/dist/commands/project.js.map +1 -0
- package/dist/commands/registry/audit.d.ts +13 -0
- package/dist/commands/registry/audit.d.ts.map +1 -0
- package/dist/commands/registry/audit.js +65 -0
- package/dist/commands/registry/audit.js.map +1 -0
- package/dist/commands/registry/index.d.ts +3 -0
- package/dist/commands/registry/index.d.ts.map +1 -0
- package/dist/commands/registry/index.js +53 -0
- package/dist/commands/registry/index.js.map +1 -0
- package/dist/commands/registry/install.d.ts +17 -0
- package/dist/commands/registry/install.d.ts.map +1 -0
- package/dist/commands/registry/install.js +153 -0
- package/dist/commands/registry/install.js.map +1 -0
- package/dist/commands/registry/publish.d.ts +8 -0
- package/dist/commands/registry/publish.d.ts.map +1 -0
- package/dist/commands/registry/publish.js +94 -0
- package/dist/commands/registry/publish.js.map +1 -0
- package/dist/commands/registry/remove.d.ts +8 -0
- package/dist/commands/registry/remove.d.ts.map +1 -0
- package/dist/commands/registry/remove.js +82 -0
- package/dist/commands/registry/remove.js.map +1 -0
- package/dist/commands/registry/search.d.ts +9 -0
- package/dist/commands/registry/search.d.ts.map +1 -0
- package/dist/commands/registry/search.js +68 -0
- package/dist/commands/registry/search.js.map +1 -0
- package/dist/commands/registry/sync.d.ts +8 -0
- package/dist/commands/registry/sync.d.ts.map +1 -0
- package/dist/commands/registry/sync.js +19 -0
- package/dist/commands/registry/sync.js.map +1 -0
- package/dist/commands/registry/update.d.ts +15 -0
- package/dist/commands/registry/update.d.ts.map +1 -0
- package/dist/commands/registry/update.js +48 -0
- package/dist/commands/registry/update.js.map +1 -0
- package/dist/commands/replay.d.ts +36 -0
- package/dist/commands/replay.d.ts.map +1 -0
- package/dist/commands/replay.js +453 -0
- package/dist/commands/replay.js.map +1 -0
- package/dist/commands/run.d.ts +18 -0
- package/dist/commands/run.d.ts.map +1 -0
- package/dist/commands/run.js +406 -0
- package/dist/commands/run.js.map +1 -0
- package/dist/commands/status.d.ts +6 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +144 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/template/index.d.ts +2 -0
- package/dist/commands/template/index.d.ts.map +1 -0
- package/dist/commands/template/index.js +57 -0
- package/dist/commands/template/index.js.map +1 -0
- package/dist/commands/template/validate.d.ts +8 -0
- package/dist/commands/template/validate.d.ts.map +1 -0
- package/dist/commands/template/validate.js +135 -0
- package/dist/commands/template/validate.js.map +1 -0
- package/dist/commands/templates.d.ts +14 -0
- package/dist/commands/templates.d.ts.map +1 -0
- package/dist/commands/templates.js +86 -0
- package/dist/commands/templates.js.map +1 -0
- package/dist/commands/tools.d.ts +12 -0
- package/dist/commands/tools.d.ts.map +1 -0
- package/dist/commands/tools.js +200 -0
- package/dist/commands/tools.js.map +1 -0
- package/dist/commands/users.d.ts +4 -0
- package/dist/commands/users.d.ts.map +1 -0
- package/dist/commands/users.js +123 -0
- package/dist/commands/users.js.map +1 -0
- package/dist/commands/validate.d.ts +2 -0
- package/dist/commands/validate.d.ts.map +1 -0
- package/dist/commands/validate.js +39 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +64 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +178 -0
- package/dist/index.js.map +1 -0
- package/dist/models-cache.d.ts +13 -0
- package/dist/models-cache.d.ts.map +1 -0
- package/dist/models-cache.js +106 -0
- package/dist/models-cache.js.map +1 -0
- package/dist/output/file-changes.d.ts +15 -0
- package/dist/output/file-changes.d.ts.map +1 -0
- package/dist/output/file-changes.js +77 -0
- package/dist/output/file-changes.js.map +1 -0
- package/dist/output/formatter.d.ts +6 -0
- package/dist/output/formatter.d.ts.map +1 -0
- package/dist/output/formatter.js +70 -0
- package/dist/output/formatter.js.map +1 -0
- package/dist/output/formatters.d.ts +42 -0
- package/dist/output/formatters.d.ts.map +1 -0
- package/dist/output/formatters.js +280 -0
- package/dist/output/formatters.js.map +1 -0
- package/dist/output/logger.d.ts +3 -0
- package/dist/output/logger.d.ts.map +1 -0
- package/dist/output/logger.js +15 -0
- package/dist/output/logger.js.map +1 -0
- package/dist/output/parallel-progress.d.ts +33 -0
- package/dist/output/parallel-progress.d.ts.map +1 -0
- package/dist/output/parallel-progress.js +85 -0
- package/dist/output/parallel-progress.js.map +1 -0
- package/dist/output/progress.d.ts +32 -0
- package/dist/output/progress.d.ts.map +1 -0
- package/dist/output/progress.js +393 -0
- package/dist/output/progress.js.map +1 -0
- package/dist/provider-validator.d.ts +15 -0
- package/dist/provider-validator.d.ts.map +1 -0
- package/dist/provider-validator.js +85 -0
- package/dist/provider-validator.js.map +1 -0
- package/dist/registry/cache.d.ts +10 -0
- package/dist/registry/cache.d.ts.map +1 -0
- package/dist/registry/cache.js +36 -0
- package/dist/registry/cache.js.map +1 -0
- package/dist/registry/client.d.ts +19 -0
- package/dist/registry/client.d.ts.map +1 -0
- package/dist/registry/client.js +63 -0
- package/dist/registry/client.js.map +1 -0
- package/dist/registry/lockfile.d.ts +17 -0
- package/dist/registry/lockfile.d.ts.map +1 -0
- package/dist/registry/lockfile.js +61 -0
- package/dist/registry/lockfile.js.map +1 -0
- package/dist/registry/resolver.d.ts +13 -0
- package/dist/registry/resolver.d.ts.map +1 -0
- package/dist/registry/resolver.js +79 -0
- package/dist/registry/resolver.js.map +1 -0
- package/dist/registry/types.d.ts +59 -0
- package/dist/registry/types.d.ts.map +1 -0
- package/dist/registry/types.js +14 -0
- package/dist/registry/types.js.map +1 -0
- package/dist/run-logger.d.ts +8 -0
- package/dist/run-logger.d.ts.map +1 -0
- package/dist/run-logger.js +53 -0
- package/dist/run-logger.js.map +1 -0
- package/dist/run-store-factory.d.ts +9 -0
- package/dist/run-store-factory.d.ts.map +1 -0
- package/dist/run-store-factory.js +26 -0
- package/dist/run-store-factory.js.map +1 -0
- package/dist/studio-dir.d.ts +7 -0
- package/dist/studio-dir.d.ts.map +1 -0
- package/dist/studio-dir.js +27 -0
- package/dist/studio-dir.js.map +1 -0
- package/dist/utils/input-wizard.d.ts +4 -0
- package/dist/utils/input-wizard.d.ts.map +1 -0
- package/dist/utils/input-wizard.js +56 -0
- package/dist/utils/input-wizard.js.map +1 -0
- package/dist/utils/placeholders.d.ts +6 -0
- package/dist/utils/placeholders.d.ts.map +1 -0
- package/dist/utils/placeholders.js +12 -0
- package/dist/utils/placeholders.js.map +1 -0
- package/package.json +43 -0
- package/src/commands/api.ts +126 -0
- package/src/commands/config.ts +433 -0
- package/src/commands/init.ts +879 -0
- package/src/commands/install.ts +23 -0
- package/src/commands/integrations.ts +332 -0
- package/src/commands/list.ts +197 -0
- package/src/commands/logs.ts +119 -0
- package/src/commands/ollama.ts +168 -0
- package/src/commands/project.ts +167 -0
- package/src/commands/registry/audit.ts +87 -0
- package/src/commands/registry/index.ts +63 -0
- package/src/commands/registry/install.ts +222 -0
- package/src/commands/registry/publish.ts +112 -0
- package/src/commands/registry/remove.ts +89 -0
- package/src/commands/registry/search.ts +93 -0
- package/src/commands/registry/sync.ts +24 -0
- package/src/commands/registry/update.ts +63 -0
- package/src/commands/replay.ts +559 -0
- package/src/commands/run.ts +454 -0
- package/src/commands/status.ts +163 -0
- package/src/commands/template/index.ts +59 -0
- package/src/commands/template/validate.ts +175 -0
- package/src/commands/templates.ts +99 -0
- package/src/commands/tools.ts +227 -0
- package/src/commands/users.ts +127 -0
- package/src/commands/validate.ts +46 -0
- package/src/config.ts +101 -0
- package/src/index.ts +201 -0
- package/src/models-cache.ts +127 -0
- package/src/output/file-changes.ts +97 -0
- package/src/output/formatter.ts +85 -0
- package/src/output/formatters.ts +303 -0
- package/src/output/logger.ts +18 -0
- package/src/output/parallel-progress.ts +103 -0
- package/src/output/progress.ts +414 -0
- package/src/provider-validator.ts +109 -0
- package/src/registry/cache.ts +46 -0
- package/src/registry/client.ts +84 -0
- package/src/registry/lockfile.ts +68 -0
- package/src/registry/resolver.ts +113 -0
- package/src/registry/types.ts +66 -0
- package/src/run-logger.ts +66 -0
- package/src/run-store-factory.ts +30 -0
- package/src/studio-dir.ts +28 -0
- package/src/utils/input-wizard.ts +73 -0
- package/src/utils/placeholders.ts +10 -0
- package/templates/.studiorc.yaml +16 -0
- package/templates/projects/blank/metadata.json +7 -0
- package/templates/studio-config.yaml +14 -0
- package/tests/__stubs__/studio-runner.ts +104 -0
- package/tests/commands/api.test.ts +110 -0
- package/tests/commands/config.test.ts +221 -0
- package/tests/commands/init.test.ts +919 -0
- package/tests/commands/install.test.ts +52 -0
- package/tests/commands/integrations.test.ts +158 -0
- package/tests/commands/ollama.test.ts +139 -0
- package/tests/commands/project.test.ts +179 -0
- package/tests/commands/registry/audit.test.ts +56 -0
- package/tests/commands/registry/install.test.ts +200 -0
- package/tests/commands/registry/publish.test.ts +56 -0
- package/tests/commands/registry/remove.test.ts +103 -0
- package/tests/commands/registry/search.test.ts +44 -0
- package/tests/commands/registry/sync.test.ts +37 -0
- package/tests/commands/registry/update.test.ts +62 -0
- package/tests/commands/replay.test.ts +283 -0
- package/tests/commands/template/validate.test.ts +150 -0
- package/tests/commands/templates.test.ts +42 -0
- package/tests/commands/tools.test.ts +106 -0
- package/tests/config.test.ts +142 -0
- package/tests/formatter.test.ts +158 -0
- package/tests/integration/sigint.test.ts +188 -0
- package/tests/models-cache.test.ts +250 -0
- package/tests/output/file-changes.test.ts +178 -0
- package/tests/output/formatters.test.ts +448 -0
- package/tests/output/progress-spinner.test.ts +232 -0
- package/tests/output/progress-timer.test.ts +230 -0
- package/tests/provider-validator.test.ts +182 -0
- package/tests/registry/cache.test.ts +66 -0
- package/tests/registry/client.test.ts +70 -0
- package/tests/registry/lockfile.test.ts +87 -0
- package/tests/registry/resolver.test.ts +122 -0
- package/tests/run-logger-events.test.ts +326 -0
- package/tests/run-logger.test.ts +64 -0
- package/tests/run-store-factory.test.ts +51 -0
- package/tests/studio-dir.test.ts +31 -0
- package/tests/utils/input-wizard.test.ts +153 -0
- package/tests/utils/placeholders.test.ts +36 -0
- package/tsconfig.json +24 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
setCachedModels,
|
|
4
|
+
getCachedModels,
|
|
5
|
+
parseAndCacheModels,
|
|
6
|
+
getAvailableModels,
|
|
7
|
+
filterModels,
|
|
8
|
+
sortModels,
|
|
9
|
+
} from '../src/models-cache.js';
|
|
10
|
+
|
|
11
|
+
// ─── filterModels ────────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
describe('filterModels — anthropic', () => {
|
|
14
|
+
it('keeps claude-* models', () => {
|
|
15
|
+
const models = [
|
|
16
|
+
{ id: 'claude-sonnet-4-20250514' },
|
|
17
|
+
{ id: 'claude-haiku-4-20250514' },
|
|
18
|
+
];
|
|
19
|
+
expect(filterModels('anthropic', models)).toEqual([
|
|
20
|
+
'claude-sonnet-4-20250514',
|
|
21
|
+
'claude-haiku-4-20250514',
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('excludes claude-instant*', () => {
|
|
26
|
+
const models = [
|
|
27
|
+
{ id: 'claude-sonnet-4-20250514' },
|
|
28
|
+
{ id: 'claude-instant-1' },
|
|
29
|
+
];
|
|
30
|
+
expect(filterModels('anthropic', models)).not.toContain('claude-instant-1');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('excludes claude-2*', () => {
|
|
34
|
+
const models = [
|
|
35
|
+
{ id: 'claude-sonnet-4-20250514' },
|
|
36
|
+
{ id: 'claude-2.1' },
|
|
37
|
+
];
|
|
38
|
+
expect(filterModels('anthropic', models)).not.toContain('claude-2.1');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('excludes non-claude models', () => {
|
|
42
|
+
const models = [{ id: 'gpt-4o' }, { id: 'claude-sonnet-4-20250514' }];
|
|
43
|
+
expect(filterModels('anthropic', models)).toEqual(['claude-sonnet-4-20250514']);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('filterModels — openai', () => {
|
|
48
|
+
it('keeps gpt-4* models', () => {
|
|
49
|
+
const models = [{ id: 'gpt-4o' }, { id: 'gpt-4-turbo' }];
|
|
50
|
+
const result = filterModels('openai', models);
|
|
51
|
+
expect(result).toContain('gpt-4o');
|
|
52
|
+
expect(result).toContain('gpt-4-turbo');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('keeps o1* and o3* models', () => {
|
|
56
|
+
const models = [{ id: 'o1-preview' }, { id: 'o3-mini' }, { id: 'gpt-3.5-turbo' }];
|
|
57
|
+
const result = filterModels('openai', models);
|
|
58
|
+
expect(result).toContain('o1-preview');
|
|
59
|
+
expect(result).toContain('o3-mini');
|
|
60
|
+
expect(result).not.toContain('gpt-3.5-turbo');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('excludes gpt-3.5* models', () => {
|
|
64
|
+
const models = [{ id: 'gpt-3.5-turbo' }, { id: 'gpt-4o' }];
|
|
65
|
+
expect(filterModels('openai', models)).toEqual(['gpt-4o']);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('filterModels — unknown provider', () => {
|
|
70
|
+
it('keeps all models for unknown providers', () => {
|
|
71
|
+
const models = [{ id: 'model-a' }, { id: 'model-b' }];
|
|
72
|
+
expect(filterModels('future-provider', models)).toEqual(['model-a', 'model-b']);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ─── sortModels ──────────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
describe('sortModels', () => {
|
|
79
|
+
it('sorts by created timestamp descending', () => {
|
|
80
|
+
const ids = ['old-model', 'new-model'];
|
|
81
|
+
const raw = [
|
|
82
|
+
{ id: 'old-model', created: 1000 },
|
|
83
|
+
{ id: 'new-model', created: 2000 },
|
|
84
|
+
];
|
|
85
|
+
expect(sortModels(ids, raw)).toEqual(['new-model', 'old-model']);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('sorts by created_at ISO string descending', () => {
|
|
89
|
+
const ids = ['model-a', 'model-b'];
|
|
90
|
+
const raw = [
|
|
91
|
+
{ id: 'model-a', created_at: '2024-01-01T00:00:00Z' },
|
|
92
|
+
{ id: 'model-b', created_at: '2025-01-01T00:00:00Z' },
|
|
93
|
+
];
|
|
94
|
+
expect(sortModels(ids, raw)).toEqual(['model-b', 'model-a']);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('falls back to lexicographic descending when no timestamp', () => {
|
|
98
|
+
const ids = ['claude-haiku', 'claude-sonnet', 'claude-opus'];
|
|
99
|
+
const raw = [{ id: 'claude-haiku' }, { id: 'claude-sonnet' }, { id: 'claude-opus' }];
|
|
100
|
+
const result = sortModels(ids, raw);
|
|
101
|
+
// lexicographic descending: sonnet > opus > haiku
|
|
102
|
+
expect(result[0]).toBe('claude-sonnet');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// ─── setCachedModels / getCachedModels ───────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
describe('setCachedModels / getCachedModels', () => {
|
|
109
|
+
it('returns null when no entry exists', () => {
|
|
110
|
+
expect(getCachedModels('anthropic', 'sk-ant-missing')).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('returns stored models on cache hit', () => {
|
|
114
|
+
const models = ['claude-sonnet-4-20250514', 'claude-haiku-4-20250514'];
|
|
115
|
+
setCachedModels('anthropic', 'sk-ant-test1234', models);
|
|
116
|
+
expect(getCachedModels('anthropic', 'sk-ant-test1234')).toEqual(models);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('uses first 8 chars of apiKey as part of cache key (different full keys, same prefix → same entry)', () => {
|
|
120
|
+
setCachedModels('anthropic', 'sk-ant-AAAAAAAA-different-suffix', ['model-a']);
|
|
121
|
+
// Same first 8 chars: 'sk-ant-A'
|
|
122
|
+
expect(getCachedModels('anthropic', 'sk-ant-AAAAAAAA-other-suffix')).toEqual(['model-a']);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('returns null after TTL expires', () => {
|
|
126
|
+
const realNow = Date.now;
|
|
127
|
+
try {
|
|
128
|
+
// Set cache entry
|
|
129
|
+
Date.now = () => 0;
|
|
130
|
+
setCachedModels('anthropic', 'sk-ant-expiry-test', ['model-x']);
|
|
131
|
+
// Advance time past 24h
|
|
132
|
+
Date.now = () => 25 * 60 * 60 * 1000;
|
|
133
|
+
expect(getCachedModels('anthropic', 'sk-ant-expiry-test')).toBeNull();
|
|
134
|
+
} finally {
|
|
135
|
+
Date.now = realNow;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('different providers do not share cache entries', () => {
|
|
140
|
+
setCachedModels('anthropic', 'sk-ant-shared', ['claude-model']);
|
|
141
|
+
expect(getCachedModels('openai', 'sk-ant-shared')).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// ─── parseAndCacheModels ─────────────────────────────────────────────────────
|
|
146
|
+
|
|
147
|
+
describe('parseAndCacheModels', () => {
|
|
148
|
+
it('parses Anthropic response format, filters, and caches', () => {
|
|
149
|
+
const json = {
|
|
150
|
+
data: [
|
|
151
|
+
{ id: 'claude-sonnet-4-20250514', created_at: '2025-05-14T00:00:00Z' },
|
|
152
|
+
{ id: 'claude-instant-1' }, // excluded
|
|
153
|
+
{ id: 'claude-2.1' }, // excluded
|
|
154
|
+
],
|
|
155
|
+
};
|
|
156
|
+
parseAndCacheModels('anthropic', 'sk-ant-parse-test', json);
|
|
157
|
+
const cached = getCachedModels('anthropic', 'sk-ant-parse-test');
|
|
158
|
+
expect(cached).toContain('claude-sonnet-4-20250514');
|
|
159
|
+
expect(cached).not.toContain('claude-instant-1');
|
|
160
|
+
expect(cached).not.toContain('claude-2.1');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('parses OpenAI response format', () => {
|
|
164
|
+
const json = {
|
|
165
|
+
data: [
|
|
166
|
+
{ id: 'gpt-4o', created: 2000 },
|
|
167
|
+
{ id: 'gpt-3.5-turbo', created: 1000 }, // excluded
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
parseAndCacheModels('openai', 'sk-parse-openai', json);
|
|
171
|
+
const cached = getCachedModels('openai', 'sk-parse-openai');
|
|
172
|
+
expect(cached).toContain('gpt-4o');
|
|
173
|
+
expect(cached).not.toContain('gpt-3.5-turbo');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('does not throw on malformed JSON', () => {
|
|
177
|
+
expect(() => parseAndCacheModels('anthropic', 'sk-ant-bad', null)).not.toThrow();
|
|
178
|
+
expect(() => parseAndCacheModels('anthropic', 'sk-ant-bad', { no_data: true })).not.toThrow();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ─── getAvailableModels ──────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
const mockFetch = vi.fn();
|
|
185
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
186
|
+
|
|
187
|
+
beforeEach(() => { mockFetch.mockReset(); });
|
|
188
|
+
|
|
189
|
+
describe('getAvailableModels', () => {
|
|
190
|
+
it('returns cached models without fetching', async () => {
|
|
191
|
+
const models = ['claude-sonnet-4-20250514'];
|
|
192
|
+
setCachedModels('anthropic', 'sk-ant-cached', models);
|
|
193
|
+
const result = await getAvailableModels('anthropic', 'sk-ant-cached');
|
|
194
|
+
expect(result).toEqual(models);
|
|
195
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('fetches from Anthropic API on cache miss', async () => {
|
|
199
|
+
mockFetch.mockResolvedValueOnce({
|
|
200
|
+
ok: true,
|
|
201
|
+
json: async () => ({
|
|
202
|
+
data: [{ id: 'claude-sonnet-4-20250514' }, { id: 'claude-instant-1' }],
|
|
203
|
+
}),
|
|
204
|
+
});
|
|
205
|
+
const result = await getAvailableModels('anthropic', 'sk-ant-fetch-test');
|
|
206
|
+
expect(result).toContain('claude-sonnet-4-20250514');
|
|
207
|
+
expect(result).not.toContain('claude-instant-1');
|
|
208
|
+
expect(mockFetch).toHaveBeenCalledOnce();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('fetches from OpenAI API on cache miss', async () => {
|
|
212
|
+
mockFetch.mockResolvedValueOnce({
|
|
213
|
+
ok: true,
|
|
214
|
+
json: async () => ({
|
|
215
|
+
data: [{ id: 'gpt-4o' }, { id: 'gpt-3.5-turbo' }],
|
|
216
|
+
}),
|
|
217
|
+
});
|
|
218
|
+
const result = await getAvailableModels('openai', 'sk-openai-fetch');
|
|
219
|
+
expect(result).toContain('gpt-4o');
|
|
220
|
+
expect(result).not.toContain('gpt-3.5-turbo');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('returns [] on fetch error (never throws)', async () => {
|
|
224
|
+
mockFetch.mockRejectedValueOnce(new Error('ECONNREFUSED'));
|
|
225
|
+
const result = await getAvailableModels('anthropic', 'sk-ant-network-error');
|
|
226
|
+
expect(result).toEqual([]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('returns [] for google provider (not supported)', async () => {
|
|
230
|
+
const result = await getAvailableModels('google', 'AIzaKey');
|
|
231
|
+
expect(result).toEqual([]);
|
|
232
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('returns [] for local provider', async () => {
|
|
236
|
+
const result = await getAvailableModels('local', 'http://localhost:11434');
|
|
237
|
+
expect(result).toEqual([]);
|
|
238
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('populates cache after fetch so second call is free', async () => {
|
|
242
|
+
mockFetch.mockResolvedValueOnce({
|
|
243
|
+
ok: true,
|
|
244
|
+
json: async () => ({ data: [{ id: 'gpt-4o' }] }),
|
|
245
|
+
});
|
|
246
|
+
await getAvailableModels('openai', 'sk-cache-after-fetch');
|
|
247
|
+
await getAvailableModels('openai', 'sk-cache-after-fetch'); // second call
|
|
248
|
+
expect(mockFetch).toHaveBeenCalledOnce(); // still only one fetch
|
|
249
|
+
});
|
|
250
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { FileChangeCollector, formatFileChanges, type FileChange } from '../../src/output/file-changes.js';
|
|
3
|
+
|
|
4
|
+
vi.mock('node:child_process', () => ({
|
|
5
|
+
execSync: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
import { execSync } from 'node:child_process';
|
|
9
|
+
const mockExecSync = vi.mocked(execSync);
|
|
10
|
+
|
|
11
|
+
describe('FileChangeCollector', () => {
|
|
12
|
+
it('collects unique file paths from tool call events', () => {
|
|
13
|
+
const collector = new FileChangeCollector();
|
|
14
|
+
|
|
15
|
+
collector.onToolCallComplete({
|
|
16
|
+
tool: 'repo_manager-write_file',
|
|
17
|
+
result: { path: 'src/app.ts', written: true },
|
|
18
|
+
duration_ms: 10,
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
});
|
|
21
|
+
collector.onToolCallComplete({
|
|
22
|
+
tool: 'repo_manager-read_file',
|
|
23
|
+
result: { content: '...' },
|
|
24
|
+
duration_ms: 5,
|
|
25
|
+
timestamp: Date.now(),
|
|
26
|
+
});
|
|
27
|
+
collector.onToolCallComplete({
|
|
28
|
+
tool: 'repo_manager-write_file',
|
|
29
|
+
result: { path: 'src/app.ts', written: true },
|
|
30
|
+
duration_ms: 8,
|
|
31
|
+
timestamp: Date.now(),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(collector.getWrittenPaths()).toEqual(['src/app.ts']);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('returns empty array when no write calls', () => {
|
|
38
|
+
const collector = new FileChangeCollector();
|
|
39
|
+
expect(collector.getWrittenPaths()).toEqual([]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('ignores failed tool calls', () => {
|
|
43
|
+
const collector = new FileChangeCollector();
|
|
44
|
+
|
|
45
|
+
collector.onToolCallComplete({
|
|
46
|
+
tool: 'repo_manager-write_file',
|
|
47
|
+
result: undefined,
|
|
48
|
+
error: 'Permission denied',
|
|
49
|
+
duration_ms: 10,
|
|
50
|
+
timestamp: Date.now(),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(collector.getWrittenPaths()).toEqual([]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('collects multiple distinct paths in order', () => {
|
|
57
|
+
const collector = new FileChangeCollector();
|
|
58
|
+
|
|
59
|
+
collector.onToolCallComplete({
|
|
60
|
+
tool: 'repo_manager-write_file',
|
|
61
|
+
result: { path: 'src/b.ts', written: true },
|
|
62
|
+
duration_ms: 10,
|
|
63
|
+
timestamp: Date.now(),
|
|
64
|
+
});
|
|
65
|
+
collector.onToolCallComplete({
|
|
66
|
+
tool: 'repo_manager-write_file',
|
|
67
|
+
result: { path: 'src/a.ts', written: true },
|
|
68
|
+
duration_ms: 10,
|
|
69
|
+
timestamp: Date.now(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(collector.getWrittenPaths()).toEqual(['src/b.ts', 'src/a.ts']);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('FileChangeCollector.computeSummary', () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
mockExecSync.mockReset();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('returns null when no files were written', () => {
|
|
82
|
+
const collector = new FileChangeCollector();
|
|
83
|
+
const result = collector.computeSummary('/fake/repo');
|
|
84
|
+
expect(result).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('parses git diff --numstat for modified files', () => {
|
|
88
|
+
mockExecSync.mockReturnValue('15\t3\tsrc/app.ts\n');
|
|
89
|
+
|
|
90
|
+
const collector = new FileChangeCollector();
|
|
91
|
+
collector.onToolCallComplete({
|
|
92
|
+
tool: 'repo_manager-write_file',
|
|
93
|
+
result: { path: 'src/app.ts', written: true },
|
|
94
|
+
duration_ms: 10,
|
|
95
|
+
timestamp: Date.now(),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const summary = collector.computeSummary('/fake/repo');
|
|
99
|
+
expect(summary).toEqual([
|
|
100
|
+
{ path: 'src/app.ts', status: 'M', added: 15, removed: 3 },
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('marks files not in git diff as Added with line count', () => {
|
|
105
|
+
mockExecSync
|
|
106
|
+
.mockReturnValueOnce('') // git diff --numstat returns nothing
|
|
107
|
+
.mockReturnValueOnce('42\n'); // wc -l for the new file
|
|
108
|
+
|
|
109
|
+
const collector = new FileChangeCollector();
|
|
110
|
+
collector.onToolCallComplete({
|
|
111
|
+
tool: 'repo_manager-write_file',
|
|
112
|
+
result: { path: 'src/new.ts', written: true },
|
|
113
|
+
duration_ms: 10,
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const summary = collector.computeSummary('/fake/repo');
|
|
118
|
+
expect(summary).toEqual([
|
|
119
|
+
{ path: 'src/new.ts', status: 'A', added: 42, removed: 0 },
|
|
120
|
+
]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('returns null when git is not available', () => {
|
|
124
|
+
mockExecSync.mockImplementation(() => {
|
|
125
|
+
throw new Error('git not found');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const collector = new FileChangeCollector();
|
|
129
|
+
collector.onToolCallComplete({
|
|
130
|
+
tool: 'repo_manager-write_file',
|
|
131
|
+
result: { path: 'src/app.ts', written: true },
|
|
132
|
+
duration_ms: 10,
|
|
133
|
+
timestamp: Date.now(),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const summary = collector.computeSummary('/fake/repo');
|
|
137
|
+
expect(summary).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('formatFileChanges', () => {
|
|
142
|
+
it('formats modified files with +/- counts', () => {
|
|
143
|
+
const changes: FileChange[] = [
|
|
144
|
+
{ path: 'src/app.ts', status: 'M', added: 15, removed: 3 },
|
|
145
|
+
];
|
|
146
|
+
const lines = formatFileChanges(changes);
|
|
147
|
+
expect(lines).toContain('Changes:');
|
|
148
|
+
expect(lines).toContain('M');
|
|
149
|
+
expect(lines).toContain('src/app.ts');
|
|
150
|
+
expect(lines).toContain('+15');
|
|
151
|
+
expect(lines).toContain('-3');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('formats added files with line count', () => {
|
|
155
|
+
const changes: FileChange[] = [
|
|
156
|
+
{ path: 'src/new.ts', status: 'A', added: 42, removed: 0 },
|
|
157
|
+
];
|
|
158
|
+
const lines = formatFileChanges(changes);
|
|
159
|
+
expect(lines).toContain('A');
|
|
160
|
+
expect(lines).toContain('src/new.ts');
|
|
161
|
+
expect(lines).toContain('new file');
|
|
162
|
+
expect(lines).toContain('42');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('formats mixed M and A files', () => {
|
|
166
|
+
const changes: FileChange[] = [
|
|
167
|
+
{ path: 'src/app.ts', status: 'M', added: 15, removed: 3 },
|
|
168
|
+
{ path: 'src/new.ts', status: 'A', added: 42, removed: 0 },
|
|
169
|
+
];
|
|
170
|
+
const lines = formatFileChanges(changes);
|
|
171
|
+
expect(lines).toContain('M');
|
|
172
|
+
expect(lines).toContain('A');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('returns empty string for empty array', () => {
|
|
176
|
+
expect(formatFileChanges([])).toBe('');
|
|
177
|
+
});
|
|
178
|
+
});
|