@leitwerk-dev/test-support 0.1.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.
Files changed (131) hide show
  1. package/dist/LICENSE +13 -0
  2. package/dist/chunk-22HCVJEE.js +294 -0
  3. package/dist/chunk-22HCVJEE.js.map +1 -0
  4. package/dist/chunk-3MQO3NCX.js +15 -0
  5. package/dist/chunk-3MQO3NCX.js.map +1 -0
  6. package/dist/chunk-7ZK5QRGP.js +98 -0
  7. package/dist/chunk-7ZK5QRGP.js.map +1 -0
  8. package/dist/chunk-JUH3LUYK.js +28 -0
  9. package/dist/chunk-JUH3LUYK.js.map +1 -0
  10. package/dist/chunk-KIDBCGNW.js +84 -0
  11. package/dist/chunk-KIDBCGNW.js.map +1 -0
  12. package/dist/chunk-QFCJV2VA.js +600 -0
  13. package/dist/chunk-QFCJV2VA.js.map +1 -0
  14. package/dist/chunk-R4KVUGWR.js +83 -0
  15. package/dist/chunk-R4KVUGWR.js.map +1 -0
  16. package/dist/compact-process-detail-fixture.d.ts +6 -0
  17. package/dist/compact-process-detail-fixture.d.ts.map +1 -0
  18. package/dist/compact-process-detail-fixture.js +73 -0
  19. package/dist/compact-process-detail-fixture.js.map +1 -0
  20. package/dist/fakes/fake-git-ops.d.ts +22 -0
  21. package/dist/fakes/fake-git-ops.d.ts.map +1 -0
  22. package/dist/fakes/fake-git-ops.js +101 -0
  23. package/dist/fakes/fake-git-ops.js.map +1 -0
  24. package/dist/fakes/fake-llm.d.ts +21 -0
  25. package/dist/fakes/fake-llm.d.ts.map +1 -0
  26. package/dist/fakes/fake-llm.js +27 -0
  27. package/dist/fakes/fake-llm.js.map +1 -0
  28. package/dist/fakes/index.d.ts +3 -0
  29. package/dist/fakes/index.d.ts.map +1 -0
  30. package/dist/fakes/index.js +3 -0
  31. package/dist/fakes/index.js.map +1 -0
  32. package/dist/fixtures.d.ts +3 -0
  33. package/dist/fixtures.d.ts.map +1 -0
  34. package/dist/fixtures.js +3 -0
  35. package/dist/fixtures.js.map +1 -0
  36. package/dist/in-process-worker.d.ts +2 -0
  37. package/dist/in-process-worker.d.ts.map +1 -0
  38. package/dist/in-process-worker.js +2 -0
  39. package/dist/in-process-worker.js.map +1 -0
  40. package/dist/index.d.ts +13 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +13 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/integration-harness.d.ts +21 -0
  45. package/dist/integration-harness.d.ts.map +1 -0
  46. package/dist/integration-harness.js +38 -0
  47. package/dist/integration-harness.js.map +1 -0
  48. package/dist/integration.d.ts +5 -0
  49. package/dist/integration.d.ts.map +1 -0
  50. package/dist/integration.js +5 -0
  51. package/dist/integration.js.map +1 -0
  52. package/dist/polling.d.ts +2 -0
  53. package/dist/polling.d.ts.map +1 -0
  54. package/dist/polling.js +14 -0
  55. package/dist/polling.js.map +1 -0
  56. package/dist/question-fixtures.d.ts +4 -0
  57. package/dist/question-fixtures.d.ts.map +1 -0
  58. package/dist/question-fixtures.js +26 -0
  59. package/dist/question-fixtures.js.map +1 -0
  60. package/dist/server-extension-test-harness.d.ts +31 -0
  61. package/dist/server-extension-test-harness.d.ts.map +1 -0
  62. package/dist/server-extension-test-harness.js +167 -0
  63. package/dist/server-extension-test-harness.js.map +1 -0
  64. package/dist/stub-worker-entry.d.ts +2 -0
  65. package/dist/stub-worker-entry.d.ts.map +1 -0
  66. package/dist/stub-worker-entry.js +10 -0
  67. package/dist/stub-worker-entry.js.map +1 -0
  68. package/dist/test-app.d.ts +23 -0
  69. package/dist/test-app.d.ts.map +1 -0
  70. package/dist/test-app.js +47 -0
  71. package/dist/test-app.js.map +1 -0
  72. package/dist/worker-testing/in-process-worker-spawn.d.ts +15 -0
  73. package/dist/worker-testing/in-process-worker-spawn.d.ts.map +1 -0
  74. package/dist/worker-testing/in-process-worker-spawn.js +61 -0
  75. package/dist/worker-testing/in-process-worker-spawn.js.map +1 -0
  76. package/dist/worker-testing/index.d.ts +8 -0
  77. package/dist/worker-testing/index.d.ts.map +1 -0
  78. package/dist/worker-testing/index.js +8 -0
  79. package/dist/worker-testing/index.js.map +1 -0
  80. package/dist/worker-testing/ipc-harness.d.ts +11 -0
  81. package/dist/worker-testing/ipc-harness.d.ts.map +1 -0
  82. package/dist/worker-testing/ipc-harness.js +17 -0
  83. package/dist/worker-testing/ipc-harness.js.map +1 -0
  84. package/dist/worker-testing/schema-driven-stub-pi.d.ts +8 -0
  85. package/dist/worker-testing/schema-driven-stub-pi.d.ts.map +1 -0
  86. package/dist/worker-testing/schema-driven-stub-pi.js +65 -0
  87. package/dist/worker-testing/schema-driven-stub-pi.js.map +1 -0
  88. package/dist/worker-testing/stub-pi-controls.d.ts +7 -0
  89. package/dist/worker-testing/stub-pi-controls.d.ts.map +1 -0
  90. package/dist/worker-testing/stub-pi-controls.js +10 -0
  91. package/dist/worker-testing/stub-pi-controls.js.map +1 -0
  92. package/dist/worker-testing/stub-pi-tree-handle.d.ts +92 -0
  93. package/dist/worker-testing/stub-pi-tree-handle.d.ts.map +1 -0
  94. package/dist/worker-testing/stub-pi-tree-handle.js +515 -0
  95. package/dist/worker-testing/stub-pi-tree-handle.js.map +1 -0
  96. package/dist/worker-testing/worker-runtime-harness.d.ts +53 -0
  97. package/dist/worker-testing/worker-runtime-harness.d.ts.map +1 -0
  98. package/dist/worker-testing/worker-runtime-harness.js +210 -0
  99. package/dist/worker-testing/worker-runtime-harness.js.map +1 -0
  100. package/dist/worker-testing/worker-start-payload.d.ts +25 -0
  101. package/dist/worker-testing/worker-start-payload.d.ts.map +1 -0
  102. package/dist/worker-testing/worker-start-payload.js +101 -0
  103. package/dist/worker-testing/worker-start-payload.js.map +1 -0
  104. package/dist/workspace-source-aliases.d.ts +6 -0
  105. package/dist/workspace-source-aliases.d.ts.map +1 -0
  106. package/dist/workspace-source-aliases.js +126 -0
  107. package/dist/workspace-source-aliases.js.map +1 -0
  108. package/package.json +66 -0
  109. package/src/compact-process-detail-fixture.ts +117 -0
  110. package/src/fakes/fake-git-ops.ts +109 -0
  111. package/src/fakes/fake-llm.ts +39 -0
  112. package/src/fakes/index.ts +2 -0
  113. package/src/fixtures.ts +2 -0
  114. package/src/in-process-worker.ts +4 -0
  115. package/src/index.ts +45 -0
  116. package/src/integration-harness.ts +75 -0
  117. package/src/integration.ts +14 -0
  118. package/src/polling.ts +17 -0
  119. package/src/question-fixtures.ts +32 -0
  120. package/src/server-extension-test-harness.ts +204 -0
  121. package/src/stub-worker-entry.ts +11 -0
  122. package/src/test-app.ts +82 -0
  123. package/src/worker-testing/in-process-worker-spawn.ts +84 -0
  124. package/src/worker-testing/index.ts +36 -0
  125. package/src/worker-testing/ipc-harness.ts +19 -0
  126. package/src/worker-testing/schema-driven-stub-pi.ts +83 -0
  127. package/src/worker-testing/stub-pi-controls.ts +16 -0
  128. package/src/worker-testing/stub-pi-tree-handle.ts +687 -0
  129. package/src/worker-testing/worker-runtime-harness.ts +280 -0
  130. package/src/worker-testing/worker-start-payload.ts +135 -0
  131. package/src/workspace-source-aliases.ts +171 -0
