@effect-app/infra 4.0.0-beta.256 → 4.0.0-beta.258
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/CHANGELOG.md +18 -0
- package/dist/WorkflowEngineCosmos.d.ts +29 -0
- package/dist/WorkflowEngineCosmos.d.ts.map +1 -0
- package/dist/WorkflowEngineCosmos.js +521 -0
- package/dist/WorkflowEngineSqlite.d.ts +24 -0
- package/dist/WorkflowEngineSqlite.d.ts.map +1 -0
- package/dist/WorkflowEngineSqlite.js +550 -0
- package/package.json +7 -206
- package/src/WorkflowEngineCosmos.ts +719 -0
- package/src/WorkflowEngineSqlite.ts +813 -0
- package/test/dist/_check-agg-infer.test-d.d.ts +2 -0
- package/test/dist/_check-agg-infer.test-d.d.ts.map +1 -0
- package/test/dist/_check-agg-infer.test-d.js +19 -0
- package/test/dist/_check-proj-infer.test-d.d.ts +2 -0
- package/test/dist/_check-proj-infer.test-d.d.ts.map +1 -0
- package/test/dist/_check-proj-infer.test-d.js +16 -0
- package/test/dist/_check-tighten.test-d.d.ts +2 -0
- package/test/dist/_check-tighten.test-d.d.ts.map +1 -0
- package/test/dist/_check-tighten.test-d.js +21 -0
- package/test/dist/workflow-engine-sqlite.test.d.ts.map +1 -0
- package/test/workflow-engine-cosmos.test.ts +354 -0
- package/test/workflow-engine-sqlite.test.ts +299 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_check-agg-infer.test-d.d.ts","sourceRoot":"","sources":["../_check-agg-infer.test-d.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as S from "effect-app/Schema";
|
|
2
|
+
import { aggregate, make, where } from "effect-app/Model/query";
|
|
3
|
+
const baseSchema = S.Struct({
|
|
4
|
+
id: S.String,
|
|
5
|
+
address: S.Struct({ city: S.String }),
|
|
6
|
+
active: S.Boolean,
|
|
7
|
+
qty: S.Number
|
|
8
|
+
});
|
|
9
|
+
// Should compile — Row inferred from pipe
|
|
10
|
+
const _ok = make().pipe(aggregate(S.Struct({ city: S.String, count: S.NonNegativeInt, total: S.Number }), ($) => ({
|
|
11
|
+
city: $.field("address.city"),
|
|
12
|
+
count: $.countWhen((q) => q.pipe(where("active", true))),
|
|
13
|
+
total: $.sum("qty")
|
|
14
|
+
})));
|
|
15
|
+
// Should ERROR — bad path
|
|
16
|
+
const _bad = make().pipe(aggregate(S.Struct({ a: S.String }), ($) => ({
|
|
17
|
+
a: $.field("nonexistent")
|
|
18
|
+
})));
|
|
19
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiX2NoZWNrLWFnZy1pbmZlci50ZXN0LWQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9fY2hlY2stYWdnLWluZmVyLnRlc3QtZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssQ0FBQyxNQUFNLG1CQUFtQixDQUFBO0FBQ3RDLE9BQU8sRUFBRSxTQUFTLEVBQUUsSUFBSSxFQUFFLEtBQUssRUFBRSxNQUFNLHdCQUF3QixDQUFBO0FBRS9ELE1BQU0sVUFBVSxHQUFHLENBQUMsQ0FBQyxNQUFNLENBQUM7SUFDMUIsRUFBRSxFQUFFLENBQUMsQ0FBQyxNQUFNO0lBQ1osT0FBTyxFQUFFLENBQUMsQ0FBQyxNQUFNLENBQUMsRUFBRSxJQUFJLEVBQUUsQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFDO0lBQ3JDLE1BQU0sRUFBRSxDQUFDLENBQUMsT0FBTztJQUNqQixHQUFHLEVBQUUsQ0FBQyxDQUFDLE1BQU07Q0FDZCxDQUFDLENBQUE7QUFHRiwwQ0FBMEM7QUFDMUMsTUFBTSxHQUFHLEdBQUcsSUFBSSxFQUFPLENBQUMsSUFBSSxDQUMxQixTQUFTLENBQ1AsQ0FBQyxDQUFDLE1BQU0sQ0FBQyxFQUFFLElBQUksRUFBRSxDQUFDLENBQUMsTUFBTSxFQUFFLEtBQUssRUFBRSxDQUFDLENBQUMsY0FBYyxFQUFFLEtBQUssRUFBRSxDQUFDLENBQUMsTUFBTSxFQUFFLENBQUMsRUFDdEUsQ0FBQyxDQUFDLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDTixJQUFJLEVBQUUsQ0FBQyxDQUFDLEtBQUssQ0FBQyxjQUFjLENBQUM7SUFDN0IsS0FBSyxFQUFFLENBQUMsQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLFFBQVEsRUFBRSxJQUFJLENBQUMsQ0FBQyxDQUFDO0lBQ3hELEtBQUssRUFBRSxDQUFDLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQztDQUNwQixDQUFDLENBQ0gsQ0FDRixDQUFBO0FBRUQsMEJBQTBCO0FBQzFCLE1BQU0sSUFBSSxHQUFHLElBQUksRUFBTyxDQUFDLElBQUksQ0FDM0IsU0FBUyxDQUNQLENBQUMsQ0FBQyxNQUFNLENBQUMsRUFBRSxDQUFDLEVBQUUsQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFDLEVBQ3pCLENBQUMsQ0FBQyxFQUFFLEVBQUUsQ0FBQyxDQUFDO0lBQ04sQ0FBQyxFQUFFLENBQUMsQ0FBQyxLQUFLLENBQUMsYUFBYSxDQUFDO0NBQzFCLENBQUMsQ0FDSCxDQUNGLENBQUEifQ==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_check-proj-infer.test-d.d.ts","sourceRoot":"","sources":["../_check-proj-infer.test-d.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as S from "effect-app/Schema";
|
|
2
|
+
import { make, projectComputed, where } from "effect-app/Model/query";
|
|
3
|
+
const baseSchema = S.Struct({
|
|
4
|
+
id: S.String,
|
|
5
|
+
items: S.Array(S.Struct({ articleId: S.String, qty: S.Number, note: S.String }))
|
|
6
|
+
});
|
|
7
|
+
// Should compile — Row inferred from pipe
|
|
8
|
+
const _ok = make().pipe(projectComputed(S.Struct({ id: S.String, total: S.Number, distinctArticles: S.NonNegativeInt }), ({ relation }) => ({
|
|
9
|
+
total: relation("items").sum("qty"),
|
|
10
|
+
distinctArticles: relation("items").distinctCount("articleId", where("qty", "gte", 0))
|
|
11
|
+
})));
|
|
12
|
+
// Should ERROR — bad path on relation element
|
|
13
|
+
const _bad = make().pipe(projectComputed(S.Struct({ a: S.Number }), ({ relation }) => ({
|
|
14
|
+
a: relation("items").sum("nonexistent")
|
|
15
|
+
})));
|
|
16
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiX2NoZWNrLXByb2otaW5mZXIudGVzdC1kLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vX2NoZWNrLXByb2otaW5mZXIudGVzdC1kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxDQUFDLE1BQU0sbUJBQW1CLENBQUE7QUFDdEMsT0FBTyxFQUFFLElBQUksRUFBRSxlQUFlLEVBQUUsS0FBSyxFQUFFLE1BQU0sd0JBQXdCLENBQUE7QUFFckUsTUFBTSxVQUFVLEdBQUcsQ0FBQyxDQUFDLE1BQU0sQ0FBQztJQUMxQixFQUFFLEVBQUUsQ0FBQyxDQUFDLE1BQU07SUFDWixLQUFLLEVBQUUsQ0FBQyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLEVBQUUsU0FBUyxFQUFFLENBQUMsQ0FBQyxNQUFNLEVBQUUsR0FBRyxFQUFFLENBQUMsQ0FBQyxNQUFNLEVBQUUsSUFBSSxFQUFFLENBQUMsQ0FBQyxNQUFNLEVBQUUsQ0FBQyxDQUFDO0NBQ2pGLENBQUMsQ0FBQ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}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as S from "effect-app/Schema";
|
|
2
|
+
import { computed, relation } from "effect-app/Model/query";
|
|
3
|
+
const baseSchema = S.Struct({
|
|
4
|
+
id: S.String,
|
|
5
|
+
items: S.Array(S.Struct({ articleId: S.String, qty: S.Number }))
|
|
6
|
+
});
|
|
7
|
+
// Should compile:
|
|
8
|
+
const _ok = computed({
|
|
9
|
+
a: relation("items").sum("qty"),
|
|
10
|
+
b: relation("items").distinctCount("articleId"),
|
|
11
|
+
c: relation("items").collect("articleId"),
|
|
12
|
+
d: relation("items").collectFields(["articleId", "qty"])
|
|
13
|
+
});
|
|
14
|
+
// Should ERROR — invalid field paths
|
|
15
|
+
const _bad = computed({
|
|
16
|
+
a: relation("items").sum("nonexistent"),
|
|
17
|
+
b: relation("items").distinctCount("not_a_field"),
|
|
18
|
+
c: relation("items").collect("zzz"),
|
|
19
|
+
d: relation("items").collectFields(["zzz"])
|
|
20
|
+
});
|
|
21
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiX2NoZWNrLXRpZ2h0ZW4udGVzdC1kLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vX2NoZWNrLXRpZ2h0ZW4udGVzdC1kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxDQUFDLE1BQU0sbUJBQW1CLENBQUE7QUFDdEMsT0FBTyxFQUFFLFFBQVEsRUFBRSxRQUFRLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQTtBQUUzRCxNQUFNLFVBQVUsR0FBRyxDQUFDLENBQUMsTUFBTSxDQUFDO0lBQzFCLEVBQUUsRUFBRSxDQUFDLENBQUMsTUFBTTtJQUNaLEtBQUssRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxNQUFNLENBQUMsRUFBRSxTQUFTLEVBQUUsQ0FBQyxDQUFDL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ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* Workflow engine conformance tests.
|
|
4
|
+
*
|
|
5
|
+
* The `runSuite` body is engine-agnostic: it runs against
|
|
6
|
+
* `WorkflowEngine.layerMemory` (fast, always) and — when `COSMOS_TEST_URL`
|
|
7
|
+
* is set — against the Cosmos adapter (`layerCosmos`) pointed at a Cosmos
|
|
8
|
+
* emulator. Both must satisfy the same observable contract: idempotent
|
|
9
|
+
* execution, activity replay (no double side effects), suspend → durable-
|
|
10
|
+
* deferred completion → resume with a correctly decoded result, interrupt
|
|
11
|
+
* propagation, and first-writer-wins deferred completion.
|
|
12
|
+
*
|
|
13
|
+
* A second describe block (Cosmos emulator only) exercises adapter
|
|
14
|
+
* internals — the recovery poller (driving execs whose lease has lapsed)
|
|
15
|
+
* and the clock poller (firing past-due clocks) — by seeding rows
|
|
16
|
+
* directly against the Cosmos container.
|
|
17
|
+
*/
|
|
18
|
+
import { assert, describe, it } from "@effect/vitest"
|
|
19
|
+
import { Context, Duration, Effect, Exit, Layer, Option, Redacted, Schema } from "effect"
|
|
20
|
+
import { Activity, DurableDeferred, Workflow, WorkflowEngine } from "effect/unstable/workflow"
|
|
21
|
+
import { CosmosClient, CosmosClientLayer } from "../src/cosmos-client.js"
|
|
22
|
+
import { layerCosmos } from "../src/WorkflowEngineCosmos.js"
|
|
23
|
+
|
|
24
|
+
// --- Shared mutable counter for activity-side-effect assertions --------
|
|
25
|
+
|
|
26
|
+
class CounterRef extends Context.Service<CounterRef, { count: number }>()("CounterRef") {
|
|
27
|
+
static readonly layer = Layer.effect(CounterRef, Effect.sync(() => ({ count: 0 })))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// --- Workflow definitions ----------------------------------------------
|
|
31
|
+
|
|
32
|
+
const IncrementWorkflow = Workflow.make({
|
|
33
|
+
name: "WorkflowEngineCosmos/IncrementWorkflow",
|
|
34
|
+
payload: { value: Schema.Number },
|
|
35
|
+
success: Schema.Number,
|
|
36
|
+
idempotencyKey: ({ value }) => String(value)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const IncrementHandler = IncrementWorkflow.toLayer(({ value }) => Effect.succeed(value + 1))
|
|
40
|
+
|
|
41
|
+
// Counts activity body invocations across re-executes so the test can
|
|
42
|
+
// prove side-effects don't repeat when a persisted result is available.
|
|
43
|
+
const CounterWorkflow = Workflow.make({
|
|
44
|
+
name: "WorkflowEngineCosmos/CounterWorkflow",
|
|
45
|
+
payload: { id: Schema.String },
|
|
46
|
+
success: Schema.Number,
|
|
47
|
+
idempotencyKey: ({ id }) => id
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const CounterHandler = CounterWorkflow.toLayer(Effect.fn(function*() {
|
|
51
|
+
const counter = yield* CounterRef
|
|
52
|
+
return yield* Activity.make({
|
|
53
|
+
name: "Bump",
|
|
54
|
+
success: Schema.Number,
|
|
55
|
+
execute: Effect.sync(() => {
|
|
56
|
+
counter.count++
|
|
57
|
+
return counter.count
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
// Suspends on a deferred, then composes the persisted activity result
|
|
63
|
+
// with the resumed deferred value. Exercises Result/Exit round-trip.
|
|
64
|
+
const Trigger = DurableDeferred.make("WorkflowEngineCosmos/Trigger", { success: Schema.String })
|
|
65
|
+
|
|
66
|
+
const SuspendWorkflow = Workflow.make({
|
|
67
|
+
name: "WorkflowEngineCosmos/SuspendWorkflow",
|
|
68
|
+
payload: { id: Schema.String },
|
|
69
|
+
success: Schema.String,
|
|
70
|
+
idempotencyKey: ({ id }) => id
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const SuspendHandler = SuspendWorkflow.toLayer(Effect.fn(function*({ id }) {
|
|
74
|
+
const n = yield* Activity.make({
|
|
75
|
+
name: "step",
|
|
76
|
+
success: Schema.Number,
|
|
77
|
+
execute: Effect.succeed(1)
|
|
78
|
+
})
|
|
79
|
+
const v = yield* DurableDeferred.await(Trigger)
|
|
80
|
+
return `${id}:${n}:${v}`
|
|
81
|
+
}))
|
|
82
|
+
|
|
83
|
+
// Plain durable-deferred await — used to assert first-writer-wins on done().
|
|
84
|
+
const AwaitOnly = Workflow.make({
|
|
85
|
+
name: "WorkflowEngineCosmos/AwaitOnly",
|
|
86
|
+
payload: { id: Schema.String },
|
|
87
|
+
success: Schema.String,
|
|
88
|
+
idempotencyKey: ({ id }) => id
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const AwaitOnlyHandler = AwaitOnly.toLayer(Effect.fn(function*() {
|
|
92
|
+
return yield* DurableDeferred.await(Trigger)
|
|
93
|
+
}))
|
|
94
|
+
|
|
95
|
+
const Handlers = Layer.mergeAll(IncrementHandler, SuspendHandler, CounterHandler, AwaitOnlyHandler)
|
|
96
|
+
|
|
97
|
+
// Helper that polls until the workflow completes (or `maxIterations` elapse).
|
|
98
|
+
const waitForComplete = (
|
|
99
|
+
workflow: { readonly poll: (id: string) => Effect.Effect<Option.Option<Workflow.Result<any, any>>, never, any> },
|
|
100
|
+
executionId: string,
|
|
101
|
+
step = Duration.millis(10),
|
|
102
|
+
maxIterations = 200
|
|
103
|
+
) =>
|
|
104
|
+
Effect.gen(function*() {
|
|
105
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
106
|
+
const polled = yield* workflow.poll(executionId)
|
|
107
|
+
if (Option.isSome(polled) && polled.value._tag === "Complete") {
|
|
108
|
+
return polled.value
|
|
109
|
+
}
|
|
110
|
+
yield* Effect.sleep(step)
|
|
111
|
+
}
|
|
112
|
+
return undefined
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const runSuite = (engineLayer: Layer.Layer<WorkflowEngine.WorkflowEngine>) => {
|
|
116
|
+
const TestLayer = Handlers.pipe(
|
|
117
|
+
Layer.provideMerge(CounterRef.layer),
|
|
118
|
+
Layer.provideMerge(engineLayer)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
it.live("executes a workflow and polls the result", () =>
|
|
122
|
+
Effect
|
|
123
|
+
.gen(function*() {
|
|
124
|
+
const executionId = yield* IncrementWorkflow.execute({ value: 41 }, { discard: true })
|
|
125
|
+
const result = yield* IncrementWorkflow.execute({ value: 41 })
|
|
126
|
+
const polled = yield* IncrementWorkflow.poll(executionId)
|
|
127
|
+
|
|
128
|
+
assert.strictEqual(result, 42)
|
|
129
|
+
assert(
|
|
130
|
+
Option.isSome(polled)
|
|
131
|
+
&& polled.value._tag === "Complete"
|
|
132
|
+
&& Exit.isSuccess(polled.value.exit)
|
|
133
|
+
)
|
|
134
|
+
assert.strictEqual(polled.value.exit.value, 42)
|
|
135
|
+
})
|
|
136
|
+
.pipe(Effect.provide(TestLayer)))
|
|
137
|
+
|
|
138
|
+
it.live("re-executing the same id is idempotent", () =>
|
|
139
|
+
Effect
|
|
140
|
+
.gen(function*() {
|
|
141
|
+
const a = yield* IncrementWorkflow.execute({ value: 7 })
|
|
142
|
+
const b = yield* IncrementWorkflow.execute({ value: 7 })
|
|
143
|
+
assert.strictEqual(a, b)
|
|
144
|
+
})
|
|
145
|
+
.pipe(Effect.provide(TestLayer)))
|
|
146
|
+
|
|
147
|
+
it.live("activity side effects run once across re-executions", () =>
|
|
148
|
+
Effect
|
|
149
|
+
.gen(function*() {
|
|
150
|
+
const counter = yield* CounterRef
|
|
151
|
+
yield* CounterWorkflow.execute({ id: "once" })
|
|
152
|
+
yield* CounterWorkflow.execute({ id: "once" })
|
|
153
|
+
yield* CounterWorkflow.execute({ id: "once" })
|
|
154
|
+
assert.strictEqual(counter.count, 1)
|
|
155
|
+
})
|
|
156
|
+
.pipe(Effect.provide(TestLayer)))
|
|
157
|
+
|
|
158
|
+
it.live("suspends on a durable deferred, then resumes with the decoded result", () =>
|
|
159
|
+
Effect
|
|
160
|
+
.gen(function*() {
|
|
161
|
+
// The execution suspends on `Trigger`; completing the deferred resumes
|
|
162
|
+
// it (the workflow body replays, the `step` activity is served from its
|
|
163
|
+
// persisted result), and the final value round-trips through the engine.
|
|
164
|
+
const executionId = yield* SuspendWorkflow.execute({ id: "abc" }, { discard: true })
|
|
165
|
+
|
|
166
|
+
const token = yield* DurableDeferred.tokenFromPayload(Trigger, {
|
|
167
|
+
workflow: SuspendWorkflow,
|
|
168
|
+
payload: { id: "abc" }
|
|
169
|
+
})
|
|
170
|
+
yield* DurableDeferred.done(Trigger, { token, exit: Exit.succeed("ok") })
|
|
171
|
+
|
|
172
|
+
const done = yield* waitForComplete(SuspendWorkflow, executionId)
|
|
173
|
+
assert(done && Exit.isSuccess(done.exit))
|
|
174
|
+
assert.strictEqual(done.exit.value, "abc:1:ok")
|
|
175
|
+
})
|
|
176
|
+
.pipe(Effect.provide(TestLayer)))
|
|
177
|
+
|
|
178
|
+
it.live("deferredDone is idempotent (first-writer-wins)", () =>
|
|
179
|
+
Effect
|
|
180
|
+
.gen(function*() {
|
|
181
|
+
const executionId = yield* AwaitOnly.execute({ id: "dup" }, { discard: true })
|
|
182
|
+
const token = yield* DurableDeferred.tokenFromPayload(Trigger, {
|
|
183
|
+
workflow: AwaitOnly,
|
|
184
|
+
payload: { id: "dup" }
|
|
185
|
+
})
|
|
186
|
+
yield* DurableDeferred.done(Trigger, { token, exit: Exit.succeed("first") })
|
|
187
|
+
// Second completion must lose; the workflow body sees "first".
|
|
188
|
+
yield* DurableDeferred.done(Trigger, { token, exit: Exit.succeed("second") })
|
|
189
|
+
|
|
190
|
+
const done = yield* waitForComplete(AwaitOnly, executionId)
|
|
191
|
+
assert(done && Exit.isSuccess(done.exit))
|
|
192
|
+
assert.strictEqual(done.exit.value, "first")
|
|
193
|
+
})
|
|
194
|
+
.pipe(Effect.provide(TestLayer)))
|
|
195
|
+
|
|
196
|
+
it.live("interrupt eventually completes a suspended execution", () =>
|
|
197
|
+
Effect
|
|
198
|
+
.gen(function*() {
|
|
199
|
+
const executionId = yield* AwaitOnly.execute({ id: "int" }, { discard: true })
|
|
200
|
+
// Give the workflow time to suspend on the deferred.
|
|
201
|
+
yield* Effect.sleep(Duration.millis(50))
|
|
202
|
+
yield* AwaitOnly.interrupt(executionId)
|
|
203
|
+
|
|
204
|
+
// The execution should stop reporting as "running" — a subsequent poll
|
|
205
|
+
// returns either Complete (engine collapses the interrupt into a
|
|
206
|
+
// completion) or None (engine surfaces it as not-yet-complete and the
|
|
207
|
+
// wrapper sleep loop eventually ends). Both are acceptable as long as
|
|
208
|
+
// the workflow no longer makes forward progress.
|
|
209
|
+
yield* Effect.sleep(Duration.millis(150))
|
|
210
|
+
const polled = yield* AwaitOnly.poll(executionId)
|
|
211
|
+
if (Option.isSome(polled)) {
|
|
212
|
+
assert.strictEqual(polled.value._tag, "Complete")
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
.pipe(Effect.provide(TestLayer)))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
describe("WorkflowEngine (in-memory)", () => {
|
|
219
|
+
runSuite(WorkflowEngine.layerMemory)
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
// --- Cosmos-emulator-only adapter tests --------------------------------
|
|
223
|
+
//
|
|
224
|
+
// Opt-in. Run with e.g.
|
|
225
|
+
// COSMOS_TEST_URL="https://localhost:8081" COSMOS_TEST_DB="workflow-test" \
|
|
226
|
+
// NODE_TLS_REJECT_UNAUTHORIZED=0 pnpm vitest run workflow-engine-cosmos
|
|
227
|
+
const cosmosUrl = process.env["COSMOS_TEST_URL"]
|
|
228
|
+
const cosmosDb = process.env["COSMOS_TEST_DB"] ?? "workflow-test"
|
|
229
|
+
|
|
230
|
+
describe.skipIf(!cosmosUrl)("WorkflowEngine (Cosmos) — conformance", () => {
|
|
231
|
+
runSuite(
|
|
232
|
+
layerCosmos({
|
|
233
|
+
url: Redacted.make(cosmosUrl ?? ""),
|
|
234
|
+
dbName: cosmosDb,
|
|
235
|
+
prefix: `test-${Date.now()}-`,
|
|
236
|
+
// Tight cadences so the suite doesn't wait on the background pollers.
|
|
237
|
+
recoveryInterval: Duration.seconds(2),
|
|
238
|
+
clockPollInterval: Duration.seconds(1)
|
|
239
|
+
})
|
|
240
|
+
)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
describe.skipIf(!cosmosUrl)("WorkflowEngine (Cosmos) — adapter internals", () => {
|
|
244
|
+
// Each test gets its own container prefix so seeded docs don't leak.
|
|
245
|
+
const prefixFor = (label: string) => `test-${Date.now()}-${label}-`
|
|
246
|
+
|
|
247
|
+
const adapterLayer = (prefix: string) =>
|
|
248
|
+
Layer
|
|
249
|
+
.mergeAll(
|
|
250
|
+
IncrementHandler,
|
|
251
|
+
AwaitOnlyHandler,
|
|
252
|
+
CounterRef.layer,
|
|
253
|
+
CosmosClientLayer(cosmosUrl ?? "", cosmosDb)
|
|
254
|
+
)
|
|
255
|
+
.pipe(
|
|
256
|
+
Layer.provideMerge(
|
|
257
|
+
layerCosmos({
|
|
258
|
+
url: Redacted.make(cosmosUrl ?? ""),
|
|
259
|
+
dbName: cosmosDb,
|
|
260
|
+
prefix,
|
|
261
|
+
recoveryInterval: Duration.millis(500),
|
|
262
|
+
clockPollInterval: Duration.millis(500)
|
|
263
|
+
})
|
|
264
|
+
)
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
it.live("recovery poller drives execs with stale leases", () => {
|
|
268
|
+
const prefix = prefixFor("recovery")
|
|
269
|
+
const containerId = `${prefix}workflow-engine`
|
|
270
|
+
return Effect
|
|
271
|
+
.gen(function*() {
|
|
272
|
+
const { db } = yield* CosmosClient
|
|
273
|
+
const container = db.container(containerId)
|
|
274
|
+
// Pre-seed a running exec whose lease has already expired and whose
|
|
275
|
+
// payload is the schema-encoded form of `{ value: 99 }`.
|
|
276
|
+
yield* Effect.promise(() =>
|
|
277
|
+
container.items.upsert({
|
|
278
|
+
id: "exec",
|
|
279
|
+
_partitionKey: "recover-1",
|
|
280
|
+
type: "exec",
|
|
281
|
+
workflowName: IncrementWorkflow.name,
|
|
282
|
+
payload: JSON.stringify({ value: 99 }),
|
|
283
|
+
status: "running",
|
|
284
|
+
suspended: false,
|
|
285
|
+
interrupted: false,
|
|
286
|
+
worker: "ghost",
|
|
287
|
+
leaseExpiresAt: new Date(Date.now() - 60_000).toISOString(),
|
|
288
|
+
etag: "seed"
|
|
289
|
+
})
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
// Wait for the recovery poller (500ms cadence) to drive it.
|
|
293
|
+
yield* Effect.sleep(Duration.seconds(2))
|
|
294
|
+
|
|
295
|
+
const polled = yield* IncrementWorkflow.poll("recover-1")
|
|
296
|
+
assert(
|
|
297
|
+
Option.isSome(polled)
|
|
298
|
+
&& polled.value._tag === "Complete"
|
|
299
|
+
&& Exit.isSuccess(polled.value.exit)
|
|
300
|
+
)
|
|
301
|
+
assert.strictEqual(polled.value.exit.value, 100)
|
|
302
|
+
})
|
|
303
|
+
.pipe(Effect.provide(adapterLayer(prefix)))
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
it.live("clock poller fires past-due clocks", () => {
|
|
307
|
+
const prefix = prefixFor("clocks")
|
|
308
|
+
const containerId = `${prefix}workflow-engine`
|
|
309
|
+
return Effect
|
|
310
|
+
.gen(function*() {
|
|
311
|
+
const { db } = yield* CosmosClient
|
|
312
|
+
const container = db.container(containerId)
|
|
313
|
+
// Seed an exec + a clock that fired in the past. No in-process timer
|
|
314
|
+
// exists (we never called scheduleClock), so only the poller can
|
|
315
|
+
// resolve the deferred.
|
|
316
|
+
yield* Effect.promise(() =>
|
|
317
|
+
container.items.upsert({
|
|
318
|
+
id: "exec",
|
|
319
|
+
_partitionKey: "exec-clock",
|
|
320
|
+
type: "exec",
|
|
321
|
+
workflowName: AwaitOnly.name,
|
|
322
|
+
payload: JSON.stringify({ id: "wake" }),
|
|
323
|
+
status: "running",
|
|
324
|
+
suspended: false,
|
|
325
|
+
interrupted: false,
|
|
326
|
+
etag: "seed"
|
|
327
|
+
})
|
|
328
|
+
)
|
|
329
|
+
yield* Effect.promise(() =>
|
|
330
|
+
container.items.upsert({
|
|
331
|
+
id: "clock::wake",
|
|
332
|
+
_partitionKey: "exec-clock",
|
|
333
|
+
type: "clock",
|
|
334
|
+
workflowName: AwaitOnly.name,
|
|
335
|
+
deferredName: Trigger.name,
|
|
336
|
+
fireAt: new Date(Date.now() - 60_000).toISOString()
|
|
337
|
+
})
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
yield* Effect.sleep(Duration.seconds(2))
|
|
341
|
+
|
|
342
|
+
// The clock fire is a deferred-complete; assert the deferred row
|
|
343
|
+
// now exists for this execution.
|
|
344
|
+
const deferred = yield* Effect.promise(() =>
|
|
345
|
+
container.item(`deferred::${Trigger.name}`, "exec-clock").read<{ exit: string }>()
|
|
346
|
+
)
|
|
347
|
+
assert(deferred.resource !== undefined)
|
|
348
|
+
// And the clock doc has been deleted.
|
|
349
|
+
const clockGone = yield* Effect.promise(() => container.item("clock::wake", "exec-clock").read())
|
|
350
|
+
assert.strictEqual(clockGone.statusCode, 404)
|
|
351
|
+
})
|
|
352
|
+
.pipe(Effect.provide(adapterLayer(prefix)))
|
|
353
|
+
})
|
|
354
|
+
})
|