feat(skins): add persistent voice workspace API and align KAAK

This commit is contained in:
illusia committed 2026-10-24 13:30:50 +08:00
1 parent 0af3236c30
commit 452e6f09ca
24 files changed
+451 -21

No files matched your search

+16
View File
@@ -106,3 +106,19 @@ test("channel selection falls back to the normal switch when no local password e
assert.equal(calls[0][0], "42");
assert.equal(calls[0][1], "");
});
test("saved-password lookup stays pending without duplicate connects and cancellation retires it", async t => {
const { connection, connecting, connectCalls } = mount(t);
connection.doConnect();
connection.doConnect();
// IndexedDB lookup is async; an idle transport at this point is not a failure.
assert.equal(connecting.value, false);
assert.equal(connectCalls.length, 0);
await nextTurn();
assert.equal(connectCalls.length, 1);
connection.doConnect();
connection.doDisconnect(false);
await nextTurn();
assert.equal(connectCalls.length, 1);
});
+37
View File
@@ -0,0 +1,37 @@
import assert from "node:assert/strict";
import test from "node:test";
import { zipSync, strToU8 } from "fflate";
import { parseSkinWorkspacePolicy } from "../../src/shared/skin-workspace.js";
import { parseSkinPluginDocument, parseSkinPluginRuntimeAction } from "../../src/shared/skin-plugin.js";
import { importSkinPack } from "../src/services/skin-pack.js";
import { validateSkinArchive } from "../../src/admin/skin-registry.js";
const base = {schemaVersion:3,packageType:'open-skin',id:'sample.workspace',name:'Workspace',version:'1.0.0',author:'Fixture',license:'MIT',minAppVersion:'0.2.8',entry:'skin.css'};
const archive = (workspace:unknown,schemaVersion=3) => zipSync({'manifest.json':strToU8(JSON.stringify({...base,schemaVersion,...(schemaVersion===1?{packageType:undefined}:{}),workspace})), 'skin.css':strToU8(':root { --skin-sample-color: #123456; }')});
test('the same workspace policy is preserved by browser and server importers',async()=>{
const bytes=archive({apiVersion:1,mode:'persistent'});
const browser=await importSkinPack(new File([bytes.buffer as ArrayBuffer],'workspace.wskin'));
const server=validateSkinArchive(Buffer.from(bytes));
assert.deepEqual(browser.workspace,{apiVersion:1,mode:'persistent'});
assert.deepEqual(server.manifest.workspace,browser.workspace);
});
test('unknown workspace versions, extra fields, executable values and legacy manifests are rejected by both importers',async()=>{
for(const value of [null,true,'persistent',{apiVersion:2,mode:'persistent'},{apiVersion:1,mode:'automatic'},{apiVersion:1,mode:'persistent',autoConnect:true}]){
const bytes=archive(value);
await assert.rejects(importSkinPack(new File([bytes.buffer as ArrayBuffer],'bad.wskin')));
assert.throws(()=>validateSkinArchive(Buffer.from(bytes)));
}
const legacy=archive({apiVersion:1,mode:'persistent'},1);
await assert.rejects(importSkinPack(new File([legacy.buffer as ArrayBuffer],'legacy.wskin')));
assert.throws(()=>validateSkinArchive(Buffer.from(legacy)));
assert.equal(parseSkinWorkspacePolicy(undefined,1),undefined);
});
test('connecting uses an explicit dedicated permission and accepts no target or credential arguments',()=>{
const component={id:'connect-ui',name:'Connection',accessibleName:'Connection',page:'voice',permissions:['session.status.read','voice.connect'],actions:{connect:{type:'voice.connect',args:{}}},root:{widget:'voice.connection-status'}};
assert.doesNotThrow(()=>parseSkinPluginDocument({schemaVersion:3,components:[component]}));
assert.throws(()=>parseSkinPluginDocument({schemaVersion:3,components:[{...component,permissions:['session.status.read']}]}));
const action = parseSkinPluginRuntimeAction({type:'voice.connect',args:{}},['voice.connect']);
assert.equal(action.type, 'voice.connect');
assert.deepEqual(Object.keys(action.args), []);
assert.throws(()=>parseSkinPluginRuntimeAction({type:'voice.connect',args:{target:'127.0.0.1'}},['voice.connect']));
assert.throws(()=>parseSkinPluginRuntimeAction({type:'voice.connect',args:{}},[]));
});
+78
View File
@@ -0,0 +1,78 @@
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, useVoiceWorkspace;
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" });
({ useVoiceWorkspace } = await vite.ssrLoadModule("/src/composables/useVoiceWorkspace.ts"));
});
after(async () => { await vite?.close(); });
function deferred() { let resolve; const promise = new Promise(done => { resolve = done; }); return {promise, resolve}; }
function mount(t, save = async () => undefined) {
const scope = effectScope(), enabled = ref(true), connected = ref(false), writes = [];
const model = scope.run(() => useVoiceWorkspace({ enabled, connected, save: (value, signal) => { writes.push({value,signal}); return save(value,signal); } }));
t.after(() => scope.stop());
return { ...model, enabled, connected, writes, scope };
}
const snapshot = (preferences = {}, recent = []) => ({ preferences: {schemaVersion: 1, ...preferences}, recent });
test("a first-time or failed connection retains onboarding, while success persists the workspace", async t => {
const model = mount(t); await model.restore(async () => snapshot());
assert.equal(model.active.value, false);
model.connected.value = true; model.connected.value = false;
assert.equal(model.active.value, true);
assert.deepEqual(model.writes[0].value, {schemaVersion: 1, hasConnected: true});
});
test("returning users restore the workspace without starting voice or requiring saved targets", async t => {
const model = mount(t); await model.restore(async () => snapshot({hasConnected: true}));
assert.equal(model.active.value, true); assert.equal(model.connected.value, false);
assert.equal(model.writes.length, 0);
});
test("recent success history migrates even when its displayed entry is also a favorite", async t => {
const model = mount(t); await model.restore(async () => snapshot({}, [{id:'opaque-test',lastConnectedAt:1}]));
assert.equal(model.active.value, true); assert.equal(model.writes.length, 1);
});
test("a saved nickname is not evidence of a successful connection", async t => {
const model = mount(t); await model.restore(async () => snapshot({lastNickname:'Failed attempt'}));
assert.equal(model.active.value, false);
});
test("the selected package's policy controls the flow without hard-coded skin IDs", async t => {
const model = mount(t); await model.restore(async () => snapshot({hasConnected:true}));
model.enabled.value = false; assert.equal(model.active.value, false);
model.enabled.value = true; assert.equal(model.active.value, true);
});
test("late preference restoration cannot undo a successful live connection", async t => {
const model = mount(t), load = deferred(); const pending = model.restore(() => load.promise);
model.connected.value = true; model.connected.value = false;
load.resolve(snapshot()); await pending;
assert.equal(model.active.value, true);
});
test("clearing local data retires pending restoration and aborts outstanding preference writes", async t => {
const model = mount(t), load = deferred(); const pending = model.restore(() => load.promise);
model.connected.value = true; const oldSignal = model.writes[0].signal;
model.reset(); assert.equal(oldSignal.aborted, true);
load.resolve(snapshot({hasConnected:true})); assert.equal(await pending, undefined);
assert.equal(model.active.value, false);
});
test("disposal discards late restoration and prevents late history writes", async t => {
const model = mount(t), load = deferred(); const pending = model.restore(() => load.promise);
model.scope.stop(); load.resolve(snapshot({}, [{id:'late',lastConnectedAt:1}]));
assert.equal(await pending, undefined); assert.equal(model.writes.length,0);
});
test("unavailable optional browser storage does not break a successful live connection", async t => {
const model = mount(t,async () => {throw new Error('blocked storage');});
assert.equal(await model.restore(async () => {throw new Error('unavailable');}),undefined);
model.connected.value = true; await Promise.resolve();
assert.equal(model.active.value,true);
});
test("retired restoration cannot apply target or language preferences to a replacement page", async t => {
const model = mount(t), load = deferred(), applied = [];
const pending = model.restore(() => load.promise, value => applied.push(value));
model.reset();
load.resolve(snapshot({hasConnected:true,lastNickname:'Retired'}));
await pending;
assert.deepEqual(applied, []);
});