Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 WorkflowAgentProgressEntry,
6 WorkflowInspectionError,
7 WorkflowLogProgressEntry,
8 WorkflowPhaseProgressEntry,
9 WorkflowProgressEntry,
10 WorkflowRunHandles,
11} from "./workflow.ts";
12
13const decodeAgent = Schema.decodeUnknownSync(WorkflowAgentProgressEntry);
14const decodePhase = Schema.decodeUnknownSync(WorkflowPhaseProgressEntry);
15const decodeLog = Schema.decodeUnknownSync(WorkflowLogProgressEntry);
16const decodeEntry = Schema.decodeUnknownSync(WorkflowProgressEntry);
17const decodeHandles = Schema.decodeUnknownSync(WorkflowRunHandles);
18
19describe("WorkflowProgressEntry variants", () => {
20 it("decodes a minimal workflow_agent entry and leaves optional fields absent", () => {
21 const agent = decodeAgent({ type: "workflow_agent", index: 0, state: "start" });
22 expect(agent).toEqual({ type: "workflow_agent", index: 0, state: "start" });
23 expect(agent.label).toBeUndefined();
24 expect(agent.phaseIndex).toBeUndefined();
25 expect(agent.startedAt).toBeUndefined();
26 });
27
28 it("decodes a workflow_agent with the full optional surface, including isolation literals", () => {
29 const agent = decodeAgent({
30 type: "workflow_agent",
31 index: 3,
32 state: "done",
33 label: "reviewer",
34 phaseIndex: 1,
35 phaseTitle: "Review",
36 agentId: "agent-3",
37 agentType: "code-reviewer",
38 model: "claude",
39 fallbackModel: "haiku",
40 isolation: "worktree",
41 attempt: 2,
42 queuedAt: 10,
43 startedAt: 20,
44 lastProgressAt: 30,
45 cached: true,
46 remoteSessionId: "remote-1",
47 lastToolName: "Bash",
48 lastToolSummary: "ran tests",
49 promptPreview: "do the thing",
50 resultPreview: "done",
51 error: "none",
52 });
53 expect(agent.isolation).toBe("worktree");
54 expect(agent.phaseIndex).toBe(1);
55 expect(agent.cached).toBe(true);
56 });
57
58 it("rejects an unknown isolation literal", () => {
59 expect(() =>
60 decodeAgent({ type: "workflow_agent", index: 0, state: "start", isolation: "cloud" }),
61 ).toThrow();
62 });
63
64 // Effect's Schema.Struct defaults to onExcessProperty: "ignore", so unknown
65 // extra keys DECODE SUCCESSFULLY and are stripped rather than rejected.
66 it("accepts and strips unknown extra keys on a workflow_agent", () => {
67 const agent = decodeAgent({
68 type: "workflow_agent",
69 index: 0,
70 state: "start",
71 somethingNew: "from a future SDK",
72 });
73 expect(agent).toEqual({ type: "workflow_agent", index: 0, state: "start" });
74 expect(agent).not.toHaveProperty("somethingNew");
75 });
76
77 it("decodes a minimal workflow_phase and keeps optional kind absent when omitted", () => {
78 const phase = decodePhase({ type: "workflow_phase", index: 0, title: "Plan" });
79 expect(phase).toEqual({ type: "workflow_phase", index: 0, title: "Plan" });
80 expect(phase.kind).toBeUndefined();
81 });
82
83 it("decodes a workflow_log entry", () => {
84 const log = decodeLog({ type: "workflow_log", message: "starting up" });
85 expect(log).toEqual({ type: "workflow_log", message: "starting up" });
86 });
87
88 it("decodes each variant through the union by its discriminant", () => {
89 expect(decodeEntry({ type: "workflow_agent", index: 0, state: "start" }).type).toBe(
90 "workflow_agent",
91 );
92 expect(decodeEntry({ type: "workflow_phase", index: 0, title: "Plan" }).type).toBe(
93 "workflow_phase",
94 );
95 expect(decodeEntry({ type: "workflow_log", message: "hi" }).type).toBe("workflow_log");
96 });
97
98 it("rejects an unknown entry type in the union", () => {
99 expect(() => decodeEntry({ type: "workflow_mystery", index: 0 })).toThrow();
100 });
101});
102
103describe("WorkflowRunHandles", () => {
104 it("decodes with only the required taskId", () => {
105 const handles = decodeHandles({ taskId: "task-1" });
106 expect(handles).toEqual({ taskId: "task-1" });
107 expect(handles.runId).toBeUndefined();
108 expect(handles.sessionUrl).toBeUndefined();
109 });
110
111 it("requires taskId", () => {
112 expect(() => decodeHandles({ runId: "wf_abc" })).toThrow();
113 });
114
115 it("rejects a blank (untrimmed-empty) taskId", () => {
116 expect(() => decodeHandles({ taskId: " " })).toThrow();
117 });
118
119 it("decodes the full remote handle surface", () => {
120 const handles = decodeHandles({
121 taskId: "task-1",
122 runId: "wf_abc",
123 workflowName: "spec",
124 taskType: "remote_agent",
125 scriptPath: "/x/s.js",
126 transcriptDir: "/x/t",
127 sessionUrl: "https://example.com/run",
128 warning: "degraded",
129 });
130 expect(handles.sessionUrl).toBe("https://example.com/run");
131 expect(handles.taskType).toBe("remote_agent");
132 });
133});
134
135describe("WorkflowInspectionError", () => {
136 it("derives a stable message from operation and detail", () => {
137 const error = new WorkflowInspectionError({
138 operation: "readScript",
139 reason: "not-found",
140 detail: "no such file",
141 });
142 expect(error.message).toBe("Workflow inspection failed in readScript: no such file");
143 expect(error.reason).toBe("not-found");
144 });
145});