Files
stack/packages/webui/tests/org-bus.test.mjs
jason.woltje 0e9b2c505d Org polish: lock-free reads, visible org errors, no-board label (P19 #867, #868, #869)
#867: organization reads no longer take the registry writer lock; they
retry while the lock exists or the registry changed during the read.
#868: a broken organization is listed as invalid with a path-masked
reason instead of disappearing; the Board, header and picker show it,
and /api/orgs 503 carries the reason. Orgs the principal may not see and
archived orgs still give an empty list.
#869: a Console with no board says "No board", not "Board: null".
#866 (save-path validation at bus admission) is left out: it refused
documents the bus accepted before and needs a migration story first.

Every bus, business, cli and webui file run serially; failures are only
the known EISDIR node_modules-link files.
2026-11-08 15:27:19 -06:00

48 lines
3.3 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import * as bus from '../src/organizations.mjs';
test('organization adapter rechecks authority and verifies roots before selecting paths', async () => {
const checks = [], ctx = { configDir: '/cfg', dataRoot: '/data', instance: 'test' };
const adapter = bus.organizationConsole({ ctx,
listOrgs: async () => [{ id: 'alpha', state: 'active' }, { id: 'beta', state: 'active' }, { id: 'reserved', state: 'reserved' }],
showOrg: async (context, id) => { checks.push([id, context.principal.sub]); if (id === 'beta') throw Object.assign(Error('denied'), { code: 'org-forbidden' }); return { doc: { appearance: { theme: 'harbor' } } }; },
resolveOrg: async (_, id) => ({ id, root: `/verified/${id}`, state: 'active' }),
orgPaths: root => ({ socket: `${root}/bus/broker.sock` }),
busStatus: async (_, id) => ({ healthy: id === 'alpha' }),
options: ({ id, paths }) => ({ id, socket: paths.socket }),
});
const session = { provider: 'local', iss: 'local', sub: 'one' };
assert.deepEqual(await adapter.list(session), [{ id: 'alpha', label: 'alpha', theme: 'harbor', busHealthy: true }]);
assert.deepEqual(await adapter.resolve('alpha', session), { id: 'alpha', socket: '/verified/alpha/bus/broker.sock', busHealthy: true });
await assert.rejects(adapter.resolve('beta', session));
assert.deepEqual(checks, [['alpha', 'one'], ['beta', 'one'], ['alpha', 'one'], ['beta', 'one']]);
});
test('adapter uses the registry and owner mapping for two scratch organizations', async t => {
const { mkdtempSync, mkdirSync, rmSync } = await import('node:fs');
const { tmpdir } = await import('node:os');
const { join } = await import('node:path');
const { createOrg, showOrg, templateRender } = await import('../../business/src/org-service.mjs');
const { listOrgs, resolveOrg } = await import('../../business/src/org-registry.mjs');
const root = mkdtempSync(join(tmpdir(), 'org-console-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const ctx = { configDir: join(root, 'config'), dataRoot: join(root, 'data'), instance: 'test' };
mkdirSync(ctx.configDir, { mode: 0o700 }); mkdirSync(ctx.dataRoot, { mode: 0o700 });
const principal = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
for (const id of ['alpha', 'beta']) await createOrg({ ...ctx, principal }, id, await templateRender(id));
const adapter = bus.organizationConsole({ ctx, listOrgs, showOrg, resolveOrg,
orgPaths: orgRoot => ({ socket: join(orgRoot, 'bus', 'broker.sock') }),
busStatus: async (_, id) => ({ healthy: id === 'alpha' }),
options: ({ id, paths }) => ({ id, socket: paths.socket }),
});
assert.deepEqual((await adapter.list(principal)).map(o => [o.id, o.busHealthy]), [['alpha', true], ['beta', false]]);
const a = await adapter.resolve('alpha', principal), b = await adapter.resolve('beta', principal);
assert.notEqual(a.socket, b.socket);
assert.equal(a.socket, join(ctx.dataRoot, 'org-runtime', 'alpha', 'bus', 'broker.sock'));
const unauthorized = { ...principal, sub: 'someone-else' };
assert.deepEqual(await adapter.list(unauthorized), []);
await assert.rejects(adapter.resolve('alpha', unauthorized));
await assert.rejects(adapter.resolve('missing', principal));
});