Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Effect, Schema } from "effect";
3
4import { ORCHESTRATION_WS_CHANNELS, ORCHESTRATION_WS_METHODS } from "./orchestration";
5import { WebSocketRequest, WsResponse, WS_CHANNELS, WS_METHODS } from "./ws";
6
7const decodeWebSocketRequest = Schema.decodeUnknownEffect(WebSocketRequest);
8const decodeWsResponse = Schema.decodeUnknownEffect(WsResponse);
9
10it.effect("accepts getTurnDiff requests when fromTurnCount <= toTurnCount", () =>
11 Effect.gen(function* () {
12 const parsed = yield* decodeWebSocketRequest({
13 id: "req-1",
14 body: {
15 _tag: ORCHESTRATION_WS_METHODS.getTurnDiff,
16 threadId: "thread-1",
17 fromTurnCount: 1,
18 toTurnCount: 2,
19 },
20 });
21 assert.strictEqual(parsed.body._tag, ORCHESTRATION_WS_METHODS.getTurnDiff);
22 }),
23);
24
25it.effect("rejects getTurnDiff requests when fromTurnCount > toTurnCount", () =>
26 Effect.gen(function* () {
27 const result = yield* Effect.exit(
28 decodeWebSocketRequest({
29 id: "req-1",
30 body: {
31 _tag: ORCHESTRATION_WS_METHODS.getTurnDiff,
32 threadId: "thread-1",
33 fromTurnCount: 3,
34 toTurnCount: 2,
35 },
36 }),
37 );
38 assert.strictEqual(result._tag, "Failure");
39 }),
40);
41
42it.effect("trims websocket request id and nested orchestration ids", () =>
43 Effect.gen(function* () {
44 const parsed = yield* decodeWebSocketRequest({
45 id: " req-1 ",
46 body: {
47 _tag: ORCHESTRATION_WS_METHODS.getTurnDiff,
48 threadId: " thread-1 ",
49 fromTurnCount: 0,
50 toTurnCount: 0,
51 },
52 });
53 assert.strictEqual(parsed.id, "req-1");
54 assert.strictEqual(parsed.body._tag, ORCHESTRATION_WS_METHODS.getTurnDiff);
55 if (parsed.body._tag === ORCHESTRATION_WS_METHODS.getTurnDiff) {
56 assert.strictEqual(parsed.body.threadId, "thread-1");
57 }
58 }),
59);
60
61it.effect("accepts git.preparePullRequestThread requests", () =>
62 Effect.gen(function* () {
63 const parsed = yield* decodeWebSocketRequest({
64 id: "req-pr-1",
65 body: {
66 _tag: WS_METHODS.gitPreparePullRequestThread,
67 cwd: "/repo",
68 reference: "#42",
69 mode: "worktree",
70 },
71 });
72 assert.strictEqual(parsed.body._tag, WS_METHODS.gitPreparePullRequestThread);
73 }),
74);
75
76it.effect("accepts typed websocket push envelopes with sequence", () =>
77 Effect.gen(function* () {
78 const parsed = yield* decodeWsResponse({
79 type: "push",
80 sequence: 1,
81 channel: WS_CHANNELS.serverWelcome,
82 data: {
83 cwd: "/tmp/workspace",
84 projectName: "workspace",
85 },
86 });
87
88 if (!("type" in parsed) || parsed.type !== "push") {
89 assert.fail("expected websocket response to decode as a push envelope");
90 }
91
92 assert.strictEqual(parsed.type, "push");
93 assert.strictEqual(parsed.sequence, 1);
94 assert.strictEqual(parsed.channel, WS_CHANNELS.serverWelcome);
95 }),
96);
97
98it.effect("accepts git.actionProgress push envelopes", () =>
99 Effect.gen(function* () {
100 const parsed = yield* decodeWsResponse({
101 type: "push",
102 sequence: 3,
103 channel: WS_CHANNELS.gitActionProgress,
104 data: {
105 actionId: "action-1",
106 cwd: "/repo",
107 action: "commit",
108 kind: "phase_started",
109 phase: "commit",
110 label: "Committing...",
111 },
112 });
113
114 if (!("type" in parsed) || parsed.type !== "push") {
115 assert.fail("expected websocket response to decode as a push envelope");
116 }
117
118 assert.strictEqual(parsed.channel, WS_CHANNELS.gitActionProgress);
119 }),
120);
121
122it.effect("accepts server.providersUpdated push envelopes", () =>
123 Effect.gen(function* () {
124 const parsed = yield* decodeWsResponse({
125 type: "push",
126 sequence: 4,
127 channel: WS_CHANNELS.serverProvidersUpdated,
128 data: {
129 providers: [],
130 },
131 });
132
133 if (!("type" in parsed) || parsed.type !== "push") {
134 assert.fail("expected websocket response to decode as a push envelope");
135 }
136
137 assert.strictEqual(parsed.channel, WS_CHANNELS.serverProvidersUpdated);
138 }),
139);
140
141it.effect("rejects push envelopes when channel payload does not match the channel schema", () =>
142 Effect.gen(function* () {
143 const result = yield* Effect.exit(
144 decodeWsResponse({
145 type: "push",
146 sequence: 2,
147 channel: ORCHESTRATION_WS_CHANNELS.domainEvent,
148 data: {
149 cwd: "/tmp/workspace",
150 projectName: "workspace",
151 },
152 }),
153 );
154
155 assert.strictEqual(result._tag, "Failure");
156 }),
157);