serverless-ircd 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/CHANGELOG.md +175 -0
  2. package/README.md +88 -19
  3. package/apps/aws-stack/README.md +4 -3
  4. package/apps/aws-stack/package.json +1 -1
  5. package/apps/aws-stack/src/aws-stack.ts +32 -4
  6. package/apps/aws-stack/tests/stack.test.ts +47 -1
  7. package/apps/cf-tcp-container/package.json +1 -1
  8. package/apps/cf-worker/package.json +1 -1
  9. package/apps/local-cli/package.json +1 -1
  10. package/apps/local-cli/src/server.ts +129 -21
  11. package/apps/local-cli/tests/e2e.test.ts +1 -1
  12. package/apps/local-cli/tests/ws-subprotocol.test.ts +257 -0
  13. package/package.json +2 -2
  14. package/packages/aws-adapter/package.json +1 -1
  15. package/packages/aws-adapter/src/aws-runtime.ts +69 -0
  16. package/packages/aws-adapter/src/handlers/connect.ts +36 -5
  17. package/packages/aws-adapter/src/handlers/default.ts +63 -5
  18. package/packages/aws-adapter/src/handlers/index.ts +41 -2
  19. package/packages/aws-adapter/src/handlers/nlb-stream.ts +9 -0
  20. package/packages/aws-adapter/src/index.ts +2 -0
  21. package/packages/aws-adapter/src/serialize.ts +11 -1
  22. package/packages/aws-adapter/src/stats.ts +80 -0
  23. package/packages/aws-adapter/tests/aws-integration.test.ts +1 -1
  24. package/packages/aws-adapter/tests/aws-runtime.test.ts +61 -0
  25. package/packages/aws-adapter/tests/connect.test.ts +97 -1
  26. package/packages/aws-adapter/tests/handlers.test.ts +148 -0
  27. package/packages/aws-adapter/tests/nlb-stream.test.ts +2 -0
  28. package/packages/aws-adapter/tests/stats.test.ts +317 -0
  29. package/packages/cf-adapter/package.json +5 -1
  30. package/packages/cf-adapter/src/cf-runtime.ts +66 -1
  31. package/packages/cf-adapter/src/channel-do.ts +2 -2
  32. package/packages/cf-adapter/src/connection-do.ts +182 -54
  33. package/packages/cf-adapter/src/env.ts +25 -6
  34. package/packages/cf-adapter/src/index.ts +2 -0
  35. package/packages/cf-adapter/src/registry-do.ts +22 -3
  36. package/packages/cf-adapter/src/sharding.ts +1 -2
  37. package/packages/cf-adapter/src/stats.ts +65 -0
  38. package/packages/cf-adapter/tests/cf-harness.ts +1 -1
  39. package/packages/cf-adapter/tests/cf-integration.test.ts +4 -4
  40. package/packages/cf-adapter/tests/cf-runtime.test.ts +38 -2
  41. package/packages/cf-adapter/tests/channel-do.test.ts +2 -2
  42. package/packages/cf-adapter/tests/connection-do-channel-registration.test.ts +2 -2
  43. package/packages/cf-adapter/tests/connection-do-no-batching-reservation.test.ts +2 -2
  44. package/packages/cf-adapter/tests/connection-do-ws-spec-contract.test.ts +289 -0
  45. package/packages/cf-adapter/tests/connection-do-ws-subprotocol.test.ts +184 -0
  46. package/packages/cf-adapter/tests/connection-do.test.ts +27 -2
  47. package/packages/cf-adapter/tests/registry-do.test.ts +4 -4
  48. package/packages/cf-adapter/tests/sharding.test.ts +1 -1
  49. package/packages/cf-adapter/tests/stats.test.ts +120 -0
  50. package/packages/cf-adapter/tests/worker/main.ts +7 -7
  51. package/packages/cf-adapter/tests/worker/stubs/channel-stub.ts +2 -2
  52. package/packages/cf-adapter/tests/worker/stubs/registry-stub.ts +8 -2
  53. package/packages/cf-adapter/wrangler.test.toml +7 -0
  54. package/packages/in-memory-runtime/package.json +1 -1
  55. package/packages/in-memory-runtime/src/in-memory-runtime.ts +39 -0
  56. package/packages/in-memory-runtime/tests/in-memory-runtime.test.ts +259 -0
  57. package/packages/irc-core/package.json +1 -1
  58. package/packages/irc-core/src/admission.ts +16 -15
  59. package/packages/irc-core/src/caps/capabilities.ts +1 -1
  60. package/packages/irc-core/src/commands/index.ts +8 -0
  61. package/packages/irc-core/src/commands/invite.ts +2 -4
  62. package/packages/irc-core/src/commands/isupport.ts +6 -2
  63. package/packages/irc-core/src/commands/kick.ts +2 -4
  64. package/packages/irc-core/src/commands/kill.ts +127 -0
  65. package/packages/irc-core/src/commands/list.ts +1 -1
  66. package/packages/irc-core/src/commands/lusers.ts +204 -0
  67. package/packages/irc-core/src/commands/mode.ts +4 -8
  68. package/packages/irc-core/src/commands/names.ts +3 -5
  69. package/packages/irc-core/src/commands/part.ts +2 -4
  70. package/packages/irc-core/src/commands/rehash.ts +119 -0
  71. package/packages/irc-core/src/commands/setname.ts +109 -0
  72. package/packages/irc-core/src/commands/stats.ts +152 -0
  73. package/packages/irc-core/src/commands/topic.ts +2 -4
  74. package/packages/irc-core/src/commands/trace.ts +137 -0
  75. package/packages/irc-core/src/commands/wallops.ts +118 -0
  76. package/packages/irc-core/src/config.ts +7 -0
  77. package/packages/irc-core/src/effects.ts +27 -1
  78. package/packages/irc-core/src/index.ts +2 -0
  79. package/packages/irc-core/src/ports.ts +179 -0
  80. package/packages/irc-core/src/protocol/numerics.ts +42 -11
  81. package/packages/irc-core/src/protocol/outbound.ts +20 -3
  82. package/packages/irc-core/src/types.ts +8 -1
  83. package/packages/irc-core/src/ws-framing.ts +132 -0
  84. package/packages/irc-core/src/ws-subprotocol.ts +66 -0
  85. package/packages/irc-core/tests/admission.test.ts +18 -0
  86. package/packages/irc-core/tests/commands/kill.test.ts +243 -0
  87. package/packages/irc-core/tests/commands/lusers.test.ts +368 -0
  88. package/packages/irc-core/tests/commands/mode.test.ts +57 -0
  89. package/packages/irc-core/tests/commands/rehash.test.ts +171 -0
  90. package/packages/irc-core/tests/commands/setname.test.ts +225 -0
  91. package/packages/irc-core/tests/commands/stats.test.ts +294 -0
  92. package/packages/irc-core/tests/commands/trace.test.ts +282 -0
  93. package/packages/irc-core/tests/commands/wallops.test.ts +231 -0
  94. package/packages/irc-core/tests/dropped-s2s-and-obsolete-verbs.test.ts +90 -0
  95. package/packages/irc-core/tests/effects.test.ts +14 -0
  96. package/packages/irc-core/tests/numerics.test.ts +90 -0
  97. package/packages/irc-core/tests/outbound.test.ts +51 -0
  98. package/packages/irc-core/tests/ports.test.ts +22 -0
  99. package/packages/irc-core/tests/raw-modules.d.ts +11 -0
  100. package/packages/irc-core/tests/stats-store.test.ts +222 -0
  101. package/packages/irc-core/tests/ws-framing.test.ts +213 -0
  102. package/packages/irc-core/tests/ws-subprotocol.test.ts +111 -0
  103. package/packages/irc-server/package.json +1 -1
  104. package/packages/irc-server/src/actor.ts +249 -16
  105. package/packages/irc-server/src/dispatch.ts +1 -0
  106. package/packages/irc-server/src/routing.ts +3 -0
  107. package/packages/irc-server/src/runtime.ts +31 -0
  108. package/packages/irc-server/src/transport.ts +10 -7
  109. package/packages/irc-server/tests/actor.test.ts +1089 -1
  110. package/packages/irc-server/tests/dispatch.test.ts +37 -0
  111. package/packages/irc-server/tests/raw-modules.d.ts +11 -0
  112. package/packages/irc-server/tests/routing.test.ts +1 -0
  113. package/packages/irc-server/tests/runtime.test.ts +7 -0
  114. package/packages/irc-test-support/package.json +1 -1
  115. package/packages/irc-test-support/src/scenarios.ts +9 -1
  116. package/packages/irc-test-support/tests/in-memory-scenarios.test.ts +1 -1
  117. package/pnpm-workspace.yaml +1 -0
  118. package/tools/ci-hardening/package.json +1 -1
  119. package/tools/package.json +6 -1
  120. package/tools/seed-cf-accounts.ts +4 -1
  121. package/tools/tcp-ws-forwarder/package.json +1 -1
  122. package/tools/tcp-ws-forwarder/src/forwarder.ts +57 -9
  123. package/tools/tcp-ws-forwarder/tests/forwarder.test.ts +34 -1
  124. package/tools/tcp-ws-forwarder/tests/framing.test.ts +65 -1
