Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

rpc.test.ts

133 lines4.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as Exit from "effect/Exit";
3import * as Schema from "effect/Schema";
4​
5import { EnvironmentAuthorizationError } from "./auth.ts";
6import { ThreadId } from "./baseSchemas.ts";
7import {
8 ORCHESTRATION_THREAD_NOT_FOUND_ERROR_CAPABILITY,
9 OrchestrationGetSnapshotError,
10 OrchestrationThreadNotFoundError,
11 OrchestrationRpcSchemas,
12} from "./orchestration.ts";
13import { WsSubscribeServerConfigRpc } from "./rpc.ts";
14​
15const decodeLegacyServerConfigPayload = Schema.decodeExit(Schema.Struct({}));
16const decodeServerConfigPayload = Schema.decodeSync(WsSubscribeServerConfigRpc.payloadSchema);
17const threadId = ThreadId.make("thread-version-skew");
18const decodeLegacySubscribeThreadPayload = Schema.decodeUnknownSync(
19 Schema.Struct({ threadId: ThreadId }),
20);
21const decodeBooleanEraSubscribeThreadPayload = Schema.decodeUnknownSync(
22 Schema.Struct({
23 threadId: ThreadId,
24 threadNotFoundError: Schema.optionalKey(Schema.Boolean),
25 }),
26);
27const decodeSubscribeThreadPayload = Schema.decodeSync(
28 OrchestrationRpcSchemas.subscribeThread.input,
29);
30const legacySubscribeThreadError = Schema.Union([
31 OrchestrationGetSnapshotError,
32 EnvironmentAuthorizationError,
33]);
34const decodeLegacySubscribeThreadError = Schema.decodeExit(
35 Schema.toCodecJson(legacySubscribeThreadError),
36);
37const encodeSnapshotError = Schema.encodeSync(Schema.toCodecJson(OrchestrationGetSnapshotError));
38const encodeThreadNotFoundError = Schema.encodeSync(
39 Schema.toCodecJson(OrchestrationThreadNotFoundError),
40);
41​
42/**
43 * The client always sends `environmentThemes`, including to servers built
44 * before the field existed, whose payload schema was an empty struct. What
45 * makes that safe is that such a schema accepts the request rather than
46 * rejecting it -- an error here would take down the config subscription.
47 */
48describe("subscribeServerConfig payload compatibility", () => {
49 it("is accepted by a server whose schema predates the field", () => {
50 const decoded = decodeLegacyServerConfigPayload({ environmentThemes: true });
51 expect(Exit.isSuccess(decoded)).toBe(true);
52 });
53​
54 it("is carried by a server that declares it", () => {
55 const decoded = decodeServerConfigPayload({
56 environmentThemes: true,
57 });
58 expect(decoded).toEqual({ environmentThemes: true });
59 });
60​
61 it("stays optional, so a client that never sends it still subscribes", () => {
62 const decoded = decodeServerConfigPayload({});
63 expect(decoded).toEqual({});
64 });
65});
66​
67describe("subscribeThread missing-error compatibility", () => {
68 it("allows the current payload when a server predates both opt-ins", () => {
69 const decoded = decodeLegacySubscribeThreadPayload({
70 threadId,
71 capabilities: [ORCHESTRATION_THREAD_NOT_FOUND_ERROR_CAPABILITY],
72 threadNotFoundError: true,
73 });
74 expect(decoded).toEqual({ threadId });
75 });
76​
77 it("preserves the legacy opt-in when a boolean-era server drops capabilities", () => {
78 const decoded = decodeBooleanEraSubscribeThreadPayload({
79 threadId,
80 capabilities: [ORCHESTRATION_THREAD_NOT_FOUND_ERROR_CAPABILITY],
81 threadNotFoundError: true,
82 });
83 expect(decoded).toEqual({ threadId, threadNotFoundError: true });
84 });
85​
86 it("carries the versioned capability on current servers and keeps it optional", () => {
87 expect(
88 decodeSubscribeThreadPayload({
89 threadId,
90 capabilities: [ORCHESTRATION_THREAD_NOT_FOUND_ERROR_CAPABILITY],
91 }),
92 ).toEqual({
93 threadId,
94 capabilities: [ORCHESTRATION_THREAD_NOT_FOUND_ERROR_CAPABILITY],
95 });
96 expect(decodeSubscribeThreadPayload({ threadId })).toEqual({ threadId });
97 });
98​
99 it("continues to decode the legacy boolean opt-in", () => {
100 expect(decodeSubscribeThreadPayload({ threadId, threadNotFoundError: true })).toEqual({
101 threadId,
102 threadNotFoundError: true,
103 });
104 });
105​
106 it("keeps unknown future capability names decodable", () => {
107 expect(
108 decodeSubscribeThreadPayload({
109 threadId,
110 capabilities: ["orchestration.thread-not-found-error.v2"],
111 }),
112 ).toEqual({
113 threadId,
114 capabilities: ["orchestration.thread-not-found-error.v2"],
115 });
116 });
117​
118 it("keeps the non-opted-in fallback decodable by legacy clients", () => {
119 const fallback = new OrchestrationGetSnapshotError({
120 message: `Thread ${threadId} was not found`,
121 cause: threadId,
122 });
123 const encoded = encodeSnapshotError(fallback);
124 const decoded = decodeLegacySubscribeThreadError(encoded);
125 expect(Exit.isSuccess(decoded)).toBe(true);
126 });
127​
128 it("shows why the dedicated error must be negotiated", () => {
129 const encoded = encodeThreadNotFoundError(new OrchestrationThreadNotFoundError({ threadId }));
130 const decoded = decodeLegacySubscribeThreadError(encoded);
131 expect(Exit.isFailure(decoded)).toBe(true);
132 });
133});