Files
memind/scripts/goose-v149-canary.test.mjs
T

134 lines
5.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { once } from 'node:events';
import { createTkmindProxy } from '../tkmind-proxy.mjs';
import {
assertLoopbackGooseTarget,
describeGooseCanaryConfig,
parseGooseCanaryMode,
resolveGooseApiTargetsFromEnv,
resolveGooseTargetForIdentity,
} from './goose-v149-canary.mjs';
test('parseGooseCanaryMode defaults to off', () => {
assert.equal(parseGooseCanaryMode({}), 'off');
assert.equal(parseGooseCanaryMode({ TKMIND_GOOSE_CANARY: 'all' }), 'all');
});
test('resolveGooseApiTargetsFromEnv all mode pins Portal to v149', () => {
const config = resolveGooseApiTargetsFromEnv({
TKMIND_GOOSE_CANARY: 'all',
TKMIND_API_TARGET_STABLE: 'https://127.0.0.1:18006',
TKMIND_API_TARGET_V149: 'https://127.0.0.1:18049',
});
assert.equal(config.mode, 'all');
assert.equal(config.primary, 'https://127.0.0.1:18049');
assert.deepEqual(config.targets, ['https://127.0.0.1:18049']);
});
test('users mode excludes the candidate from the default pool even if listed explicitly', () => {
const config = resolveGooseApiTargetsFromEnv({
TKMIND_GOOSE_CANARY: 'users',
TKMIND_GOOSE_CANARY_USER_IDS: 'user-1',
TKMIND_API_TARGETS: 'https://127.0.0.1:18006,https://127.0.0.1:18007,https://127.0.0.1:18008,https://127.0.0.1:18015',
TKMIND_API_TARGET_V149: 'https://127.0.0.1:18015',
});
assert.equal(config.mode, 'users');
assert.equal(config.primary, 'https://127.0.0.1:18006');
assert.deepEqual(config.targets, [
'https://127.0.0.1:18006',
'https://127.0.0.1:18007',
'https://127.0.0.1:18008',
]);
});
test('rollback stops new candidate sessions while preserving both session bindings', async (t) => {
async function startUpstream(label) {
const server = createServer((req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ id: label }));
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
t.after(() => new Promise((resolve) => server.close(resolve)));
return `http://127.0.0.1:${server.address().port}`;
}
const stable = await startUpstream('stable');
const candidate = await startUpstream('candidate');
const env = {
TKMIND_GOOSE_CANARY: 'users',
TKMIND_GOOSE_CANARY_USER_IDS: 'allowed',
TKMIND_GOOSE_CANARY_USERNAMES: '',
TKMIND_GOOSE_CANARY_WECHAT_USER_IDS: '',
TKMIND_API_TARGET: stable,
TKMIND_API_TARGETS: stable,
TKMIND_API_TARGET_1: '',
TKMIND_API_TARGET_V149: candidate,
TKMIND_GOOSE_CANARY_RESUME_TARGET: '',
MEMIND_RUNTIME_REDIS_URL: '',
};
const originalEnv = Object.fromEntries(Object.keys(env).map((key) => [key, process.env[key]]));
t.after(() => {
for (const [key, value] of Object.entries(originalEnv)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
});
Object.assign(process.env, env);
const sessions = new Map([['old-session', stable]]);
const sessionAccess = {
async registerSession({ sessionId, target }) { sessions.set(sessionId, target); },
async getSessionTarget(sessionId) { return { target: sessions.get(sessionId), node: 0 }; },
};
const createProxy = (apiTargets) => createTkmindProxy({
apiTargets,
sessionAccess,
userAuth: { async getUserPublishLayout() { return null; } },
});
const options = { workingDir: '/tmp', sessionPolicy: {} };
const active = createProxy(resolveGooseApiTargetsFromEnv().targets);
assert.equal(await active.resolveTarget('old-session'), stable);
assert.equal((await active.startSessionForUser('other', options)).id, 'stable');
assert.equal((await active.startSessionForUser('allowed', options)).id, 'candidate');
assert.equal(await active.resolveTarget('candidate'), candidate);
process.env.TKMIND_GOOSE_CANARY = 'off';
process.env.TKMIND_GOOSE_CANARY_RESUME_TARGET = candidate;
const rolledBack = createProxy([stable]);
assert.equal((await rolledBack.startSessionForUser('allowed', options)).id, 'stable');
assert.equal(await rolledBack.resolveTarget('old-session'), stable);
assert.equal(await rolledBack.resolveTarget('candidate'), candidate);
});
test('resolveGooseTargetForIdentity routes canary users to v149 only', () => {
const config = resolveGooseApiTargetsFromEnv({
TKMIND_GOOSE_CANARY: 'users',
TKMIND_GOOSE_CANARY_USER_IDS: 'user-abc',
TKMIND_API_TARGET_V149: 'https://127.0.0.1:18015',
});
assert.equal(
resolveGooseTargetForIdentity({ id: 'user-abc' }, config),
'https://127.0.0.1:18015',
);
assert.equal(resolveGooseTargetForIdentity({ id: 'other-user' }, config), null);
});
test('assertLoopbackGooseTarget refuses production hosts', () => {
assert.throws(
() => assertLoopbackGooseTarget('https://114.85.107.50:18006'),
/refuses production host/,
);
});
test('describeGooseCanaryConfig reports disabled when off', () => {
const summary = describeGooseCanaryConfig({
TKMIND_API_TARGET: 'https://127.0.0.1:18006',
});
assert.equal(summary.enabled, false);
assert.equal(summary.mode, 'off');
});