@ontrails/trails 1.0.0-beta.0
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/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-lint.log +3 -0
- package/.turbo/turbo-typecheck.log +1 -0
- package/CHANGELOG.md +12 -0
- package/__tests__/examples.test.ts +6 -0
- package/bin/trails.ts +3 -0
- package/dist/bin/trails.d.ts +3 -0
- package/dist/bin/trails.d.ts.map +1 -0
- package/dist/bin/trails.js +4 -0
- package/dist/bin/trails.js.map +1 -0
- package/dist/src/app.d.ts +2 -0
- package/dist/src/app.d.ts.map +1 -0
- package/dist/src/app.js +11 -0
- package/dist/src/app.js.map +1 -0
- package/dist/src/clack.d.ts +9 -0
- package/dist/src/clack.d.ts.map +1 -0
- package/dist/src/clack.js +62 -0
- package/dist/src/clack.js.map +1 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +13 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/trails/add-surface.d.ts +13 -0
- package/dist/src/trails/add-surface.d.ts.map +1 -0
- package/dist/src/trails/add-surface.js +88 -0
- package/dist/src/trails/add-surface.js.map +1 -0
- package/dist/src/trails/add-trail.d.ts +11 -0
- package/dist/src/trails/add-trail.d.ts.map +1 -0
- package/dist/src/trails/add-trail.js +85 -0
- package/dist/src/trails/add-trail.js.map +1 -0
- package/dist/src/trails/add-verify.d.ts +10 -0
- package/dist/src/trails/add-verify.d.ts.map +1 -0
- package/dist/src/trails/add-verify.js +67 -0
- package/dist/src/trails/add-verify.js.map +1 -0
- package/dist/src/trails/create-scaffold.d.ts +15 -0
- package/dist/src/trails/create-scaffold.d.ts.map +1 -0
- package/dist/src/trails/create-scaffold.js +288 -0
- package/dist/src/trails/create-scaffold.js.map +1 -0
- package/dist/src/trails/create.d.ts +22 -0
- package/dist/src/trails/create.d.ts.map +1 -0
- package/dist/src/trails/create.js +121 -0
- package/dist/src/trails/create.js.map +1 -0
- package/dist/src/trails/guide.d.ts +11 -0
- package/dist/src/trails/guide.d.ts.map +1 -0
- package/dist/src/trails/guide.js +80 -0
- package/dist/src/trails/guide.js.map +1 -0
- package/dist/src/trails/load-app.d.ts +4 -0
- package/dist/src/trails/load-app.d.ts.map +1 -0
- package/dist/src/trails/load-app.js +24 -0
- package/dist/src/trails/load-app.js.map +1 -0
- package/dist/src/trails/project.d.ts +8 -0
- package/dist/src/trails/project.d.ts.map +1 -0
- package/dist/src/trails/project.js +43 -0
- package/dist/src/trails/project.js.map +1 -0
- package/dist/src/trails/survey.d.ts +33 -0
- package/dist/src/trails/survey.d.ts.map +1 -0
- package/dist/src/trails/survey.js +225 -0
- package/dist/src/trails/survey.js.map +1 -0
- package/dist/src/trails/warden.d.ts +19 -0
- package/dist/src/trails/warden.d.ts.map +1 -0
- package/dist/src/trails/warden.js +88 -0
- package/dist/src/trails/warden.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +28 -0
- package/src/__tests__/create.test.ts +349 -0
- package/src/__tests__/guide.test.ts +91 -0
- package/src/__tests__/load-app.test.ts +15 -0
- package/src/__tests__/survey.test.ts +161 -0
- package/src/__tests__/warden.test.ts +74 -0
- package/src/app.ts +22 -0
- package/src/clack.ts +89 -0
- package/src/cli.ts +14 -0
- package/src/trails/add-surface.ts +119 -0
- package/src/trails/add-trail.ts +103 -0
- package/src/trails/add-verify.ts +87 -0
- package/src/trails/create-scaffold.ts +352 -0
- package/src/trails/create.ts +203 -0
- package/src/trails/guide.ts +104 -0
- package/src/trails/load-app.ts +37 -0
- package/src/trails/project.ts +51 -0
- package/src/trails/survey.ts +307 -0
- package/src/trails/warden.ts +104 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
rmSync,
|
|
7
|
+
writeFileSync,
|
|
8
|
+
} from 'node:fs';
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
import { basename, dirname, join } from 'node:path';
|
|
11
|
+
|
|
12
|
+
import { Result } from '@ontrails/core';
|
|
13
|
+
|
|
14
|
+
import { addSurface } from '../trails/add-surface.js';
|
|
15
|
+
import { addVerify } from '../trails/add-verify.js';
|
|
16
|
+
import { createRoute } from '../trails/create.js';
|
|
17
|
+
import { createScaffold } from '../trails/create-scaffold.js';
|
|
18
|
+
import { isInsideProject } from '../trails/project.js';
|
|
19
|
+
|
|
20
|
+
type Starter = 'empty' | 'entity' | 'hello';
|
|
21
|
+
type Surface = 'cli' | 'mcp';
|
|
22
|
+
|
|
23
|
+
const makeTempProject = (): string =>
|
|
24
|
+
join(
|
|
25
|
+
tmpdir(),
|
|
26
|
+
`trails-blaze-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const readJson = (dir: string, relativePath: string): Record<string, unknown> =>
|
|
30
|
+
JSON.parse(readFileSync(join(dir, relativePath), 'utf8')) as Record<
|
|
31
|
+
string,
|
|
32
|
+
unknown
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
const readText = (dir: string, relativePath: string): string =>
|
|
36
|
+
readFileSync(join(dir, relativePath), 'utf8');
|
|
37
|
+
|
|
38
|
+
const expectPaths = (
|
|
39
|
+
dir: string,
|
|
40
|
+
relativePaths: readonly string[],
|
|
41
|
+
expected: boolean
|
|
42
|
+
): void => {
|
|
43
|
+
for (const relativePath of relativePaths) {
|
|
44
|
+
expect(existsSync(join(dir, relativePath))).toBe(expected);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const expectContainsAll = (
|
|
49
|
+
content: string,
|
|
50
|
+
snippets: readonly string[]
|
|
51
|
+
): void => {
|
|
52
|
+
for (const snippet of snippets) {
|
|
53
|
+
expect(content).toContain(snippet);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const expectOk = <T>(result: Result<T, Error>): T => {
|
|
58
|
+
if (result.isErr()) {
|
|
59
|
+
throw result.error;
|
|
60
|
+
}
|
|
61
|
+
return result.value;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const expectErr = <T>(result: Result<T, Error>): Error => {
|
|
65
|
+
if (result.isOk()) {
|
|
66
|
+
throw new Error('Expected error result');
|
|
67
|
+
}
|
|
68
|
+
return result.error;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const runFollow = async (
|
|
72
|
+
id: string,
|
|
73
|
+
input: unknown
|
|
74
|
+
): Promise<Result<unknown, Error>> => {
|
|
75
|
+
switch (id) {
|
|
76
|
+
case 'create.scaffold': {
|
|
77
|
+
return await createScaffold.implementation(input as never, {} as never);
|
|
78
|
+
}
|
|
79
|
+
case 'add.surface': {
|
|
80
|
+
return await addSurface.implementation(input as never, {} as never);
|
|
81
|
+
}
|
|
82
|
+
case 'add.verify': {
|
|
83
|
+
return await addVerify.implementation(input as never, {} as never);
|
|
84
|
+
}
|
|
85
|
+
default: {
|
|
86
|
+
return Result.err(new Error(`Unknown follow target: ${id}`));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const runBlaze = (
|
|
92
|
+
projectDir: string,
|
|
93
|
+
overrides?: Partial<{
|
|
94
|
+
starter: Starter;
|
|
95
|
+
surfaces: readonly Surface[];
|
|
96
|
+
verify: boolean;
|
|
97
|
+
}>
|
|
98
|
+
) =>
|
|
99
|
+
createRoute.implementation(
|
|
100
|
+
{
|
|
101
|
+
dir: dirname(projectDir),
|
|
102
|
+
name: basename(projectDir),
|
|
103
|
+
starter: overrides?.starter ?? 'hello',
|
|
104
|
+
surfaces: [...(overrides?.surfaces ?? ['cli'])],
|
|
105
|
+
verify: overrides?.verify ?? true,
|
|
106
|
+
},
|
|
107
|
+
{ follow: runFollow } as never
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const setupMinimalProject = (dir: string): void => {
|
|
111
|
+
mkdirSync(join(dir, 'src'), { recursive: true });
|
|
112
|
+
mkdirSync(join(dir, '.trails'), { recursive: true });
|
|
113
|
+
writeFileSync(
|
|
114
|
+
join(dir, 'src', 'app.ts'),
|
|
115
|
+
"import { topo } from '@ontrails/core';\nexport const app = topo('test');\n"
|
|
116
|
+
);
|
|
117
|
+
writeFileSync(
|
|
118
|
+
join(dir, 'package.json'),
|
|
119
|
+
JSON.stringify(
|
|
120
|
+
{ dependencies: { '@ontrails/core': 'workspace:*' }, name: 'test' },
|
|
121
|
+
null,
|
|
122
|
+
2
|
|
123
|
+
)
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const assertDefaultProjectFiles = (dir: string): void => {
|
|
128
|
+
expectPaths(
|
|
129
|
+
dir,
|
|
130
|
+
[
|
|
131
|
+
'package.json',
|
|
132
|
+
'tsconfig.json',
|
|
133
|
+
'.gitignore',
|
|
134
|
+
'.oxlintrc.json',
|
|
135
|
+
'.oxfmtrc.jsonc',
|
|
136
|
+
'src/app.ts',
|
|
137
|
+
'.trails',
|
|
138
|
+
'src/cli.ts',
|
|
139
|
+
'src/trails/hello.ts',
|
|
140
|
+
'__tests__/examples.test.ts',
|
|
141
|
+
'lefthook.yml',
|
|
142
|
+
],
|
|
143
|
+
true
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const assertCliPackage = (dir: string): void => {
|
|
148
|
+
const pkg = readJson(dir, 'package.json');
|
|
149
|
+
expect(pkg['name']).toBe(basename(dir));
|
|
150
|
+
|
|
151
|
+
const deps = pkg['dependencies'] as Record<string, string>;
|
|
152
|
+
expect(deps['@ontrails/core']).toBe('workspace:*');
|
|
153
|
+
expect(deps['@ontrails/cli']).toBe('workspace:*');
|
|
154
|
+
expect(deps['commander']).toBeDefined();
|
|
155
|
+
|
|
156
|
+
const devDeps = pkg['devDependencies'] as Record<string, string>;
|
|
157
|
+
expect(devDeps['@ontrails/testing']).toBe('workspace:*');
|
|
158
|
+
expect(devDeps['@ontrails/warden']).toBe('workspace:*');
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const assertHelloApp = (dir: string): void => {
|
|
162
|
+
expectContainsAll(readText(dir, 'src/app.ts'), [
|
|
163
|
+
'topo',
|
|
164
|
+
`'${basename(dir)}'`,
|
|
165
|
+
'hello',
|
|
166
|
+
]);
|
|
167
|
+
expectContainsAll(readText(dir, 'src/trails/hello.ts'), [
|
|
168
|
+
"import { Result, trail } from '@ontrails/core'",
|
|
169
|
+
'return Result.ok({ message:',
|
|
170
|
+
]);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const assertEntityStarter = (dir: string): void => {
|
|
174
|
+
expectPaths(
|
|
175
|
+
dir,
|
|
176
|
+
[
|
|
177
|
+
'src/trails/entity.ts',
|
|
178
|
+
'src/trails/search.ts',
|
|
179
|
+
'src/trails/onboard.ts',
|
|
180
|
+
'src/events/entity-events.ts',
|
|
181
|
+
'src/store.ts',
|
|
182
|
+
],
|
|
183
|
+
true
|
|
184
|
+
);
|
|
185
|
+
expectPaths(dir, ['src/trails/hello.ts'], false);
|
|
186
|
+
expectContainsAll(readText(dir, 'src/app.ts'), [
|
|
187
|
+
"import * as entity from './trails/entity.js'",
|
|
188
|
+
"import * as search from './trails/search.js'",
|
|
189
|
+
"import * as onboard from './trails/onboard.js'",
|
|
190
|
+
"import * as entityEvents from './events/entity-events.js'",
|
|
191
|
+
]);
|
|
192
|
+
expectContainsAll(readText(dir, 'src/trails/entity.ts'), [
|
|
193
|
+
"import { Result, trail } from '@ontrails/core'",
|
|
194
|
+
'return Result.ok({ id: input.id, name:',
|
|
195
|
+
"return Result.ok({ id: '1', name: input.name })",
|
|
196
|
+
]);
|
|
197
|
+
expectContainsAll(readText(dir, 'src/trails/search.ts'), [
|
|
198
|
+
"import { Result, trail } from '@ontrails/core'",
|
|
199
|
+
'return Result.ok({ results: [] })',
|
|
200
|
+
]);
|
|
201
|
+
expectContainsAll(readText(dir, 'src/trails/onboard.ts'), [
|
|
202
|
+
"import { Result, hike } from '@ontrails/core'",
|
|
203
|
+
'return Result.ok({ onboarded: true })',
|
|
204
|
+
]);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const assertMcpSurface = (dir: string): void => {
|
|
208
|
+
expectPaths(dir, ['src/mcp.ts'], true);
|
|
209
|
+
expectPaths(dir, ['src/cli.ts'], false);
|
|
210
|
+
expectContainsAll(readText(dir, 'src/mcp.ts'), [
|
|
211
|
+
"import { blaze } from '@ontrails/mcp'",
|
|
212
|
+
'await blaze(app)',
|
|
213
|
+
]);
|
|
214
|
+
|
|
215
|
+
const deps = readJson(dir, 'package.json')['dependencies'] as Record<
|
|
216
|
+
string,
|
|
217
|
+
string
|
|
218
|
+
>;
|
|
219
|
+
expect(deps['@ontrails/mcp']).toBe('workspace:*');
|
|
220
|
+
expect(deps['@ontrails/cli']).toBeUndefined();
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const assertVerifySkipped = (dir: string): void => {
|
|
224
|
+
expectPaths(dir, ['__tests__/examples.test.ts', 'lefthook.yml'], false);
|
|
225
|
+
expect(readJson(dir, 'package.json')['devDependencies']).toBeUndefined();
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const assertEmptyStarter = (dir: string): void => {
|
|
229
|
+
expectPaths(dir, ['src/trails/.gitkeep'], true);
|
|
230
|
+
expectPaths(dir, ['src/trails/hello.ts'], false);
|
|
231
|
+
const appContent = readText(dir, 'src/app.ts');
|
|
232
|
+
expect(appContent).toContain(`topo('${basename(dir)}')`);
|
|
233
|
+
expect(appContent).not.toContain('import * as');
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const withTempProject = async (
|
|
237
|
+
assertion: (dir: string) => Promise<void>
|
|
238
|
+
): Promise<void> => {
|
|
239
|
+
const dir = makeTempProject();
|
|
240
|
+
try {
|
|
241
|
+
await assertion(dir);
|
|
242
|
+
} finally {
|
|
243
|
+
rmSync(dir, { force: true, recursive: true });
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
describe('trails blaze', () => {
|
|
248
|
+
describe('create mode', () => {
|
|
249
|
+
test('generates project structure with defaults', async () => {
|
|
250
|
+
await withTempProject(async (dir) => {
|
|
251
|
+
expectOk(await runBlaze(dir));
|
|
252
|
+
assertDefaultProjectFiles(dir);
|
|
253
|
+
assertCliPackage(dir);
|
|
254
|
+
assertHelloApp(dir);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('generates with entity starter', async () => {
|
|
259
|
+
await withTempProject(async (dir) => {
|
|
260
|
+
expectOk(await runBlaze(dir, { starter: 'entity' }));
|
|
261
|
+
assertEntityStarter(dir);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('generates with MCP surface', async () => {
|
|
266
|
+
await withTempProject(async (dir) => {
|
|
267
|
+
expectOk(await runBlaze(dir, { surfaces: ['mcp'] }));
|
|
268
|
+
assertMcpSurface(dir);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('skips verification when verify is false', async () => {
|
|
273
|
+
await withTempProject(async (dir) => {
|
|
274
|
+
expectOk(await runBlaze(dir, { verify: false }));
|
|
275
|
+
assertVerifySkipped(dir);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test('generates with empty starter', async () => {
|
|
280
|
+
await withTempProject(async (dir) => {
|
|
281
|
+
expectOk(await runBlaze(dir, { starter: 'empty' }));
|
|
282
|
+
assertEmptyStarter(dir);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe('add-surface mode', () => {
|
|
288
|
+
test('adds MCP to existing project', async () => {
|
|
289
|
+
await withTempProject(async (dir) => {
|
|
290
|
+
setupMinimalProject(dir);
|
|
291
|
+
const result = expectOk(
|
|
292
|
+
await addSurface.implementation({ dir, surface: 'mcp' }, {} as never)
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
expect(result.created).toBe('src/mcp.ts');
|
|
296
|
+
expect(result.dependency).toBe('@ontrails/mcp');
|
|
297
|
+
expectPaths(dir, ['src/mcp.ts'], true);
|
|
298
|
+
expectContainsAll(readText(dir, 'src/mcp.ts'), [
|
|
299
|
+
"import { blaze } from '@ontrails/mcp'",
|
|
300
|
+
]);
|
|
301
|
+
const deps = readJson(dir, 'package.json')['dependencies'] as Record<
|
|
302
|
+
string,
|
|
303
|
+
string
|
|
304
|
+
>;
|
|
305
|
+
expect(deps['@ontrails/mcp']).toBe('workspace:*');
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test('detects already-blazed surface', async () => {
|
|
310
|
+
await withTempProject(async (dir) => {
|
|
311
|
+
mkdirSync(join(dir, 'src'), { recursive: true });
|
|
312
|
+
mkdirSync(join(dir, '.trails'), { recursive: true });
|
|
313
|
+
writeFileSync(join(dir, 'src', 'mcp.ts'), 'existing content');
|
|
314
|
+
|
|
315
|
+
const error = expectErr(
|
|
316
|
+
await addSurface.implementation({ dir, surface: 'mcp' }, {} as never)
|
|
317
|
+
);
|
|
318
|
+
expect(error.message).toBe('MCP is already blazed. Nothing to do.');
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
describe('isInsideProject', () => {
|
|
325
|
+
test('detects .trails directory', async () => {
|
|
326
|
+
await withTempProject(async (dir) => {
|
|
327
|
+
mkdirSync(join(dir, '.trails'), { recursive: true });
|
|
328
|
+
expect(await isInsideProject(dir)).toBe(true);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('detects topo in src/', async () => {
|
|
333
|
+
await withTempProject(async (dir) => {
|
|
334
|
+
mkdirSync(join(dir, 'src'), { recursive: true });
|
|
335
|
+
writeFileSync(
|
|
336
|
+
join(dir, 'src', 'app.ts'),
|
|
337
|
+
"import { topo } from '@ontrails/core';\nexport const app = topo('app');\n"
|
|
338
|
+
);
|
|
339
|
+
expect(await isInsideProject(dir)).toBe(true);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test('returns false for empty directory', async () => {
|
|
344
|
+
await withTempProject(async (dir) => {
|
|
345
|
+
mkdirSync(dir, { recursive: true });
|
|
346
|
+
expect(await isInsideProject(dir)).toBe(false);
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import type { AnyTrail } from '@ontrails/core';
|
|
4
|
+
import { trail, topo, Result } from '@ontrails/core';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Test fixtures
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
const helloTrail = trail('hello', {
|
|
12
|
+
description: 'Say hello',
|
|
13
|
+
detours: {
|
|
14
|
+
NotFoundError: ['search'],
|
|
15
|
+
},
|
|
16
|
+
examples: [
|
|
17
|
+
{
|
|
18
|
+
expected: { message: 'Hello, world!' },
|
|
19
|
+
input: {},
|
|
20
|
+
name: 'Default greeting',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
expected: { message: 'Hello, Trails!' },
|
|
24
|
+
input: { name: 'Trails' },
|
|
25
|
+
name: 'Named greeting',
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
implementation: (input) => {
|
|
29
|
+
const name = input.name ?? 'world';
|
|
30
|
+
return Result.ok({ message: `Hello, ${name}!` });
|
|
31
|
+
},
|
|
32
|
+
input: z.object({ name: z.string().optional() }),
|
|
33
|
+
output: z.object({ message: z.string() }),
|
|
34
|
+
readOnly: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const app = topo('test-app', { hello: helloTrail });
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Tests
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
describe('trails guide', () => {
|
|
44
|
+
test('lists trails with descriptions', () => {
|
|
45
|
+
const items = app.list();
|
|
46
|
+
expect(items.length).toBe(1);
|
|
47
|
+
|
|
48
|
+
const [hello] = items;
|
|
49
|
+
expect(hello).toBeDefined();
|
|
50
|
+
expect(hello?.id).toBe('hello');
|
|
51
|
+
|
|
52
|
+
expect((hello as AnyTrail).description).toBe('Say hello');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('trail detail includes examples', () => {
|
|
56
|
+
const item = app.get('hello') as AnyTrail;
|
|
57
|
+
expect(item).toBeDefined();
|
|
58
|
+
expect(item.examples).toBeDefined();
|
|
59
|
+
expect(item.examples?.length).toBe(2);
|
|
60
|
+
expect(item.examples?.[0]?.name).toBe('Default greeting');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('JSON output for trail is valid', () => {
|
|
64
|
+
const t = app.get('hello') as AnyTrail;
|
|
65
|
+
expect(t).toBeDefined();
|
|
66
|
+
|
|
67
|
+
const json = JSON.stringify({
|
|
68
|
+
description: t.description,
|
|
69
|
+
detours: t.detours,
|
|
70
|
+
examples: t.examples,
|
|
71
|
+
id: t.id,
|
|
72
|
+
kind: t.kind,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const parsed = JSON.parse(json) as Record<string, unknown>;
|
|
76
|
+
expect(parsed['id']).toBe('hello');
|
|
77
|
+
expect(parsed['kind']).toBe('trail');
|
|
78
|
+
expect(parsed['description']).toBe('Say hello');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('non-existent trail returns undefined from topo', () => {
|
|
82
|
+
const item = app.get('does-not-exist');
|
|
83
|
+
expect(item).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('detours are accessible on trail', () => {
|
|
87
|
+
const t = app.get('hello') as AnyTrail;
|
|
88
|
+
expect(t).toBeDefined();
|
|
89
|
+
expect(t.detours?.['NotFoundError']).toEqual(['search']);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { loadApp } from '../trails/load-app.js';
|
|
5
|
+
|
|
6
|
+
describe('loadApp', () => {
|
|
7
|
+
test('resolves relative module paths from cwd', async () => {
|
|
8
|
+
// import.meta.dir is src/__tests__/, go up two to get apps/trails/
|
|
9
|
+
const cwd = resolve(import.meta.dir, '../..');
|
|
10
|
+
const app = await loadApp('./src/app.ts', cwd);
|
|
11
|
+
|
|
12
|
+
expect(app.name).toBe('trails');
|
|
13
|
+
expect(app.get('survey')).toBeDefined();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { topo, trail, Result } from '@ontrails/core';
|
|
4
|
+
import {
|
|
5
|
+
generateSurfaceMap,
|
|
6
|
+
hashSurfaceMap,
|
|
7
|
+
diffSurfaceMaps,
|
|
8
|
+
} from '@ontrails/schema';
|
|
9
|
+
import type { SurfaceMap } from '@ontrails/schema';
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
|
|
12
|
+
import { generateBriefReport } from '../trails/survey.js';
|
|
13
|
+
import type { BriefReport } from '../trails/survey.js';
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Test fixtures
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
const helloTrail = trail('hello', {
|
|
20
|
+
description: 'Say hello',
|
|
21
|
+
detours: {
|
|
22
|
+
NotFoundError: ['search'],
|
|
23
|
+
},
|
|
24
|
+
examples: [
|
|
25
|
+
{
|
|
26
|
+
expected: { message: 'Hello, world!' },
|
|
27
|
+
input: {},
|
|
28
|
+
name: 'Default greeting',
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
implementation: (input) => {
|
|
32
|
+
const name = input.name ?? 'world';
|
|
33
|
+
return Result.ok({ message: `Hello, ${name}!` });
|
|
34
|
+
},
|
|
35
|
+
input: z.object({ name: z.string().optional() }),
|
|
36
|
+
output: z.object({ message: z.string() }),
|
|
37
|
+
readOnly: true,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const byeTrail = trail('bye', {
|
|
41
|
+
description: 'Say goodbye',
|
|
42
|
+
implementation: (input) => Result.ok({ message: `Goodbye, ${input.name}!` }),
|
|
43
|
+
input: z.object({ name: z.string() }),
|
|
44
|
+
output: z.object({ message: z.string() }),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const app = topo('test-app', { bye: byeTrail, hello: helloTrail });
|
|
48
|
+
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
// Tests
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
describe('trails survey', () => {
|
|
54
|
+
test('generateSurfaceMap includes all trails', () => {
|
|
55
|
+
const surfaceMap = generateSurfaceMap(app);
|
|
56
|
+
expect(surfaceMap.entries.length).toBe(2);
|
|
57
|
+
const ids = surfaceMap.entries.map((e) => e.id);
|
|
58
|
+
expect(ids).toContain('hello');
|
|
59
|
+
expect(ids).toContain('bye');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('surface map entries have expected fields', () => {
|
|
63
|
+
const surfaceMap = generateSurfaceMap(app);
|
|
64
|
+
const hello = surfaceMap.entries.find((e) => e.id === 'hello');
|
|
65
|
+
expect(hello).toBeDefined();
|
|
66
|
+
expect(hello?.kind).toBe('trail');
|
|
67
|
+
expect(hello?.readOnly).toBe(true);
|
|
68
|
+
expect(hello?.exampleCount).toBe(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('JSON output is valid JSON', () => {
|
|
72
|
+
const surfaceMap = generateSurfaceMap(app);
|
|
73
|
+
const json = JSON.stringify(surfaceMap, null, 2);
|
|
74
|
+
const parsed = JSON.parse(json) as SurfaceMap;
|
|
75
|
+
expect(parsed.version).toBe('1.0');
|
|
76
|
+
expect(parsed.entries.length).toBe(2);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('hashSurfaceMap produces stable hash', () => {
|
|
80
|
+
const surfaceMap = generateSurfaceMap(app);
|
|
81
|
+
const hash1 = hashSurfaceMap(surfaceMap);
|
|
82
|
+
const hash2 = hashSurfaceMap(surfaceMap);
|
|
83
|
+
expect(hash1).toBe(hash2);
|
|
84
|
+
// SHA-256 hex
|
|
85
|
+
expect(hash1.length).toBe(64);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('diffSurfaceMaps detects added trails', () => {
|
|
89
|
+
const prev = generateSurfaceMap(topo('test', { hello: helloTrail }));
|
|
90
|
+
const curr = generateSurfaceMap(app);
|
|
91
|
+
const diff = diffSurfaceMaps(prev, curr);
|
|
92
|
+
|
|
93
|
+
expect(diff.info.length).toBeGreaterThan(0);
|
|
94
|
+
const addedBye = diff.info.find((e) => e.id === 'bye');
|
|
95
|
+
expect(addedBye).toBeDefined();
|
|
96
|
+
expect(addedBye?.change).toBe('added');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('diffSurfaceMaps detects removed trails', () => {
|
|
100
|
+
const prev = generateSurfaceMap(app);
|
|
101
|
+
const curr = generateSurfaceMap(topo('test', { hello: helloTrail }));
|
|
102
|
+
const diff = diffSurfaceMaps(prev, curr);
|
|
103
|
+
|
|
104
|
+
expect(diff.hasBreaking).toBe(true);
|
|
105
|
+
const removedBye = diff.breaking.find((e) => e.id === 'bye');
|
|
106
|
+
expect(removedBye).toBeDefined();
|
|
107
|
+
expect(removedBye?.change).toBe('removed');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('diffSurfaceMaps returns empty for identical maps', () => {
|
|
111
|
+
const map = generateSurfaceMap(app);
|
|
112
|
+
const diff = diffSurfaceMaps(map, map);
|
|
113
|
+
expect(diff.entries.length).toBe(0);
|
|
114
|
+
expect(diff.hasBreaking).toBe(false);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
// Brief mode (formerly scout)
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
describe('trails survey --brief', () => {
|
|
123
|
+
test('produces a valid capability report', () => {
|
|
124
|
+
const report = generateBriefReport(app);
|
|
125
|
+
expect(report.name).toBe('test-app');
|
|
126
|
+
expect(report.contractVersion).toBe('2026-03');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('report includes correct trail count', () => {
|
|
130
|
+
const report = generateBriefReport(app);
|
|
131
|
+
expect(report.trails).toBe(2);
|
|
132
|
+
expect(report.hikes).toBe(0);
|
|
133
|
+
expect(report.events).toBe(0);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('detects features in use', () => {
|
|
137
|
+
const report = generateBriefReport(app);
|
|
138
|
+
expect(report.features.outputSchemas).toBe(true);
|
|
139
|
+
expect(report.features.examples).toBe(true);
|
|
140
|
+
expect(report.features.detours).toBe(true);
|
|
141
|
+
expect(report.features.hikes).toBe(false);
|
|
142
|
+
expect(report.features.events).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('JSON output is valid', () => {
|
|
146
|
+
const report = generateBriefReport(app);
|
|
147
|
+
const json = JSON.stringify(report, null, 2);
|
|
148
|
+
const parsed = JSON.parse(json) as BriefReport;
|
|
149
|
+
expect(parsed.name).toBe('test-app');
|
|
150
|
+
expect(parsed.trails).toBe(2);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('empty app reports zero features', () => {
|
|
154
|
+
const emptyApp = topo('empty', {});
|
|
155
|
+
const report = generateBriefReport(emptyApp);
|
|
156
|
+
expect(report.trails).toBe(0);
|
|
157
|
+
expect(report.features.outputSchemas).toBe(false);
|
|
158
|
+
expect(report.features.examples).toBe(false);
|
|
159
|
+
expect(report.features.detours).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { formatWardenReport, runWarden } from '@ontrails/warden';
|
|
7
|
+
|
|
8
|
+
const makeTempDir = (): string => {
|
|
9
|
+
const dir = join(
|
|
10
|
+
tmpdir(),
|
|
11
|
+
`trails-warden-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
12
|
+
);
|
|
13
|
+
mkdirSync(dir, { recursive: true });
|
|
14
|
+
return dir;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe('trails warden', () => {
|
|
18
|
+
test('runs lint + drift checks and produces a report', async () => {
|
|
19
|
+
const dir = makeTempDir();
|
|
20
|
+
try {
|
|
21
|
+
writeFileSync(
|
|
22
|
+
join(dir, 'good.ts'),
|
|
23
|
+
`trail("hello", {
|
|
24
|
+
implementation: async (input, ctx) => {
|
|
25
|
+
return Result.ok({ message: "hi" });
|
|
26
|
+
}
|
|
27
|
+
})`
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const report = await runWarden({ rootDir: dir });
|
|
31
|
+
expect(report.diagnostics).toBeDefined();
|
|
32
|
+
expect(typeof report.errorCount).toBe('number');
|
|
33
|
+
expect(typeof report.warnCount).toBe('number');
|
|
34
|
+
expect(typeof report.passed).toBe('boolean');
|
|
35
|
+
} finally {
|
|
36
|
+
rmSync(dir, { force: true, recursive: true });
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('lintOnly skips drift detection', async () => {
|
|
41
|
+
const dir = makeTempDir();
|
|
42
|
+
try {
|
|
43
|
+
writeFileSync(join(dir, 'empty.ts'), 'export {}');
|
|
44
|
+
const report = await runWarden({ lintOnly: true, rootDir: dir });
|
|
45
|
+
expect(report.drift).toBeNull();
|
|
46
|
+
} finally {
|
|
47
|
+
rmSync(dir, { force: true, recursive: true });
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('driftOnly skips lint rules', async () => {
|
|
52
|
+
const dir = makeTempDir();
|
|
53
|
+
try {
|
|
54
|
+
writeFileSync(
|
|
55
|
+
join(dir, 'bad.ts'),
|
|
56
|
+
`trail("x", {
|
|
57
|
+
implementation: async () => { throw new Error("boom"); }
|
|
58
|
+
})`
|
|
59
|
+
);
|
|
60
|
+
const report = await runWarden({ driftOnly: true, rootDir: dir });
|
|
61
|
+
expect(report.diagnostics.length).toBe(0);
|
|
62
|
+
expect(report.drift).not.toBeNull();
|
|
63
|
+
} finally {
|
|
64
|
+
rmSync(dir, { force: true, recursive: true });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('formatWardenReport produces human-readable output', async () => {
|
|
69
|
+
const report = await runWarden({ rootDir: '/dev/null' });
|
|
70
|
+
const output = formatWardenReport(report);
|
|
71
|
+
expect(output).toContain('Warden Report');
|
|
72
|
+
expect(typeof output).toBe('string');
|
|
73
|
+
});
|
|
74
|
+
});
|