skillhub/plugins/dsh/test/availability-mutation-fixture.test.ts
dongmucat 6d436d664e
feat(dsh): connect accounts and manage namespace skills and members (#1114)
* feat(dsh): connect registry accounts and manage namespace skills and members

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): use the patched ZIP reader for skill packages

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): bind owned skill actions to the published version ID

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(auth): handle context-prefixed registry requests consistently

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): address account isolation and upload review findings

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): recover changed upload targets and clear private detail caches

Signed-off-by: dongmucat <1127093059@qq.com>

* refactor(dsh): keep registry authentication inside the plugin

Signed-off-by: dongmucat <1127093059@qq.com>

* test(dsh): canonicalize temporary profile roots across platforms

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): clear expired accounts during detail reads

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(auth): include DSH permissions in default personal tokens

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(web): refresh token form analytics manifest

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): organize account dialogs and preserve modal focus

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(ci): pin DSH browser checks to supported Ubuntu

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): unify workbench appearance and source navigation

Signed-off-by: dongmucat <1127093059@qq.com>

* test(dsh): wait for runtime readiness before availability install

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): align namespace pages with the account prototype

Signed-off-by: dongmucat <1127093059@qq.com>

* test(dsh): use monotonic clocks for fixture deadlines

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): expose namespace member management actions

Add a direct member-management entry, explain disabled role and state restrictions, and preserve keyboard focus after membership changes. Exercise add, role change and removal through the real account components.

Signed-off-by: dongmucat <1127093059@qq.com>

* feat(dsh): support drag and drop skill uploads

Use one validated file selection flow for dropping and browsing ZIPs. Show selected file details, protect in-flight uploads and confirmation revisions, and cover keyboard, busy and invalid-file behavior in Chromium.

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): right-align the catalog upload action

Signed-off-by: dongmucat <1127093059@qq.com>

* feat(dsh): persist Windows account credentials securely

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): remove saved accounts reliably on Windows Unicode paths

Signed-off-by: dongmucat <1127093059@qq.com>

* test(organizations): isolate committed portal fixtures

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(auth): preserve token lifecycle state during usage updates

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(skills): enforce caller-aware deletion across CLI endpoints

Signed-off-by: dongmucat <1127093059@qq.com>

* fix(dsh): submit account writes once and refresh registry views

Signed-off-by: dongmucat <1127093059@qq.com>

* chore(db): reserve unpublished token migration after upstream V128

Signed-off-by: dongmucat <1127093059@qq.com>

---------

Signed-off-by: dongmucat <1127093059@qq.com>
2026-11-25 16:07:19 +08:00

251 lines
14 KiB
TypeScript

