-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBootstrapReadme.test.ts
More file actions
95 lines (83 loc) · 3.42 KB
/
Copy pathBootstrapReadme.test.ts
File metadata and controls
95 lines (83 loc) · 3.42 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
/**
* @license
* Copyright 2026 Steven Roussey <sroussey@gmail.com>
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Executable transcription of the "Isolated context" example in
* `packages/bootstrap/README.md` (and the matching `createOrchestrationContext`
* JSDoc example).
*
* The README used to show `await task.run({ context: ctx })`. `Task.run` takes
* input overrides first and a run config second, and neither `IRunConfig` nor
* `TaskGraphRunConfig` has a `context` key — so with a loosely typed `Input`
* that object is an input override named `context`, the run keeps the global
* registry, and `ctx.dispose()` tears down a registry the task never touched.
* Both halves are pinned below so the snippet cannot silently rot back.
*/
import { createOrchestrationContext } from "@workglow/bootstrap";
import type { IExecuteContext, TaskInput, TaskOutput } from "@workglow/task-graph";
import { Task } from "@workglow/task-graph";
import type { ServiceRegistry } from "@workglow/util";
import { globalServiceRegistry } from "@workglow/util";
import type { DataPortSchema } from "@workglow/util/schema";
import { describe, expect, it } from "vitest";
interface RegistryProbeOutput extends TaskOutput {
isGlobal: boolean;
}
/** Reports whether the run resolved against the process-wide registry. */
class RegistryProbeTask extends Task<TaskInput, RegistryProbeOutput> {
public static override readonly type = "RegistryProbeTask";
public static override readonly category = "Test";
public static override readonly title = "Registry probe";
public static override readonly description = "Reports which service registry the run used.";
public static override readonly cacheable = false;
public static override inputSchema(): DataPortSchema {
return {
type: "object",
properties: {},
additionalProperties: true,
} as const satisfies DataPortSchema;
}
public static override outputSchema(): DataPortSchema {
return {
type: "object",
properties: { isGlobal: { type: "boolean" } },
additionalProperties: false,
} as const satisfies DataPortSchema;
}
public seenRegistry: ServiceRegistry | undefined;
public override async execute(
_input: TaskInput,
context: IExecuteContext
): Promise<RegistryProbeOutput> {
this.seenRegistry = context.registry;
return { isGlobal: context.registry === globalServiceRegistry };
}
}
describe("the @workglow/bootstrap README isolated-context example", () => {
it("routes the run to the isolated registry when the context is passed in the run config", async () => {
const ctx = createOrchestrationContext();
const task = new RegistryProbeTask();
try {
const output = await task.run({}, { registry: ctx.registry });
expect(output.isGlobal).toBe(false);
expect(task.seenRegistry).toBe(ctx.registry);
} finally {
await ctx.dispose();
}
});
it("keeps the global registry when a context is passed as an input override instead", async () => {
const ctx = createOrchestrationContext();
const task = new RegistryProbeTask();
try {
// The shape the README used to document. It type-checks only because this
// task's input schema is open; the run silently ignores it.
const output = await task.run({ context: ctx });
expect(output.isGlobal).toBe(true);
expect(task.seenRegistry).not.toBe(ctx.registry);
} finally {
await ctx.dispose();
}
});
});