fix: scope session state and confirm chat destinations

This commit is contained in:
illusia committed 2026-10-01 15:46:52 +08:00
1 parent c16d1d4cfb
commit db08fbda6b
18 files changed
+969 -206

No files matched your search

@@ -223,6 +223,20 @@ test("directory snapshots during reconnect backoff cannot leak members into the
assert.equal(entry.members.has(9), false);
});
test("commands arriving during recovery receive a correlated rejection", { timeout: 5_000 }, async t => {
const f = await fixture(t);
const rejected = new Promise<{ requestId?: string }>(resolve => {
f.socket.on("message", (data, binary) => {
if (binary) return;
const message = JSON.parse(data.toString());
if (message.type === "error" && message.error?.code === "SESSION_NOT_READY") resolve(message);
});
});
f.sdk.emit("disconnected");
f.socket.send(JSON.stringify({ type: "sendServerMessage", requestId: "chat-in-flight", payload: { message: "Hello" } }));
assert.equal((await rejected).requestId, "chat-in-flight");
});
for (const order of ["kick-first", "disconnect-first"] as const) {
test(`a kick remains terminal when notifications arrive ${order}`, { timeout: 5_000 }, async t => {
const f = await fixture(t);
+3 -3
View File
@@ -477,7 +477,7 @@ export class VoiceBridge {
return;
}
if (!tsReady || session.state !== "connected") {
sendProtocolError(sendJson, "SESSION_NOT_READY", "TeamSpeak 会话尚未就绪");
sendProtocolError(sendJson, "SESSION_NOT_READY", "TeamSpeak 会话尚未就绪", command.requestId);
return;
}
void handleCommand(entry!, command, sendJson);
@@ -777,6 +777,6 @@ function normalizeWebRtcHost(value: string | undefined): string | undefined {
}
}
function sendProtocolError(sendJson: (message: ServerMessage) => void, code: string, message: string): void {
sendJson({ type: "error", error: { code, message, recoverable: false } });
function sendProtocolError(sendJson: (message: ServerMessage) => void, code: string, message: string, requestId?: string): void {
sendJson({ type: "error", ...(requestId ? { requestId } : {}), error: { code, message, recoverable: false } });
}
+26 -2
View File
@@ -8,6 +8,7 @@ import { handleCommand, type VoiceCommandContext } from "./voice-commands.js";
function fixture() {
const calls: Array<[string, ...unknown[]]> = [];
const messages: ServerMessage[] = [];
const members = new Map<number, { uid?: string }>([[1, {}], [2, {}]]);
const context: VoiceCommandContext = {
tsClient: {
execCommandWithResponse: async request => { calls.push(["exec", request]); return []; },
@@ -20,7 +21,7 @@ function fixture() {
setInputMuted: async (...args) => { calls.push(["mute", ...args]); },
},
channelTree: [{ id: "18446744073709551615", parentID: "0", name: "Large ID" }],
members: new Map([[1, {}], [2, {}]]),
members,
whisperTargetIds: new Set(), whisperActive: false,
lastAudioStatsProbeAt: 0, lastLatencyProbeAt: 0,
audio: createAudioFlowStats(), webrtc: null,
@@ -34,7 +35,7 @@ function fixture() {
messages.push(message);
}, () => now);
}
return { context, calls, messages, run };
return { context, members, calls, messages, run };
}
test("moving a visible member preserves bigint channel IDs and request correlation", async () => {
@@ -44,6 +45,29 @@ test("moving a visible member preserves bigint channel IDs and request correlati
assert.deepEqual(f.messages, [{ type: "commandCompleted", requestId: "move-1" }]);
});
test("private text rejects an ID now owned by a different UID and confirms the intended recipient", async () => {
const f = fixture();
f.members.set(2, { uid: "new-user" });
await f.run({ type: "sendPrivateMessage", requestId: "old", payload: { clientId: 2, clientUid: "old-user", message: "Private" } });
assert.deepEqual(f.calls, []);
assert.equal(f.messages[0]?.type, "error");
assert.equal(f.messages[0]?.requestId, "old");
await f.run({ type: "sendPrivateMessage", requestId: "new", payload: { clientId: 2, clientUid: "new-user", message: "Private" } });
assert.deepEqual(f.calls, [["text", "private", "Private", 2n]]);
assert.deepEqual(f.messages.at(-1), { type: "commandCompleted", requestId: "new" });
});
test("channel text cannot silently go to a different channel after a failed or racing move", async () => {
const f = fixture();
await f.run({ type: "sendTextMessage", requestId: "wrong", payload: { channelId: "6", message: "Message" } });
assert.deepEqual(f.calls, []);
assert.equal(f.messages[0]?.type, "error");
await f.run({ type: "sendTextMessage", requestId: "right", payload: { channelId: "5", message: "Message" } });
assert.deepEqual(f.calls, [["text", "channel", "Message", 5n]]);
await f.run({ type: "sendTextMessage", payload: { message: "Legacy" } });
assert.deepEqual(f.calls.at(-1), ["text", "channel", "Legacy", 5n]);
});
test("invalid move targets are rejected before invoking the SDK", async () => {
const f = fixture();
for (const [clientId, channelId, code] of [[1, "18446744073709551615", "CANNOT_MOVE_SELF"], [3, "18446744073709551615", "CLIENT_NOT_FOUND"], [2, "9", "CHANNEL_NOT_FOUND"]] as const) {
+9 -3
View File
@@ -11,7 +11,7 @@ import { teamSpeakServerErrorCode } from "../errors.js";
export interface VoiceCommandContext extends AudioStatsSource {
tsClient: Pick<TSClient, "execCommandWithResponse" | "switchChannel" | "getClientId" | "getChannelId" | "moveClient" | "sendTextMessage" | "poke" | "setAway" | "setInputMuted">;
channelTree: ChannelInfo[];
members: ReadonlyMap<number, unknown>;
members: ReadonlyMap<number, { uid?: string }>;
whisperTargetIds: Set<number>;
whisperActive: boolean;
lastLatencyProbeAt: number;
@@ -103,14 +103,20 @@ export async function handleCommand(
// not prompt for or depend on the target channel's join password.
await entry.tsClient.moveClient(clientId, BigInt(channelId));
} else if (command.type === "sendTextMessage") {
const channelId = entry.tsClient.getChannelId();
if (command.payload.channelId !== undefined && BigInt(command.payload.channelId) !== channelId) {
sendJson({ type: "error", requestId: command.requestId, error: { code: "CHANNEL_CHANGED", message: "所在频道已变化,请确认频道后重试", recoverable: false } });
return;
}
const message = command.payload.message.trim();
if (message) await entry.tsClient.sendTextMessage("channel", message, entry.tsClient.getChannelId());
if (message) await entry.tsClient.sendTextMessage("channel", message, channelId);
} else if (command.type === "sendServerMessage") {
const message = command.payload.message.trim();
if (message) await entry.tsClient.sendTextMessage("server", message);
} else if (command.type === "sendPrivateMessage") {
const clientId = command.payload.clientId;
if (!entry.members.has(clientId)) {
const recipient = entry.members.get(clientId);
if (!recipient || (command.payload.clientUid !== undefined && recipient.uid !== command.payload.clientUid)) {
sendJson({ type: "error", requestId: command.requestId, error: { code: "CLIENT_NOT_FOUND", message: "成员已离线", recoverable: false } });
return;
}