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

wsRpcClient.test.ts

94 lines2.2 KB

1import type {
2 GitStatusLocalResult,
3 GitStatusRemoteResult,
4 GitStatusStreamEvent,
5} from "@t3tools/contracts";
6import { describe, expect, it, vi } from "vitest";
7​
8import { createWsRpcClient } from "./wsRpcClient";
9import { type WsTransport } from "./wsTransport";
10​
11const baseLocalStatus: GitStatusLocalResult = {
12 isRepo: true,
13 hasOriginRemote: true,
14 isDefaultBranch: false,
15 branch: "feature/demo",
16 hasWorkingTreeChanges: false,
17 workingTree: { files: [], insertions: 0, deletions: 0 },
18};
19​
20const baseRemoteStatus: GitStatusRemoteResult = {
21 hasUpstream: true,
22 aheadCount: 0,
23 behindCount: 0,
24 pr: null,
25};
26​
27describe("wsRpcClient", () => {
28 it("reduces git status stream events into flat status snapshots", () => {
29 const subscribe = vi.fn(<TValue>(_connect: unknown, listener: (value: TValue) => void) => {
30 for (const event of [
31 {
32 _tag: "snapshot",
33 local: baseLocalStatus,
34 remote: null,
35 },
36 {
37 _tag: "remoteUpdated",
38 remote: baseRemoteStatus,
39 },
40 {
41 _tag: "localUpdated",
42 local: {
43 ...baseLocalStatus,
44 hasWorkingTreeChanges: true,
45 },
46 },
47 ] satisfies GitStatusStreamEvent[]) {
48 listener(event as TValue);
49 }
50 return () => undefined;
51 });
52​
53 const transport = {
54 dispose: vi.fn(async () => undefined),
55 reconnect: vi.fn(async () => undefined),
56 request: vi.fn(),
57 requestStream: vi.fn(),
58 subscribe,
59 } satisfies Pick<
60 WsTransport,
61 "dispose" | "reconnect" | "request" | "requestStream" | "subscribe"
62 >;
63​
64 const client = createWsRpcClient(transport as unknown as WsTransport);
65 const listener = vi.fn();
66​
67 client.git.onStatus({ cwd: "/repo" }, listener);
68​
69 expect(listener.mock.calls).toEqual([
70 [
71 {
72 ...baseLocalStatus,
73 hasUpstream: false,
74 aheadCount: 0,
75 behindCount: 0,
76 pr: null,
77 },
78 ],
79 [
80 {
81 ...baseLocalStatus,
82 ...baseRemoteStatus,
83 },
84 ],
85 [
86 {
87 ...baseLocalStatus,
88 ...baseRemoteStatus,
89 hasWorkingTreeChanges: true,
90 },
91 ],
92 ]);
93 });
94});