forked from garrytan/gstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgstack-developer-profile.test.ts
More file actions
441 lines (395 loc) · 14.9 KB
/
Copy pathgstack-developer-profile.test.ts
File metadata and controls
441 lines (395 loc) · 14.9 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
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
/**
* bin/gstack-developer-profile — subcommand behavior tests.
*
* Covers:
* - --read (legacy /office-hours KEY: VALUE format, with defaults when no profile)
* - --migrate (idempotent; preserves sessions + signals_accumulated)
* - --derive (recomputes inferred from question-log events)
* - --trace <dim> (shows contributing events)
* - --gap (declared vs inferred)
* - --vibe (archetype match from inferred)
* - --check-mismatch (threshold behavior; requires 10+ samples)
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { spawnSync } from 'child_process';
const ROOT = path.resolve(import.meta.dir, '..');
const BIN_DEV = path.join(ROOT, 'bin', 'gstack-developer-profile');
const BIN_LOG = path.join(ROOT, 'bin', 'gstack-question-log');
let tmpHome: string;
beforeEach(() => {
tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-test-'));
});
afterEach(() => {
fs.rmSync(tmpHome, { recursive: true, force: true });
});
function runDev(...args: string[]): { stdout: string; stderr: string; status: number } {
const res = spawnSync(BIN_DEV, args, {
env: { ...process.env, GSTACK_HOME: tmpHome },
encoding: 'utf-8',
cwd: ROOT,
});
return {
stdout: res.stdout ?? '',
stderr: res.stderr ?? '',
status: res.status ?? -1,
};
}
function logQuestion(payload: Record<string, unknown>): number {
const res = spawnSync(BIN_LOG, [JSON.stringify(payload)], {
env: { ...process.env, GSTACK_HOME: tmpHome },
encoding: 'utf-8',
cwd: ROOT,
});
return res.status ?? -1;
}
function writeLegacyProfile(sessions: Array<Record<string, unknown>>) {
const content = sessions.map((s) => JSON.stringify(s)).join('\n') + '\n';
fs.writeFileSync(path.join(tmpHome, 'builder-profile.jsonl'), content);
}
function readProfile(): Record<string, unknown> {
const file = path.join(tmpHome, 'developer-profile.json');
return JSON.parse(fs.readFileSync(file, 'utf-8'));
}
// -----------------------------------------------------------------------
// --read (defaults + compat)
// -----------------------------------------------------------------------
describe('gstack-developer-profile --read', () => {
test('emits defaults when no profile exists (creates stub)', () => {
const r = runDev('--read');
expect(r.status).toBe(0);
expect(r.stdout).toContain('SESSION_COUNT: 0');
expect(r.stdout).toContain('TIER: introduction');
expect(r.stdout).toContain('CROSS_PROJECT: false');
});
test('creates a stub profile file when missing', () => {
runDev('--read');
const file = path.join(tmpHome, 'developer-profile.json');
expect(fs.existsSync(file)).toBe(true);
const p = readProfile();
expect(p.schema_version).toBe(1);
});
test('omits --read flag and still returns default output', () => {
const r = runDev();
expect(r.status).toBe(0);
expect(r.stdout).toContain('TIER:');
});
});
// -----------------------------------------------------------------------
// --migrate (legacy jsonl → unified profile)
// -----------------------------------------------------------------------
describe('gstack-developer-profile --migrate', () => {
test('migrates 3 sessions with signals, resources, topics', () => {
writeLegacyProfile([
{
date: '2026-03-01',
mode: 'builder',
project_slug: 'alpha',
signals: ['taste', 'agency'],
resources_shown: ['https://a.example'],
topics: ['onboarding'],
design_doc: '/tmp/a.md',
assignment: 'watch 3 users',
},
{
date: '2026-03-10',
mode: 'startup',
project_slug: 'beta',
signals: ['named_users', 'pushback', 'taste'],
resources_shown: ['https://b.example'],
topics: ['fit'],
design_doc: '/tmp/b.md',
assignment: 'interview 5',
},
{
date: '2026-04-01',
mode: 'builder',
project_slug: 'alpha',
signals: ['agency'],
resources_shown: [],
topics: ['iter'],
design_doc: '/tmp/c.md',
assignment: 'ship v1',
},
]);
const r = runDev('--migrate');
expect(r.status).toBe(0);
expect(r.stdout).toContain('migrated 3 sessions');
const p = readProfile() as {
sessions: Array<{ project_slug: string; signals: string[] }>;
signals_accumulated: Record<string, number>;
resources_shown: string[];
topics: string[];
};
expect(p.sessions.length).toBe(3);
// Accumulated signals are correctly tallied
expect(p.signals_accumulated.taste).toBe(2);
expect(p.signals_accumulated.agency).toBe(2);
expect(p.signals_accumulated.named_users).toBe(1);
expect(p.signals_accumulated.pushback).toBe(1);
expect(p.resources_shown.length).toBe(2);
expect(p.topics.length).toBe(3);
});
test('idempotent — second migrate is no-op when profile exists', () => {
writeLegacyProfile([{ date: '2026-03-01', mode: 'builder', project_slug: 'x', signals: ['taste'] }]);
runDev('--migrate');
const p1 = readProfile();
const r2 = runDev('--migrate');
expect(r2.stdout).toMatch(/no legacy file|already migrated/);
const p2 = readProfile();
// Sessions count should be identical — migration didn't duplicate
expect((p1 as any).sessions.length).toBe((p2 as any).sessions.length);
});
test('archives legacy file after successful migration', () => {
writeLegacyProfile([{ date: '2026-03-01', mode: 'builder', project_slug: 'x', signals: [] }]);
runDev('--migrate');
// Legacy file should be renamed to *.migrated-<timestamp>
const files = fs.readdirSync(tmpHome);
const archived = files.filter((f) => f.startsWith('builder-profile.jsonl.migrated-'));
expect(archived.length).toBe(1);
// Original name should no longer exist
expect(fs.existsSync(path.join(tmpHome, 'builder-profile.jsonl'))).toBe(false);
});
test('no-op when no legacy file exists', () => {
const r = runDev('--migrate');
expect(r.status).toBe(0);
expect(r.stdout).toContain('no legacy file');
});
});
// -----------------------------------------------------------------------
// --read tier calculation
// -----------------------------------------------------------------------
describe('gstack-developer-profile tier calculation', () => {
test('1-3 sessions → welcome_back', () => {
writeLegacyProfile([
{ date: 'x', mode: 'builder', project_slug: 'a', signals: [] },
{ date: 'x', mode: 'builder', project_slug: 'a', signals: [] },
{ date: 'x', mode: 'builder', project_slug: 'a', signals: [] },
]);
runDev('--migrate');
const r = runDev('--read');
expect(r.stdout).toContain('TIER: welcome_back');
});
test('4-7 sessions → regular', () => {
const sessions = Array.from({ length: 5 }, () => ({
date: 'x',
mode: 'builder',
project_slug: 'a',
signals: [],
}));
writeLegacyProfile(sessions);
runDev('--migrate');
const r = runDev('--read');
expect(r.stdout).toContain('TIER: regular');
});
test('8+ sessions → inner_circle', () => {
const sessions = Array.from({ length: 9 }, () => ({
date: 'x',
mode: 'builder',
project_slug: 'a',
signals: [],
}));
writeLegacyProfile(sessions);
runDev('--migrate');
const r = runDev('--read');
expect(r.stdout).toContain('TIER: inner_circle');
});
});
// -----------------------------------------------------------------------
// --derive: inferred dimensions from question-log events
// -----------------------------------------------------------------------
describe('gstack-developer-profile --derive', () => {
test('derive with no events yields neutral (0.5) dimensions', () => {
runDev('--derive');
const p = readProfile() as {
inferred: { values: Record<string, number>; sample_size: number };
};
expect(p.inferred.sample_size).toBe(0);
expect(p.inferred.values.scope_appetite).toBeCloseTo(0.5, 2);
});
test('derive nudges scope_appetite upward after expand choices', () => {
for (let i = 0; i < 5; i++) {
expect(
logQuestion({
skill: 'plan-ceo-review',
question_id: 'plan-ceo-review-mode',
question_summary: 'mode?',
user_choice: 'expand',
session_id: `s${i}`,
ts: `2026-04-0${i + 1}T10:00:00Z`,
}),
).toBe(0);
}
runDev('--derive');
const p = readProfile() as {
inferred: { values: Record<string, number>; sample_size: number; diversity: Record<string, number> };
};
expect(p.inferred.sample_size).toBe(5);
expect(p.inferred.values.scope_appetite).toBeGreaterThan(0.5);
expect(p.inferred.diversity.question_ids_covered).toBe(1);
expect(p.inferred.diversity.skills_covered).toBe(1);
});
test('derive nudges scope_appetite downward after reduce choices', () => {
for (let i = 0; i < 3; i++) {
logQuestion({
skill: 'plan-ceo-review',
question_id: 'plan-ceo-review-mode',
question_summary: 'mode?',
user_choice: 'reduce',
session_id: `s${i}`,
});
}
runDev('--derive');
const p = readProfile() as { inferred: { values: Record<string, number> } };
expect(p.inferred.values.scope_appetite).toBeLessThan(0.5);
});
test('derive is recomputable — same input, same output', () => {
for (let i = 0; i < 3; i++) {
logQuestion({
skill: 'plan-ceo-review',
question_id: 'plan-ceo-review-mode',
question_summary: 'mode?',
user_choice: 'expand',
session_id: `s${i}`,
});
}
runDev('--derive');
const v1 = (readProfile() as any).inferred.values;
runDev('--derive');
const v2 = (readProfile() as any).inferred.values;
expect(v1).toEqual(v2);
});
test('derive ignores events for questions not in registry (ad-hoc ids)', () => {
logQuestion({
skill: 'plan-ceo-review',
question_id: 'adhoc-unregistered-question',
question_summary: 'mystery',
user_choice: 'anything',
session_id: 's1',
});
runDev('--derive');
const p = readProfile() as { inferred: { values: Record<string, number>; sample_size: number } };
// Sample size counts the log entry, but no signal delta applied
expect(p.inferred.sample_size).toBe(1);
expect(p.inferred.values.scope_appetite).toBeCloseTo(0.5, 2);
});
});
// -----------------------------------------------------------------------
// --trace
// -----------------------------------------------------------------------
describe('gstack-developer-profile --trace <dim>', () => {
test('shows contributing events with delta values', () => {
for (let i = 0; i < 3; i++) {
logQuestion({
skill: 'plan-ceo-review',
question_id: 'plan-ceo-review-mode',
question_summary: 'mode?',
user_choice: 'expand',
session_id: `s${i}`,
});
}
const r = runDev('--trace', 'scope_appetite');
expect(r.stdout).toContain('3 events for scope_appetite');
expect(r.stdout).toContain('plan-ceo-review-mode');
expect(r.stdout).toContain('expand');
});
test('reports no contributions for untouched dimension', () => {
logQuestion({
skill: 'plan-ceo-review',
question_id: 'plan-ceo-review-mode',
question_summary: 'x',
user_choice: 'expand',
session_id: 's1',
});
const r = runDev('--trace', 'autonomy');
expect(r.stdout).toContain('no events contribute to autonomy');
});
test('errors without dimension argument', () => {
const r = runDev('--trace');
expect(r.status).not.toBe(0);
expect(r.stderr).toContain('missing dimension');
});
});
// -----------------------------------------------------------------------
// --gap
// -----------------------------------------------------------------------
describe('gstack-developer-profile --gap', () => {
test('gap is empty when nothing is declared', () => {
runDev('--read');
const r = runDev('--gap');
expect(r.status).toBe(0);
const out = JSON.parse(r.stdout);
expect(out.gap).toEqual({});
});
test('gap computed when declared and inferred both present', () => {
runDev('--read');
const file = path.join(tmpHome, 'developer-profile.json');
const p = readProfile() as any;
p.declared = { scope_appetite: 0.8 };
p.inferred.values.scope_appetite = 0.55;
fs.writeFileSync(file, JSON.stringify(p));
const r = runDev('--gap');
const out = JSON.parse(r.stdout);
expect(out.gap.scope_appetite).toBeCloseTo(0.25, 2);
});
});
// -----------------------------------------------------------------------
// --vibe (archetype match)
// -----------------------------------------------------------------------
describe('gstack-developer-profile --vibe', () => {
test('returns archetype name and description', () => {
runDev('--read');
const r = runDev('--vibe');
expect(r.status).toBe(0);
const lines = r.stdout.trim().split('\n');
expect(lines.length).toBeGreaterThanOrEqual(1);
// Default profile (all 0.5) is closest to Builder-Coach or Polymath
expect(lines[0].length).toBeGreaterThan(0);
});
});
// -----------------------------------------------------------------------
// --check-mismatch
// -----------------------------------------------------------------------
describe('gstack-developer-profile --check-mismatch', () => {
test('reports insufficient data when < 10 events', () => {
runDev('--read');
const r = runDev('--check-mismatch');
expect(r.stdout).toContain('not enough data');
});
test('reports no mismatch when declared tracks inferred closely', () => {
runDev('--read');
const file = path.join(tmpHome, 'developer-profile.json');
const p = readProfile() as any;
p.declared = { scope_appetite: 0.5, architecture_care: 0.5 };
p.inferred.sample_size = 20;
fs.writeFileSync(file, JSON.stringify(p));
const r = runDev('--check-mismatch');
expect(r.stdout).toContain('MISMATCH: none');
});
test('flags dimensions with gap > 0.3 when enough data', () => {
runDev('--read');
const file = path.join(tmpHome, 'developer-profile.json');
const p = readProfile() as any;
p.declared = { scope_appetite: 0.9, autonomy: 0.2 };
p.inferred.values.scope_appetite = 0.4;
p.inferred.values.autonomy = 0.8;
p.inferred.sample_size = 25;
fs.writeFileSync(file, JSON.stringify(p));
const r = runDev('--check-mismatch');
expect(r.stdout).toContain('2 dimension(s) disagree');
expect(r.stdout).toContain('scope_appetite');
expect(r.stdout).toContain('autonomy');
});
});
// -----------------------------------------------------------------------
// Error handling
// -----------------------------------------------------------------------
describe('gstack-developer-profile errors', () => {
test('unknown subcommand exits non-zero', () => {
const r = runDev('--not-a-real-subcommand');
expect(r.status).not.toBe(0);
expect(r.stderr).toContain('unknown subcommand');
});
});