summaryrefslogtreecommitdiffhomepage
path: root/packages/transport-contract/src/contract.types.test.ts
blob: 34ff5441d5c5a89ee63bf14893ccabf170e8fb73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * Compile-time assertions for transport-contract types.
 *
 * These are never executed — they exist solely to prove the exported types
 * compile with conforming literals. If a shape changes, this file fails to
 * typecheck.
 */

import { describe, expect, it } from "vitest";
import type {
  ChatRequest,
  Computer,
  ComputerEntry,
  ComputerListResponse,
  ComputerResponse,
  ComputerStatusResponse,
  ConversationComputerResponse,
  CwdResponse,
  LspServerInfo,
  LspServerState,
  LspStatusResponse,
  McpStatusResponse,
  ModelsResponse,
  SetConversationComputerRequest,
  SetCwdRequest,
  SetWorkspaceDefaultComputerRequest,
  TestComputerResponse,
} from "./index.js";

// ─── CwdResponse ─────────────────────────────────────────────────────────────

const _cwdNull: CwdResponse = {
  conversationId: "conv-1",
  cwd: null,
};

const _cwdSet: CwdResponse = {
  conversationId: "conv-2",
  cwd: "/home/user/project",
};

// ─── SetCwdRequest ───────────────────────────────────────────────────────────

const _setCwd: SetCwdRequest = {
  cwd: "/tmp/workspace",
};

// ─── ChatRequest.computerId (additive optional) ──────────────────────────────

const _chatWithComputer: ChatRequest = {
  message: "run the test suite",
  computerId: "prod-box",
};

const _chatWithoutComputer: ChatRequest = {
  message: "hello",
};

// ─── ChatRequest.images (additive optional) ──────────────────────────────────

const _chatWithImages: ChatRequest = {
  message: "What's in this screenshot?",
  images: [{ url: "data:image/png;base64,iVBORw0KGgo=", mimeType: "image/png" }],
};

const _chatWithHttpImage: ChatRequest = {
  message: "analyze this",
  images: [{ url: "https://example.com/diagram.png" }],
};

// ─── Computer list / single response ─────────────────────────────────────────

const _computer: Computer = {
  alias: "prod-box",
  hostName: "10.0.0.5",
  port: 22,
  user: "deploy",
  identityFile: "/home/user/.ssh/id_ed25519",
  knownHost: true,
};

const _computerEntry: ComputerEntry = {
  ..._computer,
  usageCount: 3,
};

const _computerList: ComputerListResponse = {
  computers: [_computerEntry],
};

const _computerResponse: ComputerResponse = _computer;

// ─── Computer status / test probe ────────────────────────────────────────────

const _statusConnected: ComputerStatusResponse = {
  alias: "prod-box",
  state: "connected",
  knownHost: true,
};

const _statusError: ComputerStatusResponse = {
  alias: "prod-box",
  state: "error",
  error: "connection refused",
  knownHost: false,
};

const _testOk: TestComputerResponse = {
  alias: "prod-box",
  ok: true,
};

const _testFail: TestComputerResponse = {
  alias: "prod-box",
  ok: false,
  error: "auth failed",
};

// ─── Per-conversation + workspace computer ───────────────────────────────────

const _setConvComputer: SetConversationComputerRequest = {
  computerId: "prod-box",
};

const _clearConvComputer: SetConversationComputerRequest = {
  computerId: null,
};

const _convComputer: ConversationComputerResponse = {
  conversationId: "conv-1",
  computerId: "prod-box",
};

const _convComputerNull: ConversationComputerResponse = {
  conversationId: "conv-2",
  computerId: null,
};

const _setDefaultComputer: SetWorkspaceDefaultComputerRequest = {
  computerId: null,
};

// ─── LspServerState ──────────────────────────────────────────────────────────

const _stateConnected: LspServerState = "connected";
const _stateStarting: LspServerState = "starting";
const _stateError: LspServerState = "error";
const _stateNotStarted: LspServerState = "not-started";

// ─── LspServerInfo ───────────────────────────────────────────────────────────

const _serverOk: LspServerInfo = {
  id: "typescript",
  name: "TypeScript Language Server",
  root: "/home/user/project",
  extensions: [".ts", ".tsx"],
  state: "connected",
};

const _serverErr: LspServerInfo = {
  id: "luau-lsp",
  name: "Luau LSP",
  root: "/home/user/game",
  extensions: [".luau"],
  state: "error",
  error: "Failed to start: binary not found",
};

const _serverWithSource: LspServerInfo = {
  id: "ruby-lsp",
  name: "Ruby LSP",
  root: "/home/user/raylib",
  extensions: [".rb"],
  state: "connected",
  configSource: ".dispatch/lsp.json",
};

// ─── LspStatusResponse ───────────────────────────────────────────────────────

const _lspNoCwd: LspStatusResponse = {
  conversationId: "conv-3",
  cwd: null,
  servers: [],
};

const _lspWithServers: LspStatusResponse = {
  conversationId: "conv-4",
  cwd: "/home/user/project",
  servers: [_serverOk, _serverErr],
};

// ─── Runtime smoke (vitest needs a suite) ────────────────────────────────────

