@ontrails/testing 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 +23 -0
- package/README.md +221 -0
- package/dist/all.d.ts +30 -0
- package/dist/all.d.ts.map +1 -0
- package/dist/all.js +47 -0
- package/dist/all.js.map +1 -0
- package/dist/assertions.d.ts +49 -0
- package/dist/assertions.d.ts.map +1 -0
- package/dist/assertions.js +84 -0
- package/dist/assertions.js.map +1 -0
- package/dist/context.d.ts +19 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +33 -0
- package/dist/context.js.map +1 -0
- package/dist/contracts.d.ts +16 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +56 -0
- package/dist/contracts.js.map +1 -0
- package/dist/detours.d.ts +12 -0
- package/dist/detours.d.ts.map +1 -0
- package/dist/detours.js +30 -0
- package/dist/detours.js.map +1 -0
- package/dist/examples.d.ts +22 -0
- package/dist/examples.d.ts.map +1 -0
- package/dist/examples.js +187 -0
- package/dist/examples.js.map +1 -0
- package/dist/harness-cli.d.ts +21 -0
- package/dist/harness-cli.d.ts.map +1 -0
- package/dist/harness-cli.js +213 -0
- package/dist/harness-cli.js.map +1 -0
- package/dist/harness-mcp.d.ts +21 -0
- package/dist/harness-mcp.d.ts.map +1 -0
- package/dist/harness-mcp.js +50 -0
- package/dist/harness-mcp.js.map +1 -0
- package/dist/hike.d.ts +32 -0
- package/dist/hike.d.ts.map +1 -0
- package/dist/hike.js +169 -0
- package/dist/hike.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +15 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +87 -0
- package/dist/logger.js.map +1 -0
- package/dist/trail.d.ts +20 -0
- package/dist/trail.d.ts.map +1 -0
- package/dist/trail.js +80 -0
- package/dist/trail.js.map +1 -0
- package/dist/types.d.ts +80 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -0
- package/src/__tests__/context.test.ts +60 -0
- package/src/__tests__/contracts.test.ts +68 -0
- package/src/__tests__/detours.test.ts +55 -0
- package/src/__tests__/examples.test.ts +176 -0
- package/src/__tests__/hike.test.ts +164 -0
- package/src/__tests__/logger.test.ts +136 -0
- package/src/__tests__/trail.test.ts +99 -0
- package/src/all.ts +55 -0
- package/src/assertions.ts +108 -0
- package/src/context.ts +42 -0
- package/src/contracts.ts +85 -0
- package/src/detours.ts +44 -0
- package/src/examples.ts +314 -0
- package/src/harness-cli.ts +310 -0
- package/src/harness-mcp.ts +65 -0
- package/src/hike.ts +283 -0
- package/src/index.ts +40 -0
- package/src/logger.ts +125 -0
- package/src/trail.ts +116 -0
- package/src/types.ts +117 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
package/src/logger.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test logger that captures log records for assertion.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { LogLevel, LogMetadata, LogRecord } from '@ontrails/logging';
|
|
6
|
+
|
|
7
|
+
import type { TestLogger } from './types.js';
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Level ordering for filtering
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
const LEVEL_ORDER: Record<string, number> = {
|
|
14
|
+
debug: 1,
|
|
15
|
+
error: 4,
|
|
16
|
+
fatal: 5,
|
|
17
|
+
info: 2,
|
|
18
|
+
silent: 6,
|
|
19
|
+
trace: 0,
|
|
20
|
+
warn: 3,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Internal factory (shared between root and child loggers)
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
const createTestLoggerInternal = (
|
|
28
|
+
name: string,
|
|
29
|
+
minLevel: LogLevel,
|
|
30
|
+
sharedEntries: LogRecord[],
|
|
31
|
+
baseMetadata: LogMetadata
|
|
32
|
+
): TestLogger => {
|
|
33
|
+
const minOrder = LEVEL_ORDER[minLevel] ?? 0;
|
|
34
|
+
|
|
35
|
+
const shouldLog = (level: LogLevel): boolean =>
|
|
36
|
+
(LEVEL_ORDER[level] ?? 0) >= minOrder;
|
|
37
|
+
|
|
38
|
+
const log = (
|
|
39
|
+
level: LogLevel,
|
|
40
|
+
message: string,
|
|
41
|
+
metadata?: LogMetadata
|
|
42
|
+
): void => {
|
|
43
|
+
if (!shouldLog(level)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const record: LogRecord = {
|
|
47
|
+
category: name,
|
|
48
|
+
level,
|
|
49
|
+
message,
|
|
50
|
+
metadata: { ...baseMetadata, ...metadata },
|
|
51
|
+
timestamp: new Date(),
|
|
52
|
+
};
|
|
53
|
+
sharedEntries.push(record);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
assertLogged(level: LogLevel, messageSubstring: string): void {
|
|
58
|
+
const match = sharedEntries.find(
|
|
59
|
+
(r) => r.level === level && r.message.includes(messageSubstring)
|
|
60
|
+
);
|
|
61
|
+
if (match === undefined) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Expected a log entry with level="${level}" containing "${messageSubstring}", but none was found. ` +
|
|
64
|
+
`Entries: ${JSON.stringify(sharedEntries.map((r) => ({ level: r.level, message: r.message })))}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
child(metadata: LogMetadata): TestLogger {
|
|
70
|
+
const merged = { ...baseMetadata, ...metadata };
|
|
71
|
+
return createTestLoggerInternal(name, minLevel, sharedEntries, merged);
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
clear(): void {
|
|
75
|
+
sharedEntries.length = 0;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
debug(message: string, metadata?: LogMetadata): void {
|
|
79
|
+
log('debug', message, metadata);
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
get entries(): readonly LogRecord[] {
|
|
83
|
+
return sharedEntries;
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
error(message: string, metadata?: LogMetadata): void {
|
|
87
|
+
log('error', message, metadata);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
fatal(message: string, metadata?: LogMetadata): void {
|
|
91
|
+
log('fatal', message, metadata);
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
find(predicate: (record: LogRecord) => boolean): readonly LogRecord[] {
|
|
95
|
+
return sharedEntries.filter(predicate);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
info(message: string, metadata?: LogMetadata): void {
|
|
99
|
+
log('info', message, metadata);
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
name,
|
|
103
|
+
|
|
104
|
+
trace(message: string, metadata?: LogMetadata): void {
|
|
105
|
+
log('trace', message, metadata);
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
warn(message: string, metadata?: LogMetadata): void {
|
|
109
|
+
log('warn', message, metadata);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// createTestLogger
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create a test logger that captures all log records in an array.
|
|
120
|
+
*
|
|
121
|
+
* Records are not printed. Use `entries`, `find()`, and `assertLogged()`
|
|
122
|
+
* to inspect what was logged during a test.
|
|
123
|
+
*/
|
|
124
|
+
export const createTestLogger = (options?: { level?: LogLevel }): TestLogger =>
|
|
125
|
+
createTestLoggerInternal('test', options?.level ?? 'trace', [], {});
|
package/src/trail.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* testTrail — custom scenario testing for individual trails.
|
|
3
|
+
*
|
|
4
|
+
* Use this for edge cases, boundary values, and regression tests
|
|
5
|
+
* that don't belong in `examples` (which are agent-facing documentation).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, test } from 'bun:test';
|
|
9
|
+
|
|
10
|
+
import type { AnyTrail, Result, TrailContext } from '@ontrails/core';
|
|
11
|
+
import { ValidationError, validateInput } from '@ontrails/core';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
assertErrorMatch,
|
|
15
|
+
assertFullMatch,
|
|
16
|
+
assertSchemaMatch,
|
|
17
|
+
expectOk,
|
|
18
|
+
} from './assertions.js';
|
|
19
|
+
import { mergeTestContext } from './context.js';
|
|
20
|
+
import type { TestScenario } from './types.js';
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Helpers
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
const assertScenarioResult = (
|
|
27
|
+
result: Result<unknown, Error>,
|
|
28
|
+
scenario: TestScenario,
|
|
29
|
+
trailDef: AnyTrail
|
|
30
|
+
): void => {
|
|
31
|
+
if (scenario.expectValue !== undefined) {
|
|
32
|
+
assertFullMatch(result, scenario.expectValue);
|
|
33
|
+
} else if (scenario.expectErr !== undefined) {
|
|
34
|
+
assertErrorMatch(result, scenario.expectErr, scenario.expectErrMessage);
|
|
35
|
+
} else if (scenario.expectErrMessage !== undefined) {
|
|
36
|
+
expect(result.isErr()).toBe(true);
|
|
37
|
+
if (result.isErr()) {
|
|
38
|
+
expect(result.error.message).toContain(scenario.expectErrMessage);
|
|
39
|
+
}
|
|
40
|
+
} else if (scenario.expectOk === true) {
|
|
41
|
+
expect(result.isOk()).toBe(true);
|
|
42
|
+
assertSchemaMatch(result, trailDef.output);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Handle input validation failure for a scenario.
|
|
48
|
+
* Returns true if the error was expected and handled.
|
|
49
|
+
* Throws if the error was unexpected.
|
|
50
|
+
*/
|
|
51
|
+
const handleValidationError = (
|
|
52
|
+
validated: Result<unknown, Error>,
|
|
53
|
+
scenario: TestScenario
|
|
54
|
+
): boolean => {
|
|
55
|
+
if (!validated.isErr()) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (scenario.expectErr === ValidationError) {
|
|
60
|
+
expect(validated.error).toBeInstanceOf(ValidationError);
|
|
61
|
+
if (scenario.expectErrMessage !== undefined) {
|
|
62
|
+
expect(validated.error.message).toContain(scenario.expectErrMessage);
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Input validation failed unexpectedly: ${validated.error.message}`
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const runScenario = async (
|
|
73
|
+
trailDef: AnyTrail,
|
|
74
|
+
scenario: TestScenario,
|
|
75
|
+
ctx: Partial<TrailContext> | undefined
|
|
76
|
+
): Promise<void> => {
|
|
77
|
+
const testCtx = mergeTestContext(ctx);
|
|
78
|
+
const validated = validateInput(trailDef.input, scenario.input);
|
|
79
|
+
|
|
80
|
+
if (handleValidationError(validated, scenario)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const validatedInput = expectOk(validated);
|
|
84
|
+
|
|
85
|
+
const result = await trailDef.implementation(validatedInput, testCtx);
|
|
86
|
+
assertScenarioResult(result, scenario, trailDef);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
// testTrail
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Generate a describe block for a trail with one test per scenario.
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* testTrail(myTrail, [
|
|
98
|
+
* { description: "valid input", input: { name: "Alpha" }, expectOk: true },
|
|
99
|
+
* { description: "missing name", input: {}, expectErr: ValidationError },
|
|
100
|
+
* ]);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export const testTrail = (
|
|
104
|
+
trailDef: AnyTrail,
|
|
105
|
+
scenarios: readonly TestScenario[],
|
|
106
|
+
ctx?: Partial<TrailContext>
|
|
107
|
+
): void => {
|
|
108
|
+
describe(trailDef.id, () => {
|
|
109
|
+
test.each([...scenarios])(
|
|
110
|
+
'$description',
|
|
111
|
+
async (scenario: TestScenario) => {
|
|
112
|
+
await runScenario(trailDef, scenario, ctx);
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for @ontrails/testing.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Logger, Topo } from '@ontrails/core';
|
|
6
|
+
import type { LogLevel, LogRecord } from '@ontrails/logging';
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Test Scenario (for testTrail)
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/** A custom test scenario for a single trail. */
|
|
13
|
+
export interface TestScenario {
|
|
14
|
+
/** Description shown in test output. */
|
|
15
|
+
readonly description?: string | undefined;
|
|
16
|
+
/** Assert the result error has this message (substring match). */
|
|
17
|
+
readonly expectErrMessage?: string | undefined;
|
|
18
|
+
/** Assert the result is an error of this type. */
|
|
19
|
+
readonly expectErr?: (new (...args: never[]) => Error) | undefined;
|
|
20
|
+
/** Assert the result is ok. */
|
|
21
|
+
readonly expectOk?: boolean | undefined;
|
|
22
|
+
/** Assert the result value equals this. */
|
|
23
|
+
readonly expectValue?: unknown | undefined;
|
|
24
|
+
/** Input to pass to the implementation. */
|
|
25
|
+
readonly input: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Hike Scenario (for testHike)
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
/** A test scenario for a hike's composition graph. */
|
|
33
|
+
export interface HikeScenario extends TestScenario {
|
|
34
|
+
/** Assert these trail IDs were followed, in order. */
|
|
35
|
+
readonly expectFollowed?: readonly string[] | undefined;
|
|
36
|
+
/** Assert follow counts per trail ID. */
|
|
37
|
+
readonly expectFollowedCount?: Readonly<Record<string, number>> | undefined;
|
|
38
|
+
/** Inject failure from a followed trail's example by description. */
|
|
39
|
+
readonly injectFromExample?: Readonly<Record<string, string>> | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Test Logger
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
/** A logger that captures entries for assertion in tests. */
|
|
47
|
+
export interface TestLogger extends Logger {
|
|
48
|
+
/** All log records captured during the test. */
|
|
49
|
+
readonly entries: readonly LogRecord[];
|
|
50
|
+
/** Clear captured entries. */
|
|
51
|
+
clear(): void;
|
|
52
|
+
/** Find entries matching a predicate. */
|
|
53
|
+
find(predicate: (record: LogRecord) => boolean): readonly LogRecord[];
|
|
54
|
+
/** Assert that at least one entry matches. */
|
|
55
|
+
assertLogged(level: LogLevel, messageSubstring: string): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// Test Trail Context Options
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
/** Options for creating a test trail context. */
|
|
63
|
+
export interface TestTrailContextOptions {
|
|
64
|
+
readonly cwd?: string | undefined;
|
|
65
|
+
readonly env?: Record<string, string> | undefined;
|
|
66
|
+
readonly logger?: Logger | undefined;
|
|
67
|
+
readonly requestId?: string | undefined;
|
|
68
|
+
readonly signal?: AbortSignal | undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// CLI Harness
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
/** Options for creating a CLI harness. */
|
|
76
|
+
export interface CliHarnessOptions {
|
|
77
|
+
readonly app: Topo;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A test harness for CLI commands. */
|
|
81
|
+
export interface CliHarness {
|
|
82
|
+
/** Execute a CLI command string and capture output. */
|
|
83
|
+
run(command: string): Promise<CliHarnessResult>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The result of a CLI harness command execution. */
|
|
87
|
+
export interface CliHarnessResult {
|
|
88
|
+
readonly exitCode: number;
|
|
89
|
+
/** Parsed JSON output if --output json was used. */
|
|
90
|
+
readonly json?: unknown | undefined;
|
|
91
|
+
readonly stderr: string;
|
|
92
|
+
readonly stdout: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// MCP Harness
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/** Options for creating an MCP harness. */
|
|
100
|
+
export interface McpHarnessOptions {
|
|
101
|
+
readonly app: Topo;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A test harness for MCP tools. */
|
|
105
|
+
export interface McpHarness {
|
|
106
|
+
/** Call an MCP tool by name with arguments. */
|
|
107
|
+
callTool(
|
|
108
|
+
name: string,
|
|
109
|
+
args: Record<string, unknown>
|
|
110
|
+
): Promise<McpHarnessResult>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The result of an MCP harness tool invocation. */
|
|
114
|
+
export interface McpHarnessResult {
|
|
115
|
+
readonly content: unknown;
|
|
116
|
+
readonly isError: boolean;
|
|
117
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/all.ts","./src/assertions.ts","./src/context.ts","./src/contracts.ts","./src/detours.ts","./src/examples.ts","./src/harness-cli.ts","./src/harness-mcp.ts","./src/hike.ts","./src/index.ts","./src/logger.ts","./src/trail.ts","./src/types.ts"],"version":"5.9.3"}
|