skillhub/plugins/dsh/test/account-credentials.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

127 lines
7.4 KiB
TypeScript

import { expect, test } from 'bun:test';
import { createHash } from 'node:crypto';
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { AccountCredentials } from '../src/host/account-credentials.ts';
const source = 'https://registry.example/team';
const other = 'https://registry.example/another-prefix';
const fixture = { token: 'fixture-not-a-real-api-token', identity: { handle: '用户-fixture', displayName: '团队成员 🐈' } };
const file = (profile: string, registry = source) => join(profile, 'skillhub-accounts', createHash('sha256').update(registry).digest('hex') + '.json');
test('accounts survive a new store, isolate sources, and logout remains forgotten', () => {
const profile = mkdtempSync(join(tmpdir(), 'skillhub-凭据-'));
try {
new AccountCredentials(profile).set(source, fixture);
const restored = new AccountCredentials(profile);
expect(restored.get(source)).toEqual(fixture);
expect(restored.get(other)).toBeUndefined();
writeFileSync(file(profile), 'broken');
expect(restored.get(source)).toEqual(fixture);
restored.set(source);
expect(new AccountCredentials(profile).get(source)).toBeUndefined();
expect(readdirSync(join(profile, 'skillhub-accounts'))).toEqual([]);
} finally { rmSync(profile, { recursive: true, force: true }); }
}, 30000);
test('a failed replacement preserves the previously committed account and no temporary file', () => {
const profile = mkdtempSync(join(tmpdir(), 'skillhub-replace-'));
try {
const store = new AccountCredentials(profile);
store.set(source, fixture);
const original = readFileSync(file(profile));
expect(() => store.set(source, { ...fixture, token: 'x'.repeat(9000) })).toThrow();
expect(readFileSync(file(profile))).toEqual(original);
expect(store.get(source)).toEqual(fixture);
expect(new AccountCredentials(profile).get(source)).toEqual(fixture);
expect(readdirSync(join(profile, 'skillhub-accounts'))).toHaveLength(1);
} finally { rmSync(profile, { recursive: true, force: true }); }
}, 30000);
test('failed deletion blocks reloading in this process and can be retried', () => {
const profile = mkdtempSync(join(tmpdir(), 'skillhub-clear-'));
try {
const store = new AccountCredentials(profile);
store.set(source, fixture);
const original = readFileSync(file(profile));
rmSync(file(profile)); mkdirSync(file(profile));
writeFileSync(join(file(profile), 'block-unlink'), 'fixture');
expect(() => store.set(source)).toThrow();
expect(store.get(source)).toBeUndefined();
expect(store.hasRemovalFailure(source)).toBe(true);
expect(store.hasRemovalFailure(other)).toBe(false);
rmSync(file(profile), { recursive: true });
writeFileSync(file(profile), original, { mode: 0o600 });
expect(store.get(source)).toBeUndefined();
store.set(source);
expect(store.hasRemovalFailure(source)).toBe(false);
expect(new AccountCredentials(profile).get(source)).toBeUndefined();
} finally { rmSync(profile, { recursive: true, force: true }); }
}, 30000);
test('malformed and oversized records cannot restore an account', () => {
const profile = mkdtempSync(join(tmpdir(), 'skillhub-invalid-'));
try {
mkdirSync(join(profile, 'skillhub-accounts'), { mode: 0o700 });
for (const content of ['not JSON', 'null', '{}', JSON.stringify({ version: 99 }), 'x'.repeat(16385)]) {
writeFileSync(file(profile), content, { mode: 0o600 });
expect(new AccountCredentials(profile).get(source)).toBeUndefined();
}
} finally { rmSync(profile, { recursive: true, force: true }); }
});
test.skipIf(process.platform === 'win32')('POSIX permissions and symbolic links cannot expose another credential', () => {
const profile = mkdtempSync(join(tmpdir(), 'skillhub-private-'));
try {
const store = new AccountCredentials(profile);
store.set(source, fixture);
chmodSync(file(profile), 0o644);
expect(new AccountCredentials(profile).get(source)).toBeUndefined();
chmodSync(file(profile), 0o600);
const original = file(profile) + '.original';
copyFileSync(file(profile), original);
rmSync(file(profile)); symlinkSync(original, file(profile));
expect(new AccountCredentials(profile).get(source)).toBeUndefined();
} finally { rmSync(profile, { recursive: true, force: true }); }
});
test.skipIf(process.platform !== 'win32')('real Windows Node processes restore DPAPI, reject copied ciphertext, and forget logout', async () => {
const root = mkdtempSync(join(tmpdir(), 'skillhub-Windows-凭据-'));
const profile = join(root, '中文-profile');
const second = join(root, '另一-profile');
const module = join(root, 'store.mjs');
try {
const build = await Bun.build({ entrypoints: [join(import.meta.dir, '../src/host/account-credentials.ts')], target: 'node', format: 'esm' });
expect(build.success).toBe(true);
writeFileSync(module, await build.outputs[0]!.text());
const child = `import { pathToFileURL } from 'node:url';process.stdin.setEncoding('utf8');let input='';for await(const chunk of process.stdin)input+=chunk;const r=JSON.parse(input);const {AccountCredentials}=await import(pathToFileURL(r.module).href);const store=new AccountCredentials(r.profile);try{if(r.action==='save')store.set(r.source,r.fixture);if(r.action==='clear')store.set(r.source);const value=store.get(r.source);console.log(JSON.stringify({matched:JSON.stringify(value)===JSON.stringify(r.fixture),absent:value===undefined}));}catch{console.log(JSON.stringify({rejected:true}));}`;
const run = (action: string, directory = profile, registry = source, restricted = false) => {
const result = spawnSync('node', [...(restricted ? ['--permission', '--allow-fs-read=*'] : []), '--input-type=module', '-e', child], {
input: JSON.stringify({ module, profile: directory, source: registry, fixture, action }), encoding: 'utf8', timeout: 15000,
});
expect(result.error).toBeUndefined(); expect(result.status).toBe(0);
return JSON.parse(result.stdout.trim());
};
expect(run('save').matched).toBe(true);
expect(run('read').matched).toBe(true);
const alias = (profile[0] === profile[0]!.toUpperCase() ? profile[0]!.toLowerCase() : profile[0]!.toUpperCase()) + profile.slice(1);
expect(run('read', alias).matched).toBe(true);
const original = readFileSync(file(profile));
expect(original.includes(Buffer.from(fixture.token))).toBe(false);
expect(original.includes(Buffer.from(fixture.identity.displayName))).toBe(false);
expect(run('read', profile, source, true).rejected).toBe(true);
expect(readFileSync(file(profile))).toEqual(original);
expect(run('read').matched).toBe(true);
copyFileSync(file(profile), file(profile, other));
expect(run('read', profile, other).rejected).toBe(true);
mkdirSync(join(second, 'skillhub-accounts'), { recursive: true });
copyFileSync(file(profile), file(second));
expect(run('read', second).rejected).toBe(true);
expect(run('clear').absent).toBe(true);
expect(existsSync(file(profile))).toBe(false);
expect(run('read').absent).toBe(true);
expect(readdirSync(join(profile, 'skillhub-accounts'))).toEqual([file(profile, other).split(/[\\/]/u).at(-1)!]);
} finally { rmSync(root, { recursive: true, force: true }); }
}, 90000);