refactor: extract settings dialogs and guard pending feedback

This commit is contained in:
illusia committed 2026-10-01 18:45:56 +08:00
1 parent 83b2430d18
commit d2caaa8f3c
13 files changed
+497 -155

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
import { effectScope, ref } from "vue";
let vite, useWebClientAudioControls;
before(async () => {
vite = await createServer({ configFile: false, root: fileURLToPath(new URL("../", import.meta.url)),
server: { middlewareMode: true, hmr: false, ws: false, watch: null },
optimizeDeps: { noDiscovery: true, include: [] }, appType: "custom" });
({ useWebClientAudioControls } = await vite.ssrLoadModule("/src/composables/useWebClientAudioControls.ts"));
});
after(async () => { await vite?.close(); });
function deferred() {
let resolve, reject;
const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
return { promise, resolve, reject };
}
function mount(t, extra = {}) {
const scope = effectScope(), settingsOpen = ref(false), stops = [];
const controls = scope.run(() => useWebClientAudioControls({
settingsOpen, microphoneMuted: ref(false), inputVolume: ref(1), voxThreshold: ref(.01),
notificationVolume: ref(1), micLevel: ref(0), microphoneTestActive: ref(false),
accompanimentActive: ref(false), accompanimentErrorCode: ref(""), whisperTargetIds: new Set(),
prepareInputDevices: async () => {}, stopMicrophoneTest: () => stops.push("stop"),
localizedMessage: value => value, t: key => key, ...extra,
}));
t.after(() => scope.stop());
settingsOpen.value = true;
return { controls, settingsOpen, scope, stops };
}
const deviceEvent = { target: { value: "device" } };
test("a previous opening cannot publish its preparation error after reopening", async t => {
const first = deferred(), second = deferred(); let calls = 0;
const { controls, settingsOpen } = mount(t, { prepareInputDevices: () => (++calls === 1 ? first.promise : second.promise) });
await Promise.resolve(); settingsOpen.value = false; await Promise.resolve(); settingsOpen.value = true; await Promise.resolve();
first.reject(new DOMException("old", "NotAllowedError")); await Promise.resolve();
assert.equal(controls.settingsError.value, "");
second.reject(new DOMException("current", "NotFoundError")); await Promise.resolve();
assert.match(controls.settingsError.value, /未找到可用的麦克风/);
});
test("a completed newer device selection retires older errors", async t => {
const old = deferred();
const { controls } = mount(t, { setOutputDevice: () => old.promise, setInputDevice: async () => {} });
await Promise.resolve();
const pending = controls.onOutputDeviceChange(deviceEvent);
await controls.onInputDeviceChange(deviceEvent);
old.reject(new Error("old output")); await pending;
assert.equal(controls.settingsError.value, "");
});
test("an earlier selection cannot replace the current selection's error", async t => {
const old = deferred();
const { controls } = mount(t, { setInputDevice: () => old.promise, setOutputDevice: async () => { throw new Error("current output"); } });
await Promise.resolve();
const pending = controls.onInputDeviceChange(deviceEvent);
await controls.onOutputDeviceChange(deviceEvent);
old.reject(new DOMException("old input", "NotAllowedError")); await pending;
assert.equal(controls.settingsError.value, "current output");
});
test("closing immediately cancels the test UI and ignores its late rejection", async t => {
const testStart = deferred();
const { controls, settingsOpen, stops } = mount(t, { startMicrophoneTest: () => testStart.promise });
await Promise.resolve();
const pending = controls.toggleMicTest(); settingsOpen.value = false;
assert.equal(stops.length, 1);
testStart.reject(new Error("old recording")); await pending;
assert.equal(controls.settingsError.value, "");
});
test("unmount stops recording and retires device preparation", async t => {
const preparation = deferred();
const { controls, scope, stops } = mount(t, { prepareInputDevices: () => preparation.promise });
await Promise.resolve(); scope.stop();
assert.equal(stops.length, 1);
preparation.reject(new DOMException("old", "NotAllowedError")); await Promise.resolve();
assert.equal(controls.settingsError.value, "");
});
+59
View File
@@ -0,0 +1,59 @@
import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
import { effectScope, nextTick, reactive, ref } from "vue";
let vite, useWebClientConnection;
before(async () => {
vite = await createServer({ configFile: false, root: fileURLToPath(new URL("../", import.meta.url)),
server: { middlewareMode: true, hmr: false, ws: false, watch: null },
optimizeDeps: { noDiscovery: true, include: [] }, appType: "custom" });
({ useWebClientConnection } = await vite.ssrLoadModule("/src/composables/useWebClientConnection.ts"));
});
after(async () => { await vite?.close(); });
function mount(t) {
const scope = effectScope(), errors = ref(""), message = ref(""), switched = ref(""), calls = [];
const dialog = reactive({ open: true, channelId: "2", password: "draft", error: "", submitting: false });
const connection = scope.run(() => useWebClientConnection({
initialized: ref(true), accessMode: ref("fixed"), isConnecting: ref(false), errorCode: errors,
errorMessage: message, channelSwitchedChannelId: switched,
nickname: ref("Preview"), channelName: ref("Room"), serverHost: ref("fixture.invalid"), serverPort: ref("9987"),
serverPassword: ref(""), rememberIdentity: ref(false), identityMaterial: ref(""), accelerationRelayId: ref(""),
inviteToken: "", selectedChannelId: ref("2"), channels: ref([]), clientId: ref(1),
channelPasswordDialog: dialog, serverPasswordDialog: reactive({ open: false, password: "", errorCode: "" }),
chatTab: ref("channel"), switchChannel: (...args) => calls.push(args),
clearError: () => { errors.value = ""; message.value = ""; }, t: key => key,
}));
t.after(() => scope.stop());
return { dialog, connection, errors, message, switched, calls };
}
test("a waiting channel password request cannot be submitted twice", t => {
const { connection, calls, dialog } = mount(t);
connection.submitChannelPassword(); connection.submitChannelPassword();
assert.equal(dialog.submitting, true);
assert.deepEqual(calls, [["2", "draft"]]);
});
test("an ordinary channel error preserves the password and allows retry", async t => {
const { connection, dialog, errors, message, calls } = mount(t);
connection.submitChannelPassword();
message.value = "Channel operation timed out"; errors.value = "COMMAND_TIMEOUT";
await nextTick();
assert.equal(dialog.open, true);
assert.equal(dialog.submitting, false);
assert.equal(dialog.password, "draft");
assert.equal(dialog.error, "Channel operation timed out");
assert.equal(errors.value, "");
connection.submitChannelPassword();
assert.equal(calls.length, 2);
assert.equal(dialog.error, "");
});
test("a successful channel switch closes and clears the password dialog", async t => {
const { connection, dialog, switched } = mount(t);
connection.submitChannelPassword(); switched.value = "2";
await nextTick();
assert.deepEqual({ ...dialog }, { open: false, channelId: "", password: "", error: "", submitting: false });
});