@@ -0,0 +1,82 @@
1
+ import type { ExtensionCatalog } from "@leitwerk-dev/extension-runtime";
2
+ import type { ProvidedCapability } from "@leitwerk-dev/process-sdk";
3
+ import {
4
+ type AppContext,
5
+ createAppContext,
6
+ getDefaultConfig,
7
+ type LeitwerkConfig,
8
+ } from "@leitwerk-dev/server";
9
+ import type { FastifyInstance } from "fastify";
10
+ import { FakeLlmProvider } from "./fakes/fake-llm.js";
11
+ import { createInProcessWorkerSpawn } from "./in-process-worker.js";
12
+
13
+ export interface TestAppBase {
14
+ server: FastifyInstance;
15
+ ctx: AppContext;
16
+ address: string;
17
+ llm: FakeLlmProvider;
18
+ close: () => Promise<void>;
19
+ }
20
+
21
+ export type TestApp<TResources extends Record<string, unknown> = Record<string, never>> =
22
+ TestAppBase & TResources;
23
+
24
+ export interface TestAppOptions<
25
+ TResources extends Record<string, unknown> = Record<string, never>,
26
+ > {
27
+ extensionCatalog: ExtensionCatalog | Promise<ExtensionCatalog>;
28
+ preProvidedCapabilities?: readonly ProvidedCapability[];
29
+ resources?: TResources;
30
+ listen?: boolean;
31
+ inProcessWorkers?: boolean;
32
+ configureConfig?: (config: LeitwerkConfig) => void;
33
+ }
34
+
35
+ export async function createTestApp<
36
+ TResources extends Record<string, unknown> = Record<string, never>,
37
+ >(opts: TestAppOptions<TResources>): Promise<TestApp<TResources>> {
38
+ const llm = new FakeLlmProvider();
39
+ const config = getDefaultConfig();
40
+ config.workers.runner = "local";
41
+ config.workers.shutdown_grace_period = "100ms";
42
+ opts.configureConfig?.(config);
43
+
44
+ const ctx = await createAppContext({
45
+ logger: false,
46
+ config,
47
+ // Default to in-process stub workers so integration tests do not boot the
48
+ // real Pi runtime or touch a developer's ambient Pi setup.
49
+ extensionCatalog: opts.extensionCatalog,
50
+ preProvidedCapabilities: opts.preProvidedCapabilities,
51
+ ...(opts.inProcessWorkers !== false
52
+ ? {
53
+ localWorkerSpawnImpl: createInProcessWorkerSpawn({
54
+ extensionCatalog: opts.extensionCatalog,
55
+ }),
56
+ }
57
+ : {}),
58
+ });
59
+
60
+ let address = "";
61
+ if (opts.listen !== false || config.workers.runner === "local") {
62
+ await ctx.app.listen({ host: "127.0.0.1", port: 0 });
63
+ const addressInfo = ctx.app.server.address();
64
+ const port = typeof addressInfo === "object" && addressInfo ? addressInfo.port : 0;
65
+ address = `http://127.0.0.1:${port}`;
66
+ ctx.config.server.base_url = address;
67
+ }
68
+
69
+ return {
70
+ server: ctx.app,
71
+ ctx,
72
+ address,
73
+ llm,
74
+ ...(opts.resources ?? ({} as TResources)),
75
+ close: async () => {
76
+ await ctx.supervisor.shutdownAll("test_app_close");
77
+ ctx.app.server.closeIdleConnections?.();
78
+ ctx.app.server.closeAllConnections?.();
79
+ await ctx.app.close();
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,84 @@
1
+ import type { ChildProcess, spawn } from "node:child_process";
2
+ import { EventEmitter } from "node:events";
3
+ import { createRequire } from "node:module";
4
+ import { PassThrough } from "node:stream";
5
+ import type { ExtensionCatalog } from "@leitwerk-dev/extension-runtime";
6
+ import type { PiTreeHandleFactory } from "@leitwerk-dev/worker";
7
+ import { createWorkerEntryRuntime } from "@leitwerk-dev/worker";
8
+ import { createSchemaDrivenStubPiFactory } from "./schema-driven-stub-pi.js";
9
+ import type { StubToolCallScriptResolver } from "./stub-pi-tree-handle.js";
10
+
11
+ const require = createRequire(import.meta.url);
12
+ const WebSocket =
13
+ (require("ws") as { WebSocket?: unknown; default?: unknown }).WebSocket ??
14
+ (require("ws") as { default?: unknown }).default ??
15
+ require("ws");
16
+
17
+ export interface InProcessWorkerSpawnOptions {
18
+ extensionCatalog?: ExtensionCatalog | Promise<ExtensionCatalog>;
19
+ piFactory?: PiTreeHandleFactory;
20
+ toolCallScriptResolver?: StubToolCallScriptResolver;
21
+ }
22
+
23
+ /**
24
+ * Fast local-runner-shaped spawn fake. The worker receives the local runner's
25
+ * env and connects over WebSocket just like Docker/Kubernetes workers do.
26
+ */
27
+ export function createInProcessWorkerSpawn(
28
+ options: InProcessWorkerSpawnOptions = {},
29
+ ): typeof spawn {
30
+ const piFactory =
31
+ options.piFactory ??
32
+ createSchemaDrivenStubPiFactory({
33
+ toolCallScriptResolver: options.toolCallScriptResolver,
34
+ });
35
+ return ((
36
+ command: string,
37
+ args: readonly string[] = [],
38
+ spawnOptions?: { env?: NodeJS.ProcessEnv },
39
+ ) => {
40
+ const stderr = new PassThrough();
41
+ const child = new EventEmitter() as unknown as ChildProcess;
42
+ const emitter = child as unknown as EventEmitter;
43
+ let exited = false;
44
+
45
+ let runtime: ReturnType<typeof createWorkerEntryRuntime> | null = null;
46
+ const finish = (code: number) => {
47
+ if (!exited) {
48
+ exited = true;
49
+ emitter.emit("exit", code, null);
50
+ }
51
+ };
52
+
53
+ Object.assign(child, {
54
+ stderr,
55
+ pid: Math.floor(Math.random() * 100_000) + 1_000,
56
+ kill: (_signal?: NodeJS.Signals | number) => {
57
+ finish(0);
58
+ void runtime?.stop("test_kill").catch(() => {});
59
+ return true;
60
+ },
61
+ spawnfile: command,
62
+ spawnargs: [command, ...args],
63
+ });
64
+
65
+ runtime = createWorkerEntryRuntime({
66
+ extensionCatalog: options.extensionCatalog,
67
+ piFactory,
68
+ stderr,
69
+ env: spawnOptions?.env ?? process.env,
70
+ exit: finish,
71
+ });
72
+
73
+ setImmediate(() => {
74
+ if (exited) return;
75
+ (globalThis as unknown as { WebSocket?: unknown }).WebSocket = WebSocket;
76
+ void runtime?.start().catch((error: unknown) => {
77
+ stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
78
+ finish(1);
79
+ });
80
+ });
81
+
82
+ return child;
83
+ }) as typeof spawn;
84
+ }
@@ -0,0 +1,36 @@
1
+ export {
2
+ createInProcessWorkerSpawn,
3
+ type InProcessWorkerSpawnOptions,
4
+ } from "../in-process-worker.js";
5
+ export { baseEnvelope, flushAsyncWork, testConfigSnapshot } from "./ipc-harness.js";
6
+ export {
7
+ createSchemaDrivenStubPiFactory,
8
+ createSchemaDrivenToolCallScriptResolver,
9
+ synthesizeStubArgValue,
10
+ synthesizeStubToolArgs,
11
+ } from "./schema-driven-stub-pi.js";
12
+ export {
13
+ createStubToolScriptController,
14
+ type StubToolScriptController,
15
+ } from "./stub-pi-controls.js";
16
+ export {
17
+ StubPiTreeHandle,
18
+ StubPiTreeHandleFactory,
19
+ type StubPiTreeHandleFactoryOptions,
20
+ type StubToolCallScriptCall,
21
+ type StubToolCallScriptItem,
22
+ type StubToolCallScriptResolver,
23
+ type StubToolCallScriptResolverContext,
24
+ } from "./stub-pi-tree-handle.js";
25
+ export {
26
+ createManualWorkerRuntimeScheduler,
27
+ createWorkerRuntimeHarness,
28
+ type ManualWorkerRuntimeScheduler,
29
+ type WorkerRuntimeHarness,
30
+ type WorkerRuntimeHarnessOptions,
31
+ type WorkerRuntimeObservation,
32
+ } from "./worker-runtime-harness.js";
33
+ export {
34
+ createTestLlmWorkerStartPayload,
35
+ type TestLlmWorkerStartPayloadOptions,
36
+ } from "./worker-start-payload.js";
@@ -0,0 +1,19 @@
1
+ import { createTestConfigSnapshot, IPC_PROTOCOL_VERSION } from "@leitwerk-dev/worker-protocol";
2
+
3
+ export async function flushAsyncWork(iterations = 30): Promise<void> {
4
+ for (let i = 0; i < iterations; i++) {
5
+ await new Promise<void>((resolve) => setImmediate(resolve));
6
+ }
7
+ }
8
+
9
+ export const testConfigSnapshot = createTestConfigSnapshot;
10
+
11
+ export function baseEnvelope(instanceId: string, workerId: string, messageId: string) {
12
+ return {
13
+ protocol: IPC_PROTOCOL_VERSION as typeof IPC_PROTOCOL_VERSION,
14
+ messageId,
15
+ instanceId,
16
+ workerId,
17
+ sentAt: new Date().toISOString(),
18
+ };
19
+ }
@@ -0,0 +1,83 @@
1
+ import {
2
+ StubPiTreeHandleFactory,
3
+ type StubToolCallScriptResolver,
4
+ type StubToolCallScriptResolverContext,
5
+ } from "./stub-pi-tree-handle.js";
6
+
7
+ function isObjectLike(value: unknown): value is Record<string, unknown> {
8
+ return typeof value === "object" && value !== null;
9
+ }
10
+
11
+ export function synthesizeStubArgValue(key: string, spec: unknown): unknown {
12
+ if (key === "changedProjects") {
13
+ return undefined;
14
+ }
15
+ const type = isObjectLike(spec) && typeof spec.type === "string" ? spec.type : "string";
16
+ switch (type) {
17
+ case "number":
18
+ case "integer":
19
+ return 1;
20
+ case "boolean":
21
+ return true;
22
+ case "array": {
23
+ const itemSpec = isObjectLike(spec) ? spec.items : undefined;
24
+ return [synthesizeStubArgValue(`${key}_item`, itemSpec)];
25
+ }
26
+ case "object":
27
+ return {};
28
+ default:
29
+ return `${key} value`;
30
+ }
31
+ }
32
+
33
+ export function synthesizeStubToolArgs(
34
+ parameters: Record<string, unknown>,
35
+ ): Record<string, unknown> {
36
+ const parameterEntries =
37
+ isObjectLike(parameters) && parameters.type === "object" && isObjectLike(parameters.properties)
38
+ ? Object.entries(parameters.properties)
39
+ : Object.entries(parameters);
40
+ return Object.fromEntries(
41
+ parameterEntries.map(([key, spec]) => [key, synthesizeStubArgValue(key, spec)]),
42
+ );
43
+ }
44
+
45
+ export function createSchemaDrivenToolCallScriptResolver(): StubToolCallScriptResolver {
46
+ return ({ tools }: StubToolCallScriptResolverContext) => {
47
+ const markdownTool = tools.find((tool) => tool.name === "markdown_result");
48
+ const completionTool = tools.find(
49
+ (tool) => tool.name !== "markdown_result" && tool.name !== "upload_result_images",
50
+ );
51
+ if (markdownTool && completionTool) {
52
+ return {
53
+ calls: [
54
+ {
55
+ toolName: markdownTool.name,
56
+ args: synthesizeStubToolArgs(markdownTool.parameters),
57
+ },
58
+ {
59
+ toolName: completionTool.name,
60
+ args: synthesizeStubToolArgs(completionTool.parameters),
61
+ },
62
+ ],
63
+ };
64
+ }
65
+ const tool = markdownTool ?? completionTool ?? tools[0];
66
+ if (!tool) {
67
+ return undefined;
68
+ }
69
+ return {
70
+ toolName: tool.name,
71
+ args: synthesizeStubToolArgs(tool.parameters),
72
+ };
73
+ };
74
+ }
75
+
76
+ export function createSchemaDrivenStubPiFactory(
77
+ options: { toolCallScriptResolver?: StubToolCallScriptResolver } = {},
78
+ ): StubPiTreeHandleFactory {
79
+ return new StubPiTreeHandleFactory({
80
+ toolCallScriptResolver:
81
+ options.toolCallScriptResolver ?? createSchemaDrivenToolCallScriptResolver(),
82
+ });
83
+ }
@@ -0,0 +1,16 @@
1
+ import type { StubToolCallScriptItem, StubToolCallScriptResolver } from "./stub-pi-tree-handle.js";
2
+
3
+ export interface StubToolScriptController {
4
+ readonly resolver: StubToolCallScriptResolver;
5
+ set(script: StubToolCallScriptItem[]): void;
6
+ }
7
+
8
+ export function createStubToolScriptController(): StubToolScriptController {
9
+ const queue: StubToolCallScriptItem[] = [];
10
+ return {
11
+ resolver: () => queue.shift(),
12
+ set(script) {
13
+ queue.splice(0, queue.length, ...script);
14
+ },
15
+ };
16
+ }