|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Predicate } from "effect"; |
| 3 | + |
| 4 | +import { createExecutor, definePlugin } from "@executor-js/sdk"; |
| 5 | +import type { ExecutionEvent, ExecutionObserver } from "@executor-js/sdk"; |
| 6 | +import { makeTestConfig } from "@executor-js/sdk/testing"; |
| 7 | +import type { CodeExecutor, ExecuteResult } from "@executor-js/codemode-core"; |
| 8 | + |
| 9 | +import { createExecutionEngine } from "./engine"; |
| 10 | + |
| 11 | +const emptyPlugin = definePlugin(() => ({ |
| 12 | + id: "observer-test" as const, |
| 13 | + storage: () => ({}), |
| 14 | + staticSources: () => [], |
| 15 | +})); |
| 16 | + |
| 17 | +const makeExecutor = () => createExecutor(makeTestConfig({ plugins: [emptyPlugin()] as const })); |
| 18 | + |
| 19 | +// A code executor that issues one builtin tool call (tools.search) and then |
| 20 | +// completes, enough to exercise the full event sequence. |
| 21 | +const toolCallingExecutor: CodeExecutor = { |
| 22 | + execute: (_code, invoker) => |
| 23 | + invoker |
| 24 | + .invoke({ path: "search", args: { query: "anything" } }) |
| 25 | + .pipe(Effect.as({ result: "ok", logs: [] } satisfies ExecuteResult), Effect.orDie), |
| 26 | +}; |
| 27 | + |
| 28 | +const collectingObserver = () => { |
| 29 | + const events: ExecutionEvent[] = []; |
| 30 | + const observer: ExecutionObserver = { |
| 31 | + handle: (event) => Effect.sync(() => void events.push(event)), |
| 32 | + }; |
| 33 | + return { events, observer }; |
| 34 | +}; |
| 35 | + |
| 36 | +describe("execution engine observer emission", () => { |
| 37 | + it.effect("emits the full lifecycle for a completed run with a tool call", () => |
| 38 | + Effect.gen(function* () { |
| 39 | + const executor = yield* makeExecutor(); |
| 40 | + const { events, observer } = collectingObserver(); |
| 41 | + const engine = createExecutionEngine({ |
| 42 | + executor, |
| 43 | + codeExecutor: toolCallingExecutor, |
| 44 | + observer, |
| 45 | + }); |
| 46 | + |
| 47 | + const result = yield* engine.executeWithPause("noop", { trigger: { kind: "test" } }); |
| 48 | + expect(result.status).toBe("completed"); |
| 49 | + |
| 50 | + // First event opens the run, last closes it; tool calls land in between. |
| 51 | + // `.find` with isTagged narrows each result, so the assertions read the |
| 52 | + // typed fields directly via optional chaining (no conditional blocks). |
| 53 | + const started = events.find((e) => Predicate.isTagged(e, "ExecutionStarted")); |
| 54 | + const finished = events.find((e) => Predicate.isTagged(e, "ExecutionFinished")); |
| 55 | + const toolStarted = events.find((e) => Predicate.isTagged(e, "ToolCallStarted")); |
| 56 | + const toolFinished = events.find((e) => Predicate.isTagged(e, "ToolCallFinished")); |
| 57 | + |
| 58 | + expect(Predicate.isTagged(events[0], "ExecutionStarted")).toBe(true); |
| 59 | + expect(Predicate.isTagged(events[events.length - 1], "ExecutionFinished")).toBe(true); |
| 60 | + |
| 61 | + expect(started?.trigger?.kind).toBe("test"); |
| 62 | + expect(started?.owner.tenant).toBeDefined(); |
| 63 | + expect(toolStarted).toBeDefined(); |
| 64 | + expect(finished?.status).toBe("completed"); |
| 65 | + |
| 66 | + // Tool-call events share the run's executionId and carry the path. |
| 67 | + expect(toolFinished?.path).toBe("search"); |
| 68 | + expect(toolFinished?.status).toBe("completed"); |
| 69 | + expect(toolFinished?.executionId).toBe(started?.executionId); |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + it.effect("does nothing observable when no observer is configured", () => |
| 74 | + Effect.gen(function* () { |
| 75 | + const executor = yield* makeExecutor(); |
| 76 | + const engine = createExecutionEngine({ executor, codeExecutor: toolCallingExecutor }); |
| 77 | + const result = yield* engine.executeWithPause("noop"); |
| 78 | + expect(result.status).toBe("completed"); |
| 79 | + }), |
| 80 | + ); |
| 81 | +}); |
0 commit comments