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

server.test.ts

116 lines4.0 KB

1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { ExecutionEnvironmentDescriptor } from "./environment.ts";
5import {
6 OtlpEndpointCheckInput,
7 resolveEnvironmentMachineKind,
8 ServerProvider,
9 ServerProviders,
10} from "./server.ts";
11import { ServerSettings } from "./settings.ts";
12​
13const decodeServerProvider = Schema.decodeUnknownSync(ServerProvider);
14const decodeServerProviders = Schema.decodeUnknownSync(ServerProviders);
15​
16const baseProviderSnapshot = {
17 instanceId: "codex",
18 driver: "codex",
19 enabled: true,
20 installed: true,
21 version: "1.0.0",
22 status: "ready",
23 auth: { status: "authenticated" },
24 checkedAt: "2026-04-10T00:00:00.000Z",
25 models: [],
26};
27​
28describe("ServerProvider", () => {
29 it("preserves installed-version presence through the provider wire codec", () => {
30 const encode = Schema.encodeSync(ServerProvider);
31 for (const installation of [{}, { installedVersion: null }, { installedVersion: "1.2.3" }]) {
32 const decoded = decodeServerProvider({ ...baseProviderSnapshot, ...installation });
33 const wire = JSON.parse(JSON.stringify(encode(decoded)));
34 expect(wire).toMatchObject({ version: "1.0.0", ...installation });
35 expect(Object.hasOwn(wire, "installedVersion")).toBe(
36 Object.hasOwn(installation, "installedVersion"),
37 );
38 expect(decodeServerProvider(wire)).toEqual(decoded);
39 }
40 });
41});
42​
43describe("server config forward compatibility", () => {
44 it("drops providers this build cannot decode instead of failing the whole array", () => {
45 const decodedBase = decodeServerProvider(baseProviderSnapshot);
46​
47 const parsed = decodeServerProviders([
48 baseProviderSnapshot,
49 { ...baseProviderSnapshot, instanceId: "future", status: "some-future-status" },
50 ]);
51​
52 expect(parsed).toEqual([decodedBase]);
53 });
54​
55 it("drops usage windows this build cannot decode instead of failing the provider", () => {
56 const parsed = decodeServerProvider({
57 ...baseProviderSnapshot,
58 usageLimits: {
59 checkedAt: "2026-04-10T00:00:00.000Z",
60 windows: [
61 { id: "primary", kind: "session", label: "Session", usedPercent: 12 },
62 { id: "future", kind: "some-future-kind", label: "Future", usedPercent: 1 },
63 { id: "bad", kind: "weekly", label: "Weekly", usedPercent: 120 },
64 ],
65 },
66 });
67​
68 expect(parsed.usageLimits?.windows).toEqual([
69 { id: "primary", kind: "session", label: "Session", usedPercent: 12 },
70 ]);
71 });
72});
73​
74describe("resolveEnvironmentMachineKind", () => {
75 const decodeDescriptor = Schema.decodeUnknownSync(ExecutionEnvironmentDescriptor);
76 const decodeSettings = Schema.decodeUnknownSync(ServerSettings);
77 const descriptor = (platform: Record<string, unknown>) =>
78 decodeDescriptor({
79 environmentId: "env-1",
80 label: "Box",
81 platform: { os: "linux", arch: "x64", ...platform },
82 serverVersion: "1.0.0",
83 capabilities: {},
84 });
85​
86 it("prefers the user's pick over what the server detected", () => {
87 expect(
88 resolveEnvironmentMachineKind({
89 environment: descriptor({ machine: "mac-mini" }),
90 settings: decodeSettings({ environmentIcon: "laptop" }),
91 }),
92 ).toBe("laptop");
93 });
94​
95 it("drops a machine kind this build does not know instead of failing the descriptor", () => {
96 const parsed = descriptor({ machine: "toaster" });
97​
98 expect(parsed.platform.machine).toBeUndefined();
99 expect(
100 resolveEnvironmentMachineKind({ environment: parsed, settings: decodeSettings({}) }),
101 ).toBe("server");
102 });
103});
104​
105describe("OtlpEndpointCheckInput", () => {
106 const decode = Schema.decodeUnknownSync(OtlpEndpointCheckInput);
107​
108 it("accepts HTTP receivers only", () => {
109 expect(decode({ signal: "traces", url: "https://collector.example.com/v1/traces" }).url).toBe(
110 "https://collector.example.com/v1/traces",
111 );
112 for (const url of ["data:text/plain,ok", "file:///etc/hosts", "collector:4318/v1/traces"]) {
113 expect(() => decode({ signal: "traces", url })).toThrow();
114 }
115 });
116});