import { expect, test } from 'bun:test';
import { spawnSync } from 'node:child_process';
import { mkdtemp, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import { setTimeout as delay } from 'node:timers/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
// @ts-expect-error Development-only observer fixture has no TypeScript declarations.
import { waitForAvailabilityRuntime, waitForAvailabilityWinner, withRenamedAvailabilityPath } from './integration/skill-availability-fixture.mjs';
// @ts-expect-error Development-only Node script has no TypeScript declarations.
import { safeAvailabilityCheckpoint } from '../scripts/smoke-native-host.mjs';
function checkpoint(): { scenario: string; checkpoint: string; assertion: string; restoreOsCode?: string } { return { scenario: 'directory-missing', checkpoint: 'mutation', assertion: 'none' }; }
test('availability fixture waits for a ready runtime with a Host identity before the single install', async () => {
const sequence = [null, { state: 'starting' }, { state: 'ready', hostInstanceId: null }, { state: 'ready', hostInstanceId: 'fixture-host' }];
let reads = 0;
await waitForAvailabilityRuntime(() => sequence[reads++], new AbortController().signal, { timeoutMs: 1000, pollMs: 1 });
expect(reads).toBe(4);
});
test('availability fixture fails immediately on a terminal runtime state', async () => {
for (const state of ['failed', 'closing', 'disposed']) {
let reads = 0;
await expect(waitForAvailabilityRuntime(() => { reads++; return { state }; }, new AbortController().signal)).rejects.toMatchObject({ code: 'RUNTIME_NOT_READY' });
expect(reads).toBe(1);
}
});
test('availability fixture bounds startup and observes cancellation without issuing an install', async () => {
await expect(waitForAvailabilityRuntime(() => ({ state: 'starting' }), new AbortController().signal, { timeoutMs: 10, pollMs: 1 })).rejects.toMatchObject({ code: 'RUNTIME_READY_TIMEOUT' });
const abort = new AbortController();
const work = waitForAvailabilityRuntime(() => { abort.abort(); return null; }, abort.signal, { pollMs: 1000 });
await expect(work).rejects.toMatchObject({ name: 'AbortError' });
const readyAbort = new AbortController();
const reason = new Error('fixture cancelled');
await expect(waitForAvailabilityRuntime(() => { readyAbort.abort(reason); return { state: 'ready', hostInstanceId: 'fixture-host' }; }, readyAbort.signal)).rejects.toBe(reason);
});
test('runtime startup diagnostics expose only a whitelisted state', () => {
const value = { scenario: 'user-install', checkpoint: 'runtime-ready', assertion: 'none', runtimeState: 'starting', hostInstanceId: '/private/sentinel' };
expect(safeAvailabilityCheckpoint(value)).toEqual({ scenario: 'user-install', checkpoint: 'runtime-ready', assertion: 'none', runtimeState: 'starting' });
expect(safeAvailabilityCheckpoint({ ...value, runtimeState: '/private/sentinel' })).toEqual({ scenario: 'user-install', checkpoint: 'runtime-ready', assertion: 'none' });
});
test('failed initial rename never restores an absent backup or replaces the original error', async () => {
const state = checkpoint();
const original = Object.assign(new Error('/private/locked/path'), { code: 'EPERM' });
const calls: string[][] = [];
let inspected = false;
await expect(withRenamedAvailabilityPath(state, '/source', '/backup', async () => { inspected = true; }, async (...paths: string[]) => {
calls.push(paths); throw original;
})).rejects.toBe(original);
expect(calls).toEqual([['/source', '/backup']]);
expect(inspected).toBe(false);
expect(state.checkpoint).toBe('mutation-apply');
});
test('failed restoration preserves the primary inspection error and its checkpoint', async () => {
const state = checkpoint();
const original = Object.assign(new Error('/private/assertion'), { code: 'ERR_ASSERTION' });
let calls = 0;
await expect(withRenamedAvailabilityPath(state, '/source', '/backup', async () => {
Object.assign(state, { checkpoint: 'assertion', assertion: 'integrity' });
throw original;
}, async () => { if (++calls === 2) throw Object.assign(new Error('/private/restore'), { code: 'EBUSY' }); })).rejects.toBe(original);
expect(calls).toBe(2);
expect(state).toEqual({ scenario: 'directory-missing', checkpoint: 'assertion', assertion: 'integrity', restoreOsCode: 'EBUSY' });
});
test('restoration failure after successful inspection identifies the restore phase', async () => {
const state = checkpoint();
const original = Object.assign(new Error('/private/restore'), { code: 'EACCES' });
let calls = 0;
await expect(withRenamedAvailabilityPath(state, '/source', '/backup', async () => {}, async () => {
if (++calls === 2) throw original;
})).rejects.toBe(original);
expect(state.checkpoint).toBe('mutation-restore');
});
test('real directory rename and restore preserve fixture files without skipping inspection', async () => {
const root = await mkdtemp(join(tmpdir(), 'skillhub-availability-mutation-'));
const source = join(root, 'skill');
const backup = join(root, 'backup');
await mkdir(source);
await writeFile(join(source, 'SKILL.md'), 'original');
try {
let inspected = false;
await withRenamedAvailabilityPath(checkpoint(), source, backup, async () => {
inspected = true;
expect(await readFile(join(backup, 'SKILL.md'), 'utf8')).toBe('original');
await expect(readFile(join(source, 'SKILL.md'))).rejects.toHaveProperty('code', 'ENOENT');
}, rename);
expect(inspected).toBe(true);
expect(await readFile(join(source, 'SKILL.md'), 'utf8')).toBe('original');
} finally { await rm(root, { recursive: true, force: true }); }
});
test('mutation summary keeps only fixed OS codes and excludes paths or arbitrary error strings', () => {
const state = { ...checkpoint(), checkpoint: 'mutation-apply', osCode: 'EPERM', restoreOsCode: 'ENOENT', message: '/private/token' };
expect(safeAvailabilityCheckpoint(state)).toEqual({ scenario: 'directory-missing', checkpoint: 'mutation-apply', assertion: 'none', osCode: 'EPERM', restoreOsCode: 'ENOENT' });
for (const value of ['/private/token', 'secret', 'constructor', {}, undefined]) {
const safe = safeAvailabilityCheckpoint({ ...state, osCode: value, restoreOsCode: value });
expect(safe.osCode).toBeUndefined();
expect(safe.restoreOsCode).toBeUndefined();
expect(JSON.stringify(safe)).not.toMatch(/private|token|secret/);
}
});
const discoveredWinner = (directory: string) => ({ snapshot: { complete: true, skills: [{ name: 'document-helper' }] },
winner: { name: 'document-helper', path: join(directory, 'SKILL.md'), resourceBase: { kind: 'directory', path: directory } } });
test('availability fixture waits through stale same-name user winner and incomplete snapshots for the exact project', async () => {
const project = '/project/.dsh/skills/document-helper';
const current = discoveredWinner(project);
const sequence = [discoveredWinner('/home/skills/document-helper'), { ...current, snapshot: { ...current.snapshot, complete: false } }, current];
let reads = 0;
await waitForAvailabilityWinner(async () => sequence[reads++], project, new AbortController().signal, { timeoutMs: 1000, pollMs: 1 });
expect(reads).toBe(3);
});
test('availability fixture never accepts a wrong winner and fails at its bounded deadline', async () => {
let reads = 0;
await expect(waitForAvailabilityWinner(async () => { reads++; return discoveredWinner('/other'); }, '/selected',
new AbortController().signal, { timeoutMs: 20, pollMs: 1 })).rejects.toThrow('before the deadline');
expect(reads).toBeGreaterThan(0);
});
test('availability discovery wait preserves service errors and cancellation without retrying', async () => {
const failure = new Error('Service unavailable');
let reads = 0;
await expect(waitForAvailabilityWinner(async () => { reads++; throw failure; }, '/selected',
new AbortController().signal, { timeoutMs: 100, pollMs: 1 })).rejects.toBe(failure);
expect(reads).toBe(1);
const controller = new AbortController();
await expect(waitForAvailabilityWinner(async () => { controller.abort(failure); return discoveredWinner('/selected'); },
'/selected', controller.signal)).rejects.toBe(failure);
});
test('availability fixture observes a delayed watcher refresh instead of asserting immediately after commit', async () => {
const started = performance.now();
let reads = 0;
await waitForAvailabilityWinner(async () => {
reads++;
return discoveredWinner(performance.now() - started >= 200 ? '/selected' : '/previous');
}, '/selected', new AbortController().signal, { timeoutMs: 2000, pollMs: 20 });
expect(performance.now() - started).toBeGreaterThanOrEqual(200);
expect(reads).toBeGreaterThan(1);
});
test('availability discovery rejects mismatched name, path or resource identity despite a complete catalog', async () => {
const valid = discoveredWinner('/selected');
for (const winner of [{ ...valid.winner, name: 'another' }, { ...valid.winner, path: '/other/SKILL.md' },
{ ...valid.winner, resourceBase: { kind: 'directory', path: '/other' } }]) {
await expect(waitForAvailabilityWinner(async () => ({ ...valid, winner }), '/selected',
new AbortController().signal, { timeoutMs: 10, pollMs: 1 })).rejects.toHaveProperty('code', 'DISCOVERY_TIMEOUT');
}
});
test('availability hard deadline rejects a late valid read and aborts its bounded signal', async () => {
let observed: AbortSignal | undefined;
await expect(waitForAvailabilityWinner(async (signal: AbortSignal) => {
observed = signal;
await delay(50);
return discoveredWinner('/selected');
}, '/selected', new AbortController().signal, { timeoutMs: 10 })).rejects.toHaveProperty('code', 'DISCOVERY_TIMEOUT');
expect(observed?.aborted).toBe(true);
await delay(60); // Let the ignored late result settle; Bun also detects unhandled rejections.
});
test('availability hard deadline settles even when a reader never settles', async () => {
let observed: AbortSignal | undefined;
await expect(waitForAvailabilityWinner((signal: AbortSignal) => {
observed = signal;
return new Promise(() => {});
}, '/selected', new AbortController().signal, { timeoutMs: 10 })).rejects.toHaveProperty('code', 'DISCOVERY_TIMEOUT');
expect(observed?.aborted).toBe(true);
});
test('availability external cancellation wins before deadline and preserves its exact reason during a pending read', async () => {
const controller = new AbortController();
const reason = new Error('External cancellation');
let observed: AbortSignal | undefined;
const work = waitForAvailabilityWinner((signal: AbortSignal) => {
observed = signal;
controller.abort(reason);
return new Promise(() => {});
}, '/selected', controller.signal, { timeoutMs: 100 });
await expect(work).rejects.toBe(reason);
expect(observed?.reason).toBe(reason);
});
test('availability deadline remains the cause if cancellation and late rejection follow it', async () => {
const controller = new AbortController();
let rejectRead!: (reason: Error) => void;
const work = waitForAvailabilityWinner(() => new Promise((_resolve, reject) => { rejectRead = reject; }),
'/selected', controller.signal, { timeoutMs: 10 });
await expect(work).rejects.toHaveProperty('code', 'DISCOVERY_TIMEOUT');
controller.abort(new Error('Late cancellation'));
rejectRead(new Error('Late reader failure'));
await delay(0);
});
test('availability success clears the deadline and detaches external cancellation', async () => {
const controller = new AbortController();
let observed: AbortSignal | undefined;
await waitForAvailabilityWinner(async (signal: AbortSignal) => { observed = signal; return discoveredWinner('/selected'); },
'/selected', controller.signal, { timeoutMs: 20 });
controller.abort(new Error('After success'));
await delay(30);
expect(observed?.aborted).toBe(false);
});
test('availability rejects a valid read that crosses the deadline before timers can run', async () => {
await expect(waitForAvailabilityWinner(async () => {
const until = performance.now() + 20;
while (performance.now() < until) { /* Simulate a reader monopolizing the event loop. */ }
return discoveredWinner('/selected');
}, '/selected', new AbortController().signal, { timeoutMs: 5 })).rejects.toHaveProperty('code', 'DISCOVERY_TIMEOUT');
});
test('fixture elapsed waits survive wall-clock changes in an isolated process', () => {
const module = new URL('./integration/skill-availability-fixture.mjs', import.meta.url).href;
const script = `
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { waitForAvailabilityRuntime, waitForAvailabilityWinner } from ${JSON.stringify(module)};
const original = Date.now;
let offset = 0;
Date.now = () => original() + offset;
let reads = 0;
await waitForAvailabilityRuntime(() => {
offset += 60000;
return ++reads < 3 ? { state: 'starting' } : { state: 'ready', hostInstanceId: 'fixture' };
}, new AbortController().signal, { timeoutMs: 2000, pollMs: 1 });
assert.equal(reads, 3);
reads = 0;
offset = 0;
await waitForAvailabilityWinner(async () => {
offset += reads === 0 ? -60000 : 180000;
const path = ++reads < 3 ? '/previous' : '/selected';
return { snapshot: { complete: true, skills: [{ name: 'document-helper' }] }, winner: { name: 'document-helper', path: join(path, 'SKILL.md'), resourceBase: { kind: 'directory', path } } };
}, '/selected', new AbortController().signal, { timeoutMs: 2000, pollMs: 1 });
assert.equal(reads, 3);
`;
const result = spawnSync(process.execPath, ['--eval', script], { encoding: 'utf8', timeout: 10000 });
expect(result.status, result.stderr).toBe(0);
});