Repository navigation
Expand file tree
/
Copy pathMeshClient.progress.test.ts
More file actions
153 lines (134 loc) · 4.94 KB
/
Copy pathMeshClient.progress.test.ts
File metadata and controls
153 lines (134 loc) · 4.94 KB
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
import { create, fromBinary } from "@bufbuild/protobuf";
import * as Protobuf from "@meshtastic/protobufs";
import { describe, expect, it, vi } from "vitest";
import { createFakeTransport } from "../testing/createFakeTransport.ts";
import { MeshClient } from "./MeshClient.ts";
describe("MeshClient.progress", () => {
const CONFIG_ONLY_NONCE = 69420;
const NODES_ONLY_NONCE = 69421;
it("starts in the idle phase before configure() is called", () => {
const { transport } = createFakeTransport();
const client = new MeshClient({ transport });
expect(client.progress.value.phase).toBe("idle");
});
it("flips to configuring with empty counters when configure() runs", async () => {
const { transport } = createFakeTransport();
const client = new MeshClient({ transport });
void client.configure();
expect(client.progress.value.phase).toBe("configuring");
expect(client.progress.value).toEqual({
phase: "configuring",
received: {
config: 0,
modules: 0,
channels: 0,
nodes: 0,
myInfo: false,
metadata: false,
},
});
});
it("tallies inbound packets onto the configuring counters", () => {
const { transport } = createFakeTransport();
const client = new MeshClient({ transport });
void client.configure();
client.events.onConfigPacket.dispatch(
create(Protobuf.Config.ConfigSchema, {}),
);
client.events.onConfigPacket.dispatch(
create(Protobuf.Config.ConfigSchema, {}),
);
client.events.onChannelPacket.dispatch(
create(Protobuf.Channel.ChannelSchema, {}),
);
client.events.onNodeInfoPacket.dispatch(
create(Protobuf.Mesh.NodeInfoSchema, {}),
);
client.events.onMyNodeInfo.dispatch(
create(Protobuf.Mesh.MyNodeInfoSchema, {}),
);
const cur = client.progress.value;
if (cur.phase !== "configuring")
throw new Error("expected configuring phase");
expect(cur.received.config).toBe(2);
expect(cur.received.channels).toBe(1);
expect(cur.received.nodes).toBe(1);
expect(cur.received.myInfo).toBe(true);
expect(cur.received.modules).toBe(0);
});
it("requests config and nodes in separate firmware-compatible stages", async () => {
const { transport, respond, sent } = createFakeTransport();
const client = new MeshClient({ transport });
await client.configure();
const initialPackets = sent.map(
(packet) =>
fromBinary(Protobuf.Mesh.ToRadioSchema, packet).payloadVariant,
);
expect(initialPackets[0]?.case).toBe("heartbeat");
expect(initialPackets[1]).toEqual({
case: "wantConfigId",
value: CONFIG_ONLY_NONCE,
});
client.events.onConfigPacket.dispatch(
create(Protobuf.Config.ConfigSchema, {}),
);
respond.withConfigCompleteId(CONFIG_ONLY_NONCE);
await vi.waitFor(() => {
expect(sent).toHaveLength(3);
});
expect(
fromBinary(Protobuf.Mesh.ToRadioSchema, sent[2]!).payloadVariant,
).toEqual({ case: "wantConfigId", value: NODES_ONLY_NONCE });
expect(client.progress.value.phase).toBe("configuring");
respond.withConfigCompleteId(NODES_ONLY_NONCE);
await vi.waitFor(() => {
expect(client.progress.value.phase).toBe("configured");
});
const cur = client.progress.value;
expect(cur.phase).toBe("configured");
if (cur.phase !== "configured") throw new Error("unreachable");
expect(cur.received.config).toBe(1);
});
it("ignores stale and out-of-order config completion nonces", async () => {
const { transport, respond, sent } = createFakeTransport();
const client = new MeshClient({ transport });
const completed: number[] = [];
client.events.onConfigComplete.subscribe((id) => completed.push(id));
await client.configure();
respond.withConfigCompleteId(NODES_ONLY_NONCE);
respond.withConfigCompleteId(12345);
await new Promise((resolve) => setTimeout(resolve, 20));
expect(sent).toHaveLength(2);
expect(client.progress.value.phase).toBe("configuring");
expect(completed).toEqual([]);
});
it("ignores packets that arrive while idle (post-completion)", () => {
const { transport } = createFakeTransport();
const client = new MeshClient({ transport });
// not calling configure() — phase is idle
client.events.onConfigPacket.dispatch(
create(Protobuf.Config.ConfigSchema, {}),
);
expect(client.progress.value.phase).toBe("idle");
});
it("resets counters when configure() runs again", async () => {
const { transport } = createFakeTransport();
const client = new MeshClient({ transport });
await client.configure();
client.events.onConfigPacket.dispatch(
create(Protobuf.Config.ConfigSchema, {}),
);
void client.configure();
expect(client.progress.value).toEqual({
phase: "configuring",
received: {
config: 0,
modules: 0,
channels: 0,
nodes: 0,
myInfo: false,
metadata: false,
},
});
});
});