@@ -25,6 +25,8 @@ import {
25
25
  type SaslResult,
26
26
  SequentialIdFactory,
27
27
  type ServerConfig,
28
+ type ServerStats,
29
+ type ServerStatsSnapshot,
28
30
  type StoredMessage,
29
31
  applyChannelDelta as applyDelta,
30
32
  createChannel,
@@ -35,7 +37,9 @@ import {
35
37
  toSnapshot,
36
38
  } from '@serverless-ircd/irc-core';
37
39
  import { describe, expect, it } from 'vitest';
38
- import { ConnectionActor } from '../src/actor';
40
+ import { ConnectionActor, channelTargetsForMessage } from '../src/actor';
41
+ import type { ActorChannelAccess } from '../src/actor';
42
+ import actorSource from '../src/actor.ts?raw';
39
43
  import type { IrcRuntime } from '../src/runtime';
40
44
  import { TcpByteStreamTransport, WsTextFrameTransport } from '../src/transport';
41
45
  import type { Transport } from '../src/transport';
@@ -54,6 +58,14 @@ class FakeRuntime implements IrcRuntime {
54
58
  private readonly channels = new Map<string, ChannelState>();
55
59
  private readonly testConnections = new Map<ConnId, ConnectionState>();
56
60
  private readonly nicks = new Map<string, ConnId>();
61
+ /**
62
+ * Configurable `reloadConfig` result. When set, the next `reloadConfig()`
63
+ * call returns this value; when `reloadConfigError` is set instead it
64
+ * throws. Leaving both unset throws so a test that drives `REHASH` without
65
+ * configuring the fake fails loudly.
66
+ */
67
+ reloadConfigResult: ServerConfig | undefined;
68
+ reloadConfigError: unknown;
57
69
 
58
70
  constructor(private readonly clock: { now(): number }) {}
59
71
 
@@ -64,6 +76,16 @@ class FakeRuntime implements IrcRuntime {
64
76
  }
65
77
  }
66
78
 
79
+ /**
80
+ * Drops a connection from the live-connection store while leaving its nick
81
+ * registered. Simulates the race a reducer sees when `lookupNick` resolves
82
+ * a nick but `getConnection` returns null (the connection tore down between
83
+ * the two awaits).
84
+ */
85
+ dropConnection(id: ConnId): void {
86
+ this.testConnections.delete(id);
87
+ }
88
+
67
89
  // Channel-state access (used by the actor's `channels` dep).
68
90
  getOrCreateChannel(name: ChanName): ChannelState {
69
91
  const key = name.toLowerCase();
@@ -84,6 +106,9 @@ class FakeRuntime implements IrcRuntime {
84
106
  async broadcast(chan: ChanName, lines: RawLine[], except?: ConnId): Promise<void> {
85
107
  this.calls.push({ method: 'broadcast', args: [chan, lines, except] });
86
108
  }
109
+ async broadcastWallops(lines: RawLine[], except?: ConnId): Promise<void> {
110
+ this.calls.push({ method: 'broadcastWallops', args: [lines, except] });
111
+ }
87
112
  async disconnect(conn: ConnId, reason?: string): Promise<void> {
88
113
  this.calls.push({ method: 'disconnect', args: [conn, reason] });
89
114
  }
@@ -156,6 +181,15 @@ class FakeRuntime implements IrcRuntime {
156
181
  }
157
182
  return out;
158
183
  }
184
+
185
+ async reloadConfig(): Promise<ServerConfig> {
186
+ this.calls.push({ method: 'reloadConfig', args: [] });
187
+ if (this.reloadConfigError !== undefined) throw this.reloadConfigError;
188
+ if (this.reloadConfigResult === undefined) {
189
+ throw new Error('FakeRuntime.reloadConfigResult not configured');
190
+ }
191
+ return this.reloadConfigResult;
192
+ }
159
193
  }
160
194
 
161
195
  function makeActor(opts: {
@@ -168,8 +202,10 @@ function makeActor(opts: {
168
202
  accounts?: AccountStore;
169
203
  mtlsIdentity?: MtlsIdentityProvider;
170
204
  history?: NickHistoryStore;
205
+ stats?: ServerStats;
171
206
  operCreds?: ReadonlyArray<{ user: string; password: string }>;
172
207
  transport?: Transport;
208
+ configSource?: string;
173
209
  }): { actor: ConnectionActor; runtime: FakeRuntime; conn: ConnectionState } {
174
210
  const clock = opts.clock ?? new FakeClock(1_000);
175
211
  const conn = createConnection({ id: 'c1', connectedSince: 0 });
@@ -229,9 +265,15 @@ function makeActor(opts: {
229
265
  if (opts.history !== undefined) {
230
266
  finalOpts.history = opts.history;
231
267
  }
268
+ if (opts.stats !== undefined) {
269
+ finalOpts.stats = opts.stats;
270
+ }
232
271
  if (opts.transport !== undefined) {
233
272
  finalOpts.transport = opts.transport;
234
273
  }
274
+ if (opts.configSource !== undefined) {
275
+ finalOpts.configSource = opts.configSource;
276
+ }
235
277
  const actor = new ConnectionActor(finalOpts);
236
278
  return { actor, runtime, conn };
237
279
  }
@@ -785,6 +827,24 @@ describe('ConnectionActor — additional routing', () => {
785
827
  }
786
828
  });
787
829
 
830
+ it('routes WHOIS with no parameter through whoisReducer (just 318, never 421)', async () => {
831
+ // Covers the `targetNick.length > 0` false branch in routeWhois:
832
+ // a bare `WHOIS` resolves no target and defers to the reducer, which
833
+ // emits only the 318 end marker.
834
+ const { actor, runtime } = makeActor({});
835
+ runtime.calls.length = 0;
836
+
837
+ await actor.receiveTextFrame('WHOIS\r\n');
838
+
839
+ const sends = runtime.calls.filter((c) => c.method === 'send');
840
+ expect(sends.length).toBeGreaterThanOrEqual(1);
841
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
842
+ expect(allLines.some((l) => l.text.includes(' 318 '))).toBe(true);
843
+ for (const line of allLines) {
844
+ expect(line.text).not.toContain(' 421 ');
845
+ }
846
+ });
847
+
788
848
  it('routes WHOWAS of a quit nick through whowasReducer (314 + 369, never 421)', async () => {
789
849
  const history = new InMemoryNickHistoryStore(new FakeClock(1_000));
790
850
  history.record({
@@ -968,6 +1028,34 @@ describe('ConnectionActor — additional routing', () => {
968
1028
  }
969
1029
  });
970
1030
 
1031
+ it('routes SETNAME :<realname> through setnameReducer, mutating state.realname (never 421)', async () => {
1032
+ const { actor, runtime, conn } = makeActor({});
1033
+ runtime.calls.length = 0;
1034
+
1035
+ await actor.receiveTextFrame('SETNAME :New Name\r\n');
1036
+
1037
+ expect(conn.realname).toBe('New Name');
1038
+ // Silent update: no Send reply and no broadcast, just the state mutation.
1039
+ expect(runtime.calls).toHaveLength(0);
1040
+ });
1041
+
1042
+ it('routes SETNAME with no param to 461 (never 421)', async () => {
1043
+ const { actor, runtime } = makeActor({});
1044
+ runtime.calls.length = 0;
1045
+
1046
+ await actor.receiveTextFrame('SETNAME\r\n');
1047
+
1048
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1049
+ expect(sends).toHaveLength(1);
1050
+ const lines = sends[0]?.args[1] as RawLine[];
1051
+ expect(lines[0]?.text).toBe(':irc.example.com 461 alice SETNAME :Not enough parameters');
1052
+ for (const s of sends) {
1053
+ for (const line of s.args[1] as RawLine[]) {
1054
+ expect(line.text).not.toContain(' 421 ');
1055
+ }
1056
+ }
1057
+ });
1058
+
971
1059
  it('routes OPER with matching creds through operReducer (381, never 421)', async () => {
972
1060
  const { actor, runtime, conn } = makeActor({
973
1061
  operCreds: [{ user: 'alice', password: 'secret' }],
@@ -1046,6 +1134,212 @@ describe('ConnectionActor — additional routing', () => {
1046
1134
  expect(lines[0]?.text).toBe(':irc.example.com 421 alice :Unknown command');
1047
1135
  });
1048
1136
 
1137
+ it('routes WALLOPS from an oper through wallopsReducer (broadcastWallops, never 421)', async () => {
1138
+ const { actor, runtime } = makeActor({
1139
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1140
+ });
1141
+ runtime.calls.length = 0;
1142
+
1143
+ await actor.receiveTextFrame('WALLOPS :reboot in 5\r\n');
1144
+
1145
+ // Oper happy path: one broadcastWallops runtime call, carrying the
1146
+ // oper-attributed line and excepting the sender.
1147
+ const wallops = runtime.calls.filter((c) => c.method === 'broadcastWallops');
1148
+ expect(wallops).toHaveLength(1);
1149
+ const lines = wallops[0]?.args[0] as RawLine[];
1150
+ const except = wallops[0]?.args[1];
1151
+ expect(lines[0]?.text).toBe(':alice!alice@example.com WALLOPS :reboot in 5');
1152
+ expect(except).toBe('c1');
1153
+ // Critical: WALLOPS must reach the reducer, never the 421 fallthrough.
1154
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1155
+ for (const s of sends) {
1156
+ for (const line of s.args[1] as RawLine[]) {
1157
+ expect(line.text).not.toContain(' 421 ');
1158
+ }
1159
+ }
1160
+ });
1161
+
1162
+ it('routes WALLOPS from a non-oper through wallopsReducer (481, never 421)', async () => {
1163
+ const { actor, runtime } = makeActor({});
1164
+ runtime.calls.length = 0;
1165
+
1166
+ await actor.receiveTextFrame('WALLOPS :hi\r\n');
1167
+
1168
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1169
+ expect(sends).toHaveLength(1);
1170
+ const lines = sends[0]?.args[1] as RawLine[];
1171
+ expect(lines[0]?.text).toContain(' 481 ');
1172
+ // A non-oper must never trigger global fanout.
1173
+ expect(runtime.calls.some((c) => c.method === 'broadcastWallops')).toBe(false);
1174
+ for (const s of sends) {
1175
+ for (const line of s.args[1] as RawLine[]) {
1176
+ expect(line.text).not.toContain(' 421 ');
1177
+ }
1178
+ }
1179
+ });
1180
+
1181
+ it('routes WALLOPS with no message through wallopsReducer (412, never 421)', async () => {
1182
+ const { actor, runtime } = makeActor({
1183
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1184
+ });
1185
+ runtime.calls.length = 0;
1186
+
1187
+ await actor.receiveTextFrame('WALLOPS\r\n');
1188
+
1189
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1190
+ expect(sends).toHaveLength(1);
1191
+ const lines = sends[0]?.args[1] as RawLine[];
1192
+ expect(lines[0]?.text).toContain(' 412 ');
1193
+ expect(runtime.calls.some((c) => c.method === 'broadcastWallops')).toBe(false);
1194
+ for (const s of sends) {
1195
+ for (const line of s.args[1] as RawLine[]) {
1196
+ expect(line.text).not.toContain(' 421 ');
1197
+ }
1198
+ }
1199
+ });
1200
+
1201
+ it('routes REHASH from an oper through rehashReducer (382, never 421) and re-invokes the loader', async () => {
1202
+ const { actor, runtime, conn } = makeActor({
1203
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1204
+ configSource: 'KV',
1205
+ });
1206
+ runtime.reloadConfigResult = {
1207
+ serverName: 'irc.reloaded.test',
1208
+ networkName: 'ExampleNet',
1209
+ maxChannelsPerUser: 30,
1210
+ maxTargetsPerCommand: 10,
1211
+ maxListEntries: 50,
1212
+ nickLen: 30,
1213
+ channelLen: 50,
1214
+ topicLen: 390,
1215
+ quitMessage: 'Client Quit',
1216
+ };
1217
+ runtime.calls.length = 0;
1218
+
1219
+ await actor.receiveTextFrame('REHASH\r\n');
1220
+
1221
+ // 382 emitted, never 421.
1222
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1223
+ expect(sends).toHaveLength(1);
1224
+ const firstLines = sends[0]?.args[1] as RawLine[];
1225
+ expect(firstLines[0]?.text).toBe(':irc.example.com 382 alice :Rehashing from KV');
1226
+ for (const s of sends) {
1227
+ for (const line of s.args[1] as RawLine[]) {
1228
+ expect(line.text).not.toContain(' 421 ');
1229
+ }
1230
+ }
1231
+ // The bound loader was actually re-invoked.
1232
+ expect(runtime.calls.some((c) => c.method === 'reloadConfig')).toBe(true);
1233
+ void conn;
1234
+ // The live config was swapped: a subsequent REHASH's 382 carries the
1235
+ // reloaded server name (ctx is rebuilt from the swapped serverConfig).
1236
+ runtime.calls.length = 0;
1237
+ await actor.receiveTextFrame('REHASH\r\n');
1238
+ const secondSends = runtime.calls.filter((c) => c.method === 'send');
1239
+ const secondLines = secondSends[0]?.args[1] as RawLine[];
1240
+ expect(secondLines[0]?.text).toBe(':irc.reloaded.test 382 alice :Rehashing from KV');
1241
+ });
1242
+
1243
+ it('routes REHASH from a non-oper through rehashReducer (481) and does NOT reload', async () => {
1244
+ const { actor, runtime } = makeActor({ configSource: 'KV' });
1245
+ runtime.calls.length = 0;
1246
+
1247
+ await actor.receiveTextFrame('REHASH\r\n');
1248
+
1249
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1250
+ expect(sends).toHaveLength(1);
1251
+ const lines = sends[0]?.args[1] as RawLine[];
1252
+ expect(lines[0]?.text).toContain(' 481 ');
1253
+ // Critical: a non-oper must not trigger a config reload.
1254
+ expect(runtime.calls.some((c) => c.method === 'reloadConfig')).toBe(false);
1255
+ for (const s of sends) {
1256
+ for (const line of s.args[1] as RawLine[]) {
1257
+ expect(line.text).not.toContain(' 421 ');
1258
+ }
1259
+ }
1260
+ });
1261
+
1262
+ it('keeps the oper connected and retains the prior config when the loader rejects', async () => {
1263
+ const { actor, runtime } = makeActor({
1264
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1265
+ configSource: 'KV',
1266
+ });
1267
+ runtime.reloadConfigError = new Error('store unreachable');
1268
+ runtime.calls.length = 0;
1269
+
1270
+ await actor.receiveTextFrame('REHASH\r\n');
1271
+
1272
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1273
+ // Optimistic 382 + graceful error-suffixed 382; oper NOT disconnected.
1274
+ expect(sends).toHaveLength(2);
1275
+ const lines = [
1276
+ ...((sends[0]?.args[1] as RawLine[]) ?? []),
1277
+ ...((sends[1]?.args[1] as RawLine[]) ?? []),
1278
+ ];
1279
+ expect(lines[0]?.text).toBe(':irc.example.com 382 alice :Rehashing from KV');
1280
+ expect(lines[1]?.text).toBe(
1281
+ ':irc.example.com 382 alice :Rehashing from KV failed: store unreachable',
1282
+ );
1283
+ // The oper is never disconnected on a reload failure.
1284
+ expect(runtime.calls.some((c) => c.method === 'disconnect')).toBe(false);
1285
+ // Prior config retained: a subsequent command still sees the old name.
1286
+ runtime.calls.length = 0;
1287
+ await actor.receiveTextFrame('REHASH\r\n');
1288
+ const retrySends = runtime.calls.filter((c) => c.method === 'send');
1289
+ const retryLines = retrySends[0]?.args[1] as RawLine[];
1290
+ expect(retryLines[0]?.text).toBe(':irc.example.com 382 alice :Rehashing from KV');
1291
+ });
1292
+
1293
+ it('stringifies a non-Error rejection in the graceful 382 suffix', async () => {
1294
+ const { actor, runtime } = makeActor({
1295
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1296
+ configSource: 'KV',
1297
+ });
1298
+ // A non-Error throw exercises the `String(err)` branch of the suffix
1299
+ // formatting (vs. `err.message` for a real Error).
1300
+ runtime.reloadConfigError = 'totally broken';
1301
+ runtime.calls.length = 0;
1302
+
1303
+ await actor.receiveTextFrame('REHASH\r\n');
1304
+
1305
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1306
+ expect(sends).toHaveLength(2);
1307
+ const errorLine = (sends[1]?.args[1] as RawLine[])[0];
1308
+ expect(errorLine?.text).toBe(
1309
+ ':irc.example.com 382 alice :Rehashing from KV failed: totally broken',
1310
+ );
1311
+ expect(runtime.calls.some((c) => c.method === 'disconnect')).toBe(false);
1312
+ });
1313
+
1314
+ it('falls back to "*" in the graceful 382 suffix when the oper has no nick', async () => {
1315
+ // Covers the `this.state.nick ?? '*'` fallback in routeRehash's catch
1316
+ // block. Oper state without a nick is unusual (registration normally
1317
+ // sets the nick before OPER can succeed) but the actor must still
1318
+ // format a valid numeric addressed to "*" rather than crash.
1319
+ const { actor, runtime, conn } = makeActor({
1320
+ conn: { userModes: { invisible: false, oper: true, wallops: false, serverNotices: false } },
1321
+ configSource: 'KV',
1322
+ });
1323
+ // Clear the nick post-construction (exactOptionalPropertyTypes forbids
1324
+ // assigning undefined; delete is the idiomatic way to unset an optional).
1325
+ // biome-ignore lint/performance/noDelete: exactOptionalPropertyTypes forbids `= undefined`.
1326
+ delete conn.nick;
1327
+ runtime.reloadConfigError = new Error('store unreachable');
1328
+ runtime.calls.length = 0;
1329
+
1330
+ await actor.receiveTextFrame('REHASH\r\n');
1331
+
1332
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1333
+ expect(sends.length).toBeGreaterThanOrEqual(1);
1334
+ for (const s of sends) {
1335
+ for (const line of s.args[1] as RawLine[]) {
1336
+ // Addressed to "*" (no nick), never 421.
1337
+ expect(line.text).toContain(' 382 * ');
1338
+ expect(line.text).not.toContain(' 421 ');
1339
+ }
1340
+ }
1341
+ });
1342
+
1049
1343
  it('covers JOIN with no params (splitCsv("") → [])', async () => {
1050
1344
  const { actor, runtime } = makeActor({});
1051
1345
  await actor.receiveTextFrame('JOIN\r\n');
@@ -1552,6 +1846,27 @@ describe('ConnectionActor — CHATHISTORY', () => {
1552
1846
  expect(sends).toHaveLength(0);
1553
1847
  });
1554
1848
 
1849
+ it('handles a missing target parameter gracefully (no 421)', async () => {
1850
+ // Covers the `target !== undefined` false branch in routeChathistory:
1851
+ // a CHATHISTORY message with only the subcommand and no target param.
1852
+ // peekChannel is not called; the reducer surfaces the param error.
1853
+ const store = new InMemoryMessageStore();
1854
+ const { actor, runtime } = makeActor({
1855
+ conn: { caps: CHATHISTORY_CAPS },
1856
+ messages: store,
1857
+ });
1858
+ runtime.calls.length = 0;
1859
+
1860
+ await actor.receiveTextFrame('CHATHISTORY LATEST\r\n');
1861
+
1862
+ const sends = runtime.calls.filter((c) => c.method === 'send');
1863
+ for (const s of sends) {
1864
+ for (const line of s.args[1] as RawLine[]) {
1865
+ expect(line.text).not.toContain(' 421 ');
1866
+ }
1867
+ }
1868
+ });
1869
+
1555
1870
  it('records a channel PRIVMSG into the bound store (JOIN auto-playback data source)', async () => {
1556
1871
  const store = new InMemoryMessageStore();
1557
1872
  const { actor, runtime } = makeActor({ messages: store });
@@ -1850,6 +2165,30 @@ describe('ConnectionActor — server-info commands (VERSION/TIME/ADMIN/INFO/USER
1850
2165
  expect(lines[0]?.text).toContain('bob*=-bobuser@bobhost');
1851
2166
  });
1852
2167
 
2168
+ it('USERHOST treats a nick whose conn tore down mid-resolve as offline', async () => {
2169
+ // Covers the `conn === null` branch in routeUserhost: lookupNick
2170
+ // resolves but the live connection is gone (race between the two
2171
+ // awaits). The reducer must treat the nick as offline rather than
2172
+ // crash.
2173
+ const { actor, runtime, conn } = makeActor({});
2174
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2175
+ bobState.nick = 'bob';
2176
+ bobState.user = 'bobuser';
2177
+ bobState.host = 'bobhost';
2178
+ bobState.registration = 'registered';
2179
+ runtime.addConnection(conn);
2180
+ runtime.addConnection(bobState);
2181
+ runtime.dropConnection('c2'); // race: nick map lags the connection map
2182
+ runtime.calls.length = 0;
2183
+
2184
+ await actor.receiveTextFrame('USERHOST bob\r\n');
2185
+
2186
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2187
+ const lines = sends[0]?.args[1] as RawLine[];
2188
+ expect(lines[0]?.text).toContain(' 302 ');
2189
+ expect(lines[0]?.text).not.toContain('bobuser@bobhost');
2190
+ });
2191
+
1853
2192
  it('routes ISON resolving online nicks (303, never 421)', async () => {
1854
2193
  const { actor, runtime, conn } = makeActor({});
1855
2194
  const bobState = createConnection({ id: 'c2', connectedSince: 0 });
@@ -1885,4 +2224,753 @@ describe('ConnectionActor — server-info commands (VERSION/TIME/ADMIN/INFO/USER
1885
2224
  const lines = sends[0]?.args[1] as RawLine[];
1886
2225
  expect(lines[0]?.text).toContain(':Bob');
1887
2226
  });
2227
+
2228
+ it('skips a nick whose connection has torn down between lookupNick and getConnection (race)', async () => {
2229
+ // Covers the `conn?.nick !== undefined` false branch in routeIson:
2230
+ // lookupNick resolves because the nick map still holds the entry, but
2231
+ // the connection is already gone from the live-connection store.
2232
+ const { actor, runtime, conn } = makeActor({});
2233
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2234
+ bobState.nick = 'bob';
2235
+ bobState.registration = 'registered';
2236
+ runtime.addConnection(conn);
2237
+ runtime.addConnection(bobState);
2238
+ // Drop the live connection but leave the nick registered.
2239
+ runtime.dropConnection('c2');
2240
+ runtime.calls.length = 0;
2241
+
2242
+ await actor.receiveTextFrame('ISON bob\r\n');
2243
+
2244
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2245
+ const lines = sends[0]?.args[1] as RawLine[];
2246
+ // 303 reply lists no nicks (bob was dropped mid-resolve).
2247
+ expect(lines[0]?.text).toContain(' 303 ');
2248
+ expect(lines[0]?.text).not.toContain('bob');
2249
+ expect(lines[0]?.text).not.toContain(' 421 ');
2250
+ });
2251
+ });
2252
+
2253
+ describe('ConnectionActor — KILL (oper force-disconnect)', () => {
2254
+ it('routes an oper KILL through killReducer, invoking runtime.disconnect for the target (never 421)', async () => {
2255
+ const { actor, runtime, conn } = makeActor({});
2256
+ conn.userModes.oper = true;
2257
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2258
+ bobState.nick = 'bob';
2259
+ bobState.user = 'bob';
2260
+ bobState.host = 'example.com';
2261
+ bobState.realname = 'Bob';
2262
+ bobState.registration = 'registered';
2263
+ runtime.addConnection(conn);
2264
+ runtime.addConnection(bobState);
2265
+ runtime.calls.length = 0;
2266
+
2267
+ await actor.receiveTextFrame('KILL bob :flooding\r\n');
2268
+
2269
+ const disconnects = runtime.calls.filter((c) => c.method === 'disconnect');
2270
+ expect(disconnects).toHaveLength(1);
2271
+ expect(disconnects[0]?.args[0]).toBe('c2');
2272
+ expect(disconnects[0]?.args[1]).toBe('Killed (alice (flooding))');
2273
+ // Critical: never 421 for a routed KILL.
2274
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2275
+ for (const s of sends) {
2276
+ for (const line of s.args[1] as RawLine[]) {
2277
+ expect(line.text).not.toContain(' 421 ');
2278
+ }
2279
+ }
2280
+ });
2281
+
2282
+ it('emits 481 ERR_NOPRIVILEGES (never 421, no disconnect) when the sender is not an oper', async () => {
2283
+ const { actor, runtime } = makeActor({});
2284
+ runtime.calls.length = 0;
2285
+
2286
+ await actor.receiveTextFrame('KILL bob :flooding\r\n');
2287
+
2288
+ const disconnects = runtime.calls.filter((c) => c.method === 'disconnect');
2289
+ expect(disconnects).toHaveLength(0);
2290
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2291
+ expect(sends).toHaveLength(1);
2292
+ const lines = sends[0]?.args[1] as RawLine[];
2293
+ expect(lines[0]?.text).toContain(' 481 ');
2294
+ expect(lines[0]?.text).not.toContain(' 421 ');
2295
+ });
2296
+
2297
+ it('emits 401 ERR_NOSUCHNICK for an unknown target (never 421, no disconnect)', async () => {
2298
+ const { actor, runtime, conn } = makeActor({});
2299
+ conn.userModes.oper = true;
2300
+ runtime.calls.length = 0;
2301
+
2302
+ await actor.receiveTextFrame('KILL ghost :nope\r\n');
2303
+
2304
+ const disconnects = runtime.calls.filter((c) => c.method === 'disconnect');
2305
+ expect(disconnects).toHaveLength(0);
2306
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2307
+ expect(sends).toHaveLength(1);
2308
+ const lines = sends[0]?.args[1] as RawLine[];
2309
+ expect(lines[0]?.text).toContain(' 401 ');
2310
+ expect(lines[0]?.text).toContain(' ghost ');
2311
+ expect(lines[0]?.text).not.toContain(' 421 ');
2312
+ });
2313
+
2314
+ it('emits 461 ERR_NEEDMOREPARAMS when the comment is missing (never 421)', async () => {
2315
+ const { actor, runtime, conn } = makeActor({});
2316
+ conn.userModes.oper = true;
2317
+ runtime.calls.length = 0;
2318
+
2319
+ await actor.receiveTextFrame('KILL bob\r\n');
2320
+
2321
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2322
+ expect(sends).toHaveLength(1);
2323
+ const lines = sends[0]?.args[1] as RawLine[];
2324
+ expect(lines[0]?.text).toContain(' 461 ');
2325
+ expect(lines[0]?.text).toContain(' KILL ');
2326
+ expect(lines[0]?.text).not.toContain(' 421 ');
2327
+ });
2328
+
2329
+ it('emits 461 when an oper sends KILL with no params at all (never 421)', async () => {
2330
+ const { actor, runtime, conn } = makeActor({});
2331
+ conn.userModes.oper = true;
2332
+ runtime.calls.length = 0;
2333
+
2334
+ await actor.receiveTextFrame('KILL\r\n');
2335
+
2336
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2337
+ expect(sends).toHaveLength(1);
2338
+ const lines = sends[0]?.args[1] as RawLine[];
2339
+ expect(lines[0]?.text).toContain(' 461 ');
2340
+ expect(lines[0]?.text).not.toContain(' 421 ');
2341
+ });
2342
+
2343
+ it('treats a nick whose connection vanished mid-route as unknown (401, never 421)', async () => {
2344
+ const { actor, runtime, conn } = makeActor({});
2345
+ conn.userModes.oper = true;
2346
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2347
+ bobState.nick = 'bob';
2348
+ bobState.registration = 'registered';
2349
+ runtime.addConnection(conn);
2350
+ runtime.addConnection(bobState);
2351
+ // lookupNick will still resolve bob, but getConnection returns null.
2352
+ runtime.dropConnection('c2');
2353
+ runtime.calls.length = 0;
2354
+
2355
+ await actor.receiveTextFrame('KILL bob :flooding\r\n');
2356
+
2357
+ const disconnects = runtime.calls.filter((c) => c.method === 'disconnect');
2358
+ expect(disconnects).toHaveLength(0);
2359
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2360
+ expect(sends).toHaveLength(1);
2361
+ const lines = sends[0]?.args[1] as RawLine[];
2362
+ expect(lines[0]?.text).toContain(' 401 ');
2363
+ expect(lines[0]?.text).not.toContain(' 421 ');
2364
+ });
2365
+ });
2366
+
2367
+ describe('ConnectionActor — TRACE (routing diagnostics)', () => {
2368
+ it('emits the 262 terminator and never 421 for TRACE with no target', async () => {
2369
+ const { actor, runtime } = makeActor({});
2370
+ runtime.calls.length = 0;
2371
+
2372
+ await actor.receiveTextFrame('TRACE\r\n');
2373
+
2374
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2375
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2376
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2377
+ expect(allLines[allLines.length - 1]?.text).toContain(' 262 ');
2378
+ for (const line of allLines) {
2379
+ expect(line.text).not.toContain(' 421 ');
2380
+ }
2381
+ });
2382
+
2383
+ it('routes oper TRACE <nick> through traceReducer (205 + 262, never 421)', async () => {
2384
+ const { actor, runtime, conn } = makeActor({});
2385
+ conn.userModes.oper = true;
2386
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2387
+ bobState.nick = 'bob';
2388
+ bobState.user = 'bob';
2389
+ bobState.host = 'example.com';
2390
+ bobState.realname = 'Bob';
2391
+ bobState.registration = 'registered';
2392
+ runtime.addConnection(conn);
2393
+ runtime.addConnection(bobState);
2394
+ runtime.calls.length = 0;
2395
+
2396
+ await actor.receiveTextFrame('TRACE bob\r\n');
2397
+
2398
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2399
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2400
+ const firstBatch = sends[0]?.args[1] as RawLine[];
2401
+ expect(firstBatch[0]?.text).toContain(' 205 ');
2402
+ expect(firstBatch[0]?.text).toContain(' bob ');
2403
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2404
+ expect(allLines[allLines.length - 1]?.text).toContain(' 262 ');
2405
+ for (const line of allLines) {
2406
+ expect(line.text).not.toContain(' 421 ');
2407
+ }
2408
+ });
2409
+
2410
+ it('emits 402 ERR_NOSUCHSERVER for a non-local server target (never 421)', async () => {
2411
+ const { actor, runtime } = makeActor({});
2412
+ runtime.calls.length = 0;
2413
+
2414
+ await actor.receiveTextFrame('TRACE remote.example.org\r\n');
2415
+
2416
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2417
+ expect(sends).toHaveLength(1);
2418
+ const lines = sends[0]?.args[1] as RawLine[];
2419
+ expect(lines[0]?.text).toContain(' 402 ');
2420
+ expect(lines[0]?.text).toContain(' remote.example.org ');
2421
+ expect(lines[0]?.text).not.toContain(' 421 ');
2422
+ });
2423
+
2424
+ it('treats the local server name as the local server (no 402, never 421)', async () => {
2425
+ const { actor, runtime } = makeActor({});
2426
+ runtime.calls.length = 0;
2427
+
2428
+ await actor.receiveTextFrame('TRACE irc.example.com\r\n');
2429
+
2430
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2431
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2432
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2433
+ expect(allLines[allLines.length - 1]?.text).toContain(' 262 ');
2434
+ for (const line of allLines) {
2435
+ expect(line.text).not.toContain(' 402 ');
2436
+ expect(line.text).not.toContain(' 421 ');
2437
+ }
2438
+ });
2439
+
2440
+ it('treats a nick whose connection vanished mid-route as the no-target path (262, never 421)', async () => {
2441
+ const { actor, runtime, conn } = makeActor({});
2442
+ conn.userModes.oper = true;
2443
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2444
+ bobState.nick = 'bob';
2445
+ bobState.registration = 'registered';
2446
+ runtime.addConnection(conn);
2447
+ runtime.addConnection(bobState);
2448
+ // lookupNick resolves bob, but getConnection returns null.
2449
+ runtime.dropConnection('c2');
2450
+ runtime.calls.length = 0;
2451
+
2452
+ await actor.receiveTextFrame('TRACE bob\r\n');
2453
+
2454
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2455
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2456
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2457
+ expect(allLines[allLines.length - 1]?.text).toContain(' 262 ');
2458
+ for (const line of allLines) {
2459
+ expect(line.text).not.toContain(' 421 ');
2460
+ }
2461
+ });
2462
+ });
2463
+
2464
+ describe('ConnectionActor — dropped S2S and obsolete verbs', () => {
2465
+ // These verbs are formally dropped (not deferred):
2466
+ // - S2S (PLAN non-goal): CONNECT, SQUIT, LINKS
2467
+ // - RFC 2812 obsolete (never widely implemented): SERVICE, SUMMON, USERS
2468
+ // They keep returning `421 ERR_UNKNOWNCOMMAND` (the correct response for an
2469
+ // unimplemented command); the regression guard below pins that behaviour so
2470
+ // a future accidental `case` label is caught.
2471
+ const droppedVerbs = ['CONNECT', 'SQUIT', 'LINKS', 'SERVICE', 'SUMMON', 'USERS'];
2472
+
2473
+ it.each(droppedVerbs)('emits 421 for the dropped verb %s', async (verb) => {
2474
+ const { actor, runtime } = makeActor({});
2475
+ await actor.receiveTextFrame(`${verb} ignored-arg\r\n`);
2476
+
2477
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2478
+ expect(sends).toHaveLength(1);
2479
+ const lines = sends[0]?.args[1] as RawLine[];
2480
+ expect(lines[0]?.text).toContain(' 421 ');
2481
+ expect(lines[0]?.text).toContain(` ${verb} `);
2482
+ });
2483
+
2484
+ it('documents the deliberate drop at the default branch (no "deferred" framing)', () => {
2485
+ // The default branch must explain these verbs are deliberately dropped,
2486
+ // not still "deferred" (the framing removed by the S2S / obsolete-verb
2487
+ // drop).
2488
+ expect(actorSource).toMatch(/S2S/);
2489
+ expect(actorSource).toMatch(/non-goal/);
2490
+ expect(actorSource).toMatch(/obsolete/i);
2491
+ // The CONNECT/SQUIT/LINKS and SERVICE/SUMMON/USERS lines must not be
2492
+ // framed as deferred.
2493
+ expect(actorSource).not.toMatch(/deferred LINKS/);
2494
+ expect(actorSource).not.toMatch(/deferred SQUIT/);
2495
+ expect(actorSource).not.toMatch(/deferred SERVICE/);
2496
+ expect(actorSource).not.toMatch(/deferred SUMMON/);
2497
+ expect(actorSource).not.toMatch(/deferred USERS/);
2498
+ });
2499
+ });
2500
+
2501
+ // ---------------------------------------------------------------------------
2502
+ // FakeServerStats + helpers
2503
+ // ---------------------------------------------------------------------------
2504
+
2505
+ /** Stats backend double that returns a fixed snapshot. */
2506
+ class FakeServerStats implements ServerStats {
2507
+ constructor(readonly snapshot: ServerStatsSnapshot) {}
2508
+ async getStats(): Promise<ServerStatsSnapshot> {
2509
+ return this.snapshot;
2510
+ }
2511
+ }
2512
+
2513
+ const populatedSnapshot: ServerStatsSnapshot = {
2514
+ users: 4,
2515
+ invisible: 1,
2516
+ opers: 2,
2517
+ unknownConnections: 1,
2518
+ channels: 3,
2519
+ servers: 1,
2520
+ localConns: 6,
2521
+ globalConns: 6,
2522
+ maxLocalConns: 9,
2523
+ maxGlobalConns: 9,
2524
+ uptimeStartedAt: 0,
2525
+ };
2526
+
2527
+ // ---------------------------------------------------------------------------
2528
+ // LUSERS actor routing
2529
+ // ---------------------------------------------------------------------------
2530
+
2531
+ describe('ConnectionActor — LUSERS (network statistics)', () => {
2532
+ it('emits 251–255 from the bound ServerStats snapshot and never 421', async () => {
2533
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2534
+ runtime.calls.length = 0;
2535
+
2536
+ await actor.receiveTextFrame('LUSERS\r\n');
2537
+
2538
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2539
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2540
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2541
+ const codes = allLines.map((l) => Number(l.text.split(' ')[1]));
2542
+ expect(codes).toContain(251);
2543
+ expect(codes).toContain(252);
2544
+ expect(codes).toContain(253);
2545
+ expect(codes).toContain(254);
2546
+ expect(codes).toContain(255);
2547
+ expect(codes).toContain(265);
2548
+ expect(codes).toContain(266);
2549
+ for (const line of allLines) {
2550
+ expect(line.text).not.toContain(' 421 ');
2551
+ }
2552
+ });
2553
+
2554
+ it('awaits the stats backend before invoking the reducer (async getStats)', async () => {
2555
+ let calls = 0;
2556
+ const stats: ServerStats = {
2557
+ async getStats() {
2558
+ calls++;
2559
+ return populatedSnapshot;
2560
+ },
2561
+ };
2562
+ const { actor, runtime } = makeActor({ stats });
2563
+ runtime.calls.length = 0;
2564
+
2565
+ await actor.receiveTextFrame('LUSERS\r\n');
2566
+
2567
+ expect(calls).toBe(1);
2568
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2569
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2570
+ // The 251 line carries the snapshot's user/invisible/server counts.
2571
+ expect(allLines.some((l) => l.text.includes(' 251 ') && l.text.includes('4 users'))).toBe(true);
2572
+ });
2573
+
2574
+ it('emits 402 ERR_NOSUCHSERVER for a non-local server target (never 421)', async () => {
2575
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2576
+ runtime.calls.length = 0;
2577
+
2578
+ await actor.receiveTextFrame('LUSERS * remote.example.org\r\n');
2579
+
2580
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2581
+ expect(sends).toHaveLength(1);
2582
+ const lines = sends[0]?.args[1] as RawLine[];
2583
+ expect(lines[0]?.text).toContain(' 402 ');
2584
+ expect(lines[0]?.text).toContain(' remote.example.org ');
2585
+ expect(lines[0]?.text).not.toContain(' 421 ');
2586
+ });
2587
+
2588
+ it('treats the local server name as the local server (no 402, never 421)', async () => {
2589
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2590
+ runtime.calls.length = 0;
2591
+
2592
+ await actor.receiveTextFrame('LUSERS * irc.example.com\r\n');
2593
+
2594
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2595
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2596
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2597
+ for (const line of allLines) {
2598
+ expect(line.text).not.toContain(' 402 ');
2599
+ expect(line.text).not.toContain(' 421 ');
2600
+ }
2601
+ });
2602
+
2603
+ it('falls back to a zero-count snapshot when no stats backend is bound (never 421)', async () => {
2604
+ const { actor, runtime } = makeActor({});
2605
+ runtime.calls.length = 0;
2606
+
2607
+ await actor.receiveTextFrame('LUSERS\r\n');
2608
+
2609
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2610
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2611
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2612
+ // 251 still emitted with zero counts; 252/253/254 suppressed (zero).
2613
+ expect(allLines.some((l) => l.text.includes(' 251 '))).toBe(true);
2614
+ expect(allLines.some((l) => l.text.includes(' 255 '))).toBe(true);
2615
+ for (const line of allLines) {
2616
+ expect(line.text).not.toContain(' 252 ');
2617
+ expect(line.text).not.toContain(' 253 ');
2618
+ expect(line.text).not.toContain(' 254 ');
2619
+ expect(line.text).not.toContain(' 421 ');
2620
+ }
2621
+ });
2622
+ });
2623
+
2624
+ // ---------------------------------------------------------------------------
2625
+ // STATS actor routing
2626
+ // ---------------------------------------------------------------------------
2627
+
2628
+ describe('ConnectionActor — STATS (server statistics)', () => {
2629
+ it('routes STATS u to a 242 + 219 reply (never 421)', async () => {
2630
+ const clock = new FakeClock(5_000);
2631
+ const { actor, runtime } = makeActor({
2632
+ stats: new FakeServerStats({ ...populatedSnapshot, uptimeStartedAt: 0 }),
2633
+ clock,
2634
+ });
2635
+ runtime.calls.length = 0;
2636
+
2637
+ await actor.receiveTextFrame('STATS u\r\n');
2638
+
2639
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2640
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2641
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2642
+ expect(allLines.some((l) => l.text.includes(' 242 '))).toBe(true);
2643
+ expect(allLines[allLines.length - 1]?.text).toContain(' 219 ');
2644
+ expect(allLines[allLines.length - 1]?.text).toContain(' u ');
2645
+ for (const line of allLines) {
2646
+ expect(line.text).not.toContain(' 421 ');
2647
+ }
2648
+ });
2649
+
2650
+ it('routes STATS l to a 211 + 219 reply (never 421)', async () => {
2651
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2652
+ runtime.calls.length = 0;
2653
+
2654
+ await actor.receiveTextFrame('STATS l\r\n');
2655
+
2656
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2657
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2658
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2659
+ expect(allLines.some((l) => l.text.includes(' 211 '))).toBe(true);
2660
+ expect(allLines[allLines.length - 1]?.text).toContain(' 219 ');
2661
+ for (const line of allLines) {
2662
+ expect(line.text).not.toContain(' 421 ');
2663
+ }
2664
+ });
2665
+
2666
+ it('routes an unknown STATS letter to a charybdis-style empty body + 219 (never 421)', async () => {
2667
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2668
+ runtime.calls.length = 0;
2669
+
2670
+ await actor.receiveTextFrame('STATS z\r\n');
2671
+
2672
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2673
+ expect(sends).toHaveLength(1);
2674
+ const lines = sends[0]?.args[1] as RawLine[];
2675
+ expect(lines[0]?.text).toContain(' 219 ');
2676
+ expect(lines[0]?.text).toContain(' z ');
2677
+ expect(lines[0]?.text).not.toContain(' 421 ');
2678
+ });
2679
+
2680
+ it('emits 402 ERR_NOSUCHSERVER for a non-local server target (never 421)', async () => {
2681
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2682
+ runtime.calls.length = 0;
2683
+
2684
+ await actor.receiveTextFrame('STATS u remote.example.org\r\n');
2685
+
2686
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2687
+ expect(sends).toHaveLength(1);
2688
+ const lines = sends[0]?.args[1] as RawLine[];
2689
+ expect(lines[0]?.text).toContain(' 402 ');
2690
+ expect(lines[0]?.text).toContain(' remote.example.org ');
2691
+ expect(lines[0]?.text).not.toContain(' 421 ');
2692
+ });
2693
+
2694
+ it('treats the local server name as the local server (no 402, never 421)', async () => {
2695
+ const { actor, runtime } = makeActor({ stats: new FakeServerStats(populatedSnapshot) });
2696
+ runtime.calls.length = 0;
2697
+
2698
+ await actor.receiveTextFrame('STATS u irc.example.com\r\n');
2699
+
2700
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2701
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2702
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2703
+ for (const line of allLines) {
2704
+ expect(line.text).not.toContain(' 402 ');
2705
+ expect(line.text).not.toContain(' 421 ');
2706
+ }
2707
+ });
2708
+ });
2709
+
2710
+ // ============================================================================
2711
+ // channelTargetsForMessage — pure dispatch table for prefetch
2712
+ // ============================================================================
2713
+
2714
+ describe('channelTargetsForMessage', () => {
2715
+ it('returns the channel list for each channel-touching verb', () => {
2716
+ // Covers every case clause in the switch.
2717
+ expect(channelTargetsForMessage({ command: 'PART', params: ['#a,#b'], tags: {} })).toEqual([
2718
+ '#a',
2719
+ '#b',
2720
+ ]);
2721
+ expect(channelTargetsForMessage({ command: 'TOPIC', params: ['#foo'], tags: {} })).toEqual([
2722
+ '#foo',
2723
+ ]);
2724
+ expect(channelTargetsForMessage({ command: 'NAMES', params: ['#foo'], tags: {} })).toEqual([
2725
+ '#foo',
2726
+ ]);
2727
+ expect(
2728
+ channelTargetsForMessage({ command: 'KICK', params: ['#foo', 'bob'], tags: {} }),
2729
+ ).toEqual(['#foo']);
2730
+ expect(channelTargetsForMessage({ command: 'MODE', params: ['#foo', '+t'], tags: {} })).toEqual(
2731
+ ['#foo'],
2732
+ );
2733
+ expect(
2734
+ channelTargetsForMessage({ command: 'PRIVMSG', params: ['#foo', 'hi'], tags: {} }),
2735
+ ).toEqual(['#foo']);
2736
+ expect(
2737
+ channelTargetsForMessage({ command: 'NOTICE', params: ['#foo', 'hi'], tags: {} }),
2738
+ ).toEqual(['#foo']);
2739
+ expect(channelTargetsForMessage({ command: 'TAGMSG', params: ['#foo'], tags: {} })).toEqual([
2740
+ '#foo',
2741
+ ]);
2742
+ });
2743
+
2744
+ it('returns the JOIN channel(s), skipping the `JOIN 0` sentinel', () => {
2745
+ expect(channelTargetsForMessage({ command: 'JOIN', params: ['#a,#b'], tags: {} })).toEqual([
2746
+ '#a',
2747
+ '#b',
2748
+ ]);
2749
+ // `JOIN 0` is the part-everything sentinel; reducer reads
2750
+ // joinedChannels directly, so no prefetch target.
2751
+ expect(channelTargetsForMessage({ command: 'JOIN', params: ['0'], tags: {} })).toEqual([]);
2752
+ });
2753
+
2754
+ it('reads the channel from the SECOND param for INVITE (<nick> <channel>)', () => {
2755
+ expect(
2756
+ channelTargetsForMessage({ command: 'INVITE', params: ['bob', '#room'], tags: {} }),
2757
+ ).toEqual(['#room']);
2758
+ });
2759
+
2760
+ it('returns an empty list for commands with no channel target', () => {
2761
+ // Covers the default branch (PING, NICK, QUIT, …).
2762
+ expect(channelTargetsForMessage({ command: 'PING', params: ['t'], tags: {} })).toEqual([]);
2763
+ expect(channelTargetsForMessage({ command: 'NICK', params: ['alice'], tags: {} })).toEqual([]);
2764
+ });
2765
+
2766
+ it('ignores non-channel targets in the comma-split list', () => {
2767
+ // A PRIVMSG to a nick (not a #channel) yields no prefetch targets.
2768
+ expect(
2769
+ channelTargetsForMessage({ command: 'PRIVMSG', params: ['bob,#foo', 'hi'], tags: {} }),
2770
+ ).toEqual(['#foo']);
2771
+ });
2772
+
2773
+ it('handles a missing first parameter gracefully', () => {
2774
+ // Defensive: params[0] is absent → empty string → empty split.
2775
+ expect(channelTargetsForMessage({ command: 'PART', params: [], tags: {} })).toEqual([]);
2776
+ });
2777
+
2778
+ it('returns an empty list for JOIN / INVITE when their channel param is absent', () => {
2779
+ // Covers the `?? ''` fallbacks on the JOIN and INVITE branches.
2780
+ expect(channelTargetsForMessage({ command: 'JOIN', params: [], tags: {} })).toEqual([]);
2781
+ expect(channelTargetsForMessage({ command: 'INVITE', params: ['bob'], tags: {} })).toEqual([]);
2782
+ });
2783
+ });
2784
+
2785
+ // ============================================================================
2786
+ // prefetchChannelState — refresh hook for distributed runtimes
2787
+ // ============================================================================
2788
+
2789
+ describe('ConnectionActor — prefetchChannelState (refresh hook)', () => {
2790
+ it('invokes refreshChannel for each channel-targeted command when the hook is bound', async () => {
2791
+ // Covers the body of prefetchChannelState (refresh !== undefined path)
2792
+ // and the call to channelTargetsForMessage for PART. Distributed
2793
+ // runtimes (CF ChannelDO) wire `refreshChannel`; the in-memory fake
2794
+ // does not, so we inject a custom channel-access here.
2795
+ const refreshed: string[] = [];
2796
+ const channelsAccess: ActorChannelAccess = {
2797
+ getOrCreateChannel: (name: ChanName) => {
2798
+ const key = name.toLowerCase();
2799
+ const existing = fakeChannels.get(key);
2800
+ if (existing) return existing;
2801
+ const chan = createChannel({ name, nameLower: key, createdAt: 0 });
2802
+ fakeChannels.set(key, chan);
2803
+ return chan;
2804
+ },
2805
+ refreshChannel: async (name: ChanName) => {
2806
+ refreshed.push(name);
2807
+ },
2808
+ };
2809
+ const fakeChannels = new Map<string, ChannelState>();
2810
+ const { actor, runtime } = makeActor({});
2811
+ // Swap in the channel access with the refresh hook.
2812
+ (actor as unknown as { channels: ActorChannelAccess }).channels = channelsAccess;
2813
+ // Pre-add membership so PART can find the channel.
2814
+ await runtime.applyChannelDelta('#foo', {
2815
+ memberships: [{ type: 'add', conn: 'c1', nick: 'alice' }],
2816
+ });
2817
+ runtime.calls.length = 0;
2818
+
2819
+ await actor.receiveTextFrame('PART #foo :leaving\r\n');
2820
+
2821
+ expect(refreshed).toEqual(['#foo']);
2822
+ });
2823
+
2824
+ it('is a no-op when refreshChannel is not bound (in-memory runtime)', async () => {
2825
+ // Covers the early-return path. The default FakeRuntime has no
2826
+ // refreshChannel; the actor must not throw and the PART still routes.
2827
+ const { actor, runtime } = makeActor({});
2828
+ await runtime.applyChannelDelta('#foo', {
2829
+ memberships: [{ type: 'add', conn: 'c1', nick: 'alice' }],
2830
+ });
2831
+ runtime.calls.length = 0;
2832
+
2833
+ await expect(actor.receiveTextFrame('PART #foo\r\n')).resolves.toBeUndefined();
2834
+ expect(runtime.calls.some((c) => c.method === 'applyChannelDelta')).toBe(true);
2835
+ });
2836
+
2837
+ it('skips refresh for commands with no channel target (empty targets list)', async () => {
2838
+ // Covers the `targets.length === 0` early-return in
2839
+ // prefetchChannelState: refreshChannel is bound but the command (PING)
2840
+ // has no channel argument, so the refresh hook must not fire.
2841
+ const refreshed: string[] = [];
2842
+ const { actor, runtime } = makeActor({});
2843
+ const accessWithRefresh: ActorChannelAccess = {
2844
+ getOrCreateChannel: (name: ChanName) => runtime.getOrCreateChannel(name),
2845
+ refreshChannel: async (name: ChanName) => {
2846
+ refreshed.push(name);
2847
+ },
2848
+ };
2849
+ (actor as unknown as { channels: ActorChannelAccess }).channels = accessWithRefresh;
2850
+ runtime.calls.length = 0;
2851
+
2852
+ await actor.receiveTextFrame('PING :token\r\n');
2853
+
2854
+ expect(refreshed).toEqual([]);
2855
+ });
2856
+ });
2857
+
2858
+ // ============================================================================
2859
+ // peekChannel — getChannel fallback
2860
+ // ============================================================================
2861
+
2862
+ describe('ConnectionActor — peekChannel fallback', () => {
2863
+ it('falls back to getOrCreateChannel when getChannel is not implemented', async () => {
2864
+ // Covers the fallback branch in peekChannel: a channel-access object
2865
+ // that omits `getChannel` (the legacy shape before peek-only lookup) must
2866
+ // still resolve CHATHISTORY targets via getOrCreateChannel. The actor
2867
+ // is reconstructed with a wrapper that delegates everything except
2868
+ // getChannel (which is intentionally absent).
2869
+ const store = new InMemoryMessageStore();
2870
+ const { actor, runtime } = makeActor({
2871
+ conn: {
2872
+ caps: new Set<string>(['draft/chathistory', 'server-time', 'message-tags', 'batch']),
2873
+ },
2874
+ messages: store,
2875
+ });
2876
+ // Replace the channel access with a wrapper that lacks getChannel.
2877
+ // The actor holds `channels` by reference, so this swap takes effect.
2878
+ const accessWithoutGetChannel: ActorChannelAccess = {
2879
+ getOrCreateChannel: (name: ChanName) => runtime.getOrCreateChannel(name),
2880
+ };
2881
+ (actor as unknown as { channels: ActorChannelAccess }).channels = accessWithoutGetChannel;
2882
+ // Pre-create the channel so getOrCreateChannel returns it, and seed
2883
+ // one stored message so the reducer has something to replay.
2884
+ runtime.getOrCreateChannel('#foo');
2885
+ store.record({
2886
+ msgid: 'm1',
2887
+ time: 1_000,
2888
+ chan: '#foo',
2889
+ command: 'PRIVMSG',
2890
+ nick: 'bob',
2891
+ user: 'bob',
2892
+ host: 'ex.org',
2893
+ text: 'hello',
2894
+ });
2895
+ runtime.calls.length = 0;
2896
+
2897
+ await actor.receiveTextFrame('CHATHISTORY LATEST #foo * 1\r\n');
2898
+
2899
+ // The reducer should be reached (no 421) and emit a BATCH frame,
2900
+ // proving peekChannel resolved the channel via the getOrCreateChannel
2901
+ // fallback rather than 403/421.
2902
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2903
+ expect(sends.length).toBeGreaterThanOrEqual(1);
2904
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2905
+ for (const line of allLines) {
2906
+ expect(line.text).not.toContain(' 421 ');
2907
+ expect(line.text).not.toContain(' 403 ');
2908
+ }
2909
+ expect(allLines.some((l) => l.text.startsWith('BATCH +'))).toBe(true);
2910
+ });
2911
+ });
2912
+
2913
+ // ============================================================================
2914
+ // WHOIS — channel-list branch (target with joinedChannels)
2915
+ // ============================================================================
2916
+
2917
+ describe('ConnectionActor — WHOIS channel list', () => {
2918
+ it("includes the target's joined channels in the 319 reply", async () => {
2919
+ // Covers the Array.from(target.joinedChannels, ...) branch in
2920
+ // routeWhois: when the target has joinedChannels, each is resolved to
2921
+ // a ChannelState and passed to whoisReducer for the 319 list.
2922
+ const { actor, runtime, conn } = makeActor({});
2923
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2924
+ bobState.nick = 'bob';
2925
+ bobState.user = 'bob';
2926
+ bobState.host = 'example.com';
2927
+ bobState.realname = 'Bob';
2928
+ bobState.registration = 'registered';
2929
+ bobState.joinedChannels.add('#room1');
2930
+ bobState.joinedChannels.add('#room2');
2931
+ runtime.addConnection(conn);
2932
+ runtime.addConnection(bobState);
2933
+ // Materialize the channels so getOrCreateChannel resolves them.
2934
+ await runtime.applyChannelDelta('#room1', {
2935
+ memberships: [{ type: 'add', conn: 'c2', nick: 'bob' }],
2936
+ });
2937
+ await runtime.applyChannelDelta('#room2', {
2938
+ memberships: [{ type: 'add', conn: 'c2', nick: 'bob' }],
2939
+ });
2940
+ runtime.calls.length = 0;
2941
+
2942
+ await actor.receiveTextFrame('WHOIS bob\r\n');
2943
+
2944
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2945
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2946
+ // 319 RPL_WHOISCHANNELS lists at least one of the target's channels.
2947
+ const chanLines = allLines.filter((l) => l.text.includes(' 319 '));
2948
+ expect(chanLines.length).toBeGreaterThan(0);
2949
+ const joined = chanLines.some((l) => l.text.includes('#room1') || l.text.includes('#room2'));
2950
+ expect(joined).toBe(true);
2951
+ });
2952
+
2953
+ it('treats a target whose conn tore down mid-resolve as offline (401 + 318, never 421)', async () => {
2954
+ // Covers the `?? undefined` branch in routeWhois: lookupNick resolved
2955
+ // the nick but getConnection returned null (race). The reducer must
2956
+ // emit the unknown-nick path (401 + 318), not crash.
2957
+ const { actor, runtime, conn } = makeActor({});
2958
+ const bobState = createConnection({ id: 'c2', connectedSince: 0 });
2959
+ bobState.nick = 'bob';
2960
+ bobState.registration = 'registered';
2961
+ runtime.addConnection(conn);
2962
+ runtime.addConnection(bobState);
2963
+ runtime.dropConnection('c2');
2964
+ runtime.calls.length = 0;
2965
+
2966
+ await actor.receiveTextFrame('WHOIS bob\r\n');
2967
+
2968
+ const sends = runtime.calls.filter((c) => c.method === 'send');
2969
+ const allLines = sends.flatMap((s) => s.args[1] as RawLine[]);
2970
+ expect(allLines.some((l) => l.text.includes(' 401 '))).toBe(true);
2971
+ expect(allLines.some((l) => l.text.includes(' 318 '))).toBe(true);
2972
+ for (const line of allLines) {
2973
+ expect(line.text).not.toContain(' 421 ');
2974
+ }
2975
+ });
1888
2976
  });