describe("transport-contract types compile and are exported", () => {
  it("CwdResponse: null cwd round-trips", () => {
    expect(_cwdNull).toEqual({ conversationId: "conv-1", cwd: null });
  });

  it("CwdResponse: set cwd round-trips", () => {
    expect(_cwdSet.cwd).toBe("/home/user/project");
  });

  it("SetCwdRequest: carries cwd", () => {
    expect(_setCwd.cwd).toBe("/tmp/workspace");
  });

  it("LspServerState: all four variants are valid", () => {
    const states: LspServerState[] = [
      _stateConnected,
      _stateStarting,
      _stateError,
      _stateNotStarted,
    ];
    expect(states).toHaveLength(4);
  });

  it("LspServerInfo: ok server has no error field", () => {
    expect(_serverOk.state).toBe("connected");
    expect(_serverOk.error).toBeUndefined();
  });

  it("LspServerInfo: error server carries error message", () => {
    expect(_serverErr.state).toBe("error");
    expect(_serverErr.error).toBe("Failed to start: binary not found");
  });

  it("LspServerInfo: carries optional configSource", () => {
    expect(_serverWithSource.configSource).toBe(".dispatch/lsp.json");
    expect(_serverOk.configSource).toBeUndefined();
  });

  it("LspStatusResponse: empty servers when cwd is null", () => {
    expect(_lspNoCwd.servers).toEqual([]);
  });

  it("LspStatusResponse: populated servers when cwd is set", () => {
    expect(_lspWithServers.servers).toHaveLength(2);
  });

  // ─── MCP status ─────────────────────────────────────────────────────────────

  it("McpStatusResponse: empty servers when cwd is null", () => {
    const _noCwd: McpStatusResponse = { conversationId: "c1", cwd: null, servers: [] };
    expect(_noCwd.servers).toEqual([]);
  });

  it("McpStatusResponse: populated servers when cwd is set", () => {
    const _withServers: McpStatusResponse = {
      conversationId: "c2",
      cwd: "/home/user/project",
      servers: [
        { id: "freecad", state: "connected", toolCount: 12, configSource: ".dispatch/mcp.json" },
        { id: "chrome", state: "error", error: "spawn failed", toolCount: 0 },
      ],
    };
    expect(_withServers.servers).toHaveLength(2);
    expect(_withServers.servers[0]?.toolCount).toBe(12);
    expect(_withServers.servers[1]?.error).toBe("spawn failed");
  });

  // ─── ChatRequest.computerId ──────────────────────────────────────────────

  it("ChatRequest: computerId is additive optional (omittable)", () => {
    expect(_chatWithoutComputer.computerId).toBeUndefined();
  });

  it("ChatRequest: carries computerId when set", () => {
    expect(_chatWithComputer.computerId).toBe("prod-box");
  });

  // ─── ChatRequest.images (additive optional) ──────────────────────────────

  it("ChatRequest: images is additive optional (omittable)", () => {
    expect(_chatWithoutComputer.images).toBeUndefined();
  });

  it("ChatRequest: carries images (data URL) when set", () => {
    expect(_chatWithImages.images).toHaveLength(1);
    expect(_chatWithImages.images?.[0]?.url).toContain("base64");
    expect(_chatWithImages.images?.[0]?.mimeType).toBe("image/png");
  });

  it("ChatRequest: carries images (http URL, mimeType optional)", () => {
    expect(_chatWithHttpImage.images?.[0]?.url).toBe("https://example.com/diagram.png");
    expect(_chatWithHttpImage.images?.[0]?.mimeType).toBeUndefined();
  });

  it("ModelsResponse: ModelMetadata carries optional vision flag", () => {
    const resp: ModelsResponse = {
      models: ["umans/kimi-k2.7", "umans/glm-5.2"],
      modelInfo: {
        "umans/kimi-k2.7": { contextWindow: 200000, vision: true },
        "umans/glm-5.2": { contextWindow: 128000 },
      },
    };
    expect(resp.modelInfo?.["umans/kimi-k2.7"]?.vision).toBe(true);
    expect(resp.modelInfo?.["umans/glm-5.2"]?.vision).toBeUndefined();
  });

  // ─── Computers ───────────────────────────────────────────────────────────

  it("ComputerListResponse: carries entries with usage counts", () => {
    expect(_computerList.computers).toHaveLength(1);
    expect(_computerList.computers[0]?.usageCount).toBe(3);
    expect(_computerList.computers[0]?.alias).toBe("prod-box");
  });

  it("ComputerResponse: is a single Computer", () => {
    expect(_computerResponse.alias).toBe("prod-box");
    expect(_computerResponse.port).toBe(22);
  });

  it("ComputerStatusResponse: all four states are valid", () => {
    const states: ComputerStatusResponse["state"][] = [
      "disconnected",
      "connecting",
      "connected",
      "error",
    ];
    expect(states).toHaveLength(4);
  });

  it("ComputerStatusResponse: connected has no error field", () => {
    expect(_statusConnected.state).toBe("connected");
    expect(_statusConnected.error).toBeUndefined();
  });

  it("ComputerStatusResponse: error carries message", () => {
    expect(_statusError.state).toBe("error");
    expect(_statusError.error).toBe("connection refused");
  });

  it("TestComputerResponse: ok has no error field", () => {
    expect(_testOk.ok).toBe(true);
    expect(_testOk.error).toBeUndefined();
  });

  it("TestComputerResponse: failure carries error", () => {
    expect(_testFail.ok).toBe(false);
    expect(_testFail.error).toBe("auth failed");
  });

  it("SetConversationComputerRequest: null clears to inherit/local", () => {
    expect(_setConvComputer.computerId).toBe("prod-box");
    expect(_clearConvComputer.computerId).toBeNull();
  });

  it("ConversationComputerResponse: null computerId round-trips", () => {
    expect(_convComputer.computerId).toBe("prod-box");
    expect(_convComputerNull.computerId).toBeNull();
  });

  it("SetWorkspaceDefaultComputerRequest: null clears to local", () => {
    expect(_setDefaultComputer.computerId).toBeNull();
  });
});