147 lines
3.6 KiB
JavaScript
147 lines
3.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createPortalSessionCoordinator } from './portal-session-coordinator.mjs';
|
|
|
|
test('session coordinator prefers the initialized access service', async () => {
|
|
const calls = [];
|
|
const access = {
|
|
async validateOwnership(userId, sessionId) {
|
|
calls.push({ kind: 'owns', userId, sessionId });
|
|
return true;
|
|
},
|
|
async unregisterSession(input) {
|
|
calls.push({ kind: 'unregister', input });
|
|
},
|
|
};
|
|
const coordinator = createPortalSessionCoordinator({
|
|
getSessionAccess: () => access,
|
|
getUserAuth: () => {
|
|
throw new Error('fallback user auth should not be read');
|
|
},
|
|
createSessionAccessFn: () => {
|
|
throw new Error('fallback access should not be created');
|
|
},
|
|
});
|
|
|
|
assert.equal(
|
|
await coordinator.ownsAgentSession('user-1', 'session-1'),
|
|
true,
|
|
);
|
|
await coordinator.unregisterAgentSessionForUser(
|
|
'user-1',
|
|
'session-1',
|
|
);
|
|
assert.deepEqual(calls, [
|
|
{
|
|
kind: 'owns',
|
|
userId: 'user-1',
|
|
sessionId: 'session-1',
|
|
},
|
|
{
|
|
kind: 'unregister',
|
|
input: {
|
|
userId: 'user-1',
|
|
sessionId: 'session-1',
|
|
},
|
|
},
|
|
]);
|
|
});
|
|
|
|
test('session coordinator creates a fallback access facade on demand', async () => {
|
|
const calls = [];
|
|
const userAuth = { id: 'auth-1' };
|
|
const coordinator = createPortalSessionCoordinator({
|
|
getUserAuth: () => userAuth,
|
|
isSessionBrokerEnabledFn: () => true,
|
|
createSessionAccessFn(input) {
|
|
calls.push(input);
|
|
return {
|
|
async validateOwnership() {
|
|
return false;
|
|
},
|
|
async unregisterSession() {},
|
|
};
|
|
},
|
|
});
|
|
|
|
assert.equal(
|
|
await coordinator.ownsAgentSession('user-1', 'session-1'),
|
|
false,
|
|
);
|
|
assert.deepEqual(calls, [
|
|
{
|
|
userAuth,
|
|
enabled: true,
|
|
},
|
|
]);
|
|
});
|
|
|
|
test('session coordinator fails closed for missing identifiers or auth', async () => {
|
|
let fallbackCreates = 0;
|
|
const coordinator = createPortalSessionCoordinator({
|
|
createSessionAccessFn() {
|
|
fallbackCreates += 1;
|
|
return {};
|
|
},
|
|
});
|
|
|
|
assert.equal(
|
|
await coordinator.ownsAgentSession('', 'session-1'),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
await coordinator.ownsAgentSession('user-1', ''),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
await coordinator.ownsAgentSession('user-1', 'session-1'),
|
|
false,
|
|
);
|
|
await coordinator.unregisterAgentSessionForUser(
|
|
'',
|
|
'session-1',
|
|
);
|
|
await coordinator.unregisterAgentSessionForUser(
|
|
'user-1',
|
|
'session-1',
|
|
);
|
|
assert.equal(fallbackCreates, 0);
|
|
});
|
|
|
|
test('page delivery locks preserve nesting and tolerate extra releases', () => {
|
|
const coordinator = createPortalSessionCoordinator();
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-1'),
|
|
false,
|
|
);
|
|
coordinator.beginSessionPageDelivery('session-1');
|
|
coordinator.beginSessionPageDelivery('session-1');
|
|
coordinator.beginSessionPageDelivery('session-2');
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-1'),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-2'),
|
|
true,
|
|
);
|
|
|
|
coordinator.endSessionPageDelivery('session-1');
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-1'),
|
|
true,
|
|
);
|
|
coordinator.endSessionPageDelivery('session-1');
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-1'),
|
|
false,
|
|
);
|
|
coordinator.endSessionPageDelivery('session-1');
|
|
coordinator.beginSessionPageDelivery('');
|
|
coordinator.endSessionPageDelivery('');
|
|
assert.equal(
|
|
coordinator.isSessionPageDeliveryActive('session-1'),
|
|
false,
|
|
);
|
|
});
|