40 lines
1.9 KiB
JavaScript
40 lines
1.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import test from 'node:test';
|
|
|
|
import { createValidateSubscription } from '../../dist/server/features/subscription/index.js';
|
|
import { createSubscriptionValidationRoute } from '../../dist/server/http/routes/subscriptionValidationRoute.js';
|
|
|
|
test('subscription validation trims once and returns only the provider server count', async () => {
|
|
const calls = [];
|
|
const validate = createValidateSubscription(async (url) => {
|
|
calls.push(url);
|
|
return { servers: [{ id: 'one' }, { id: 'two' }], privateConfig: 'not returned' };
|
|
});
|
|
|
|
assert.deepEqual(await validate(' https://provider.example/sub '), { servers: 2 });
|
|
assert.deepEqual(await validate(null), { servers: 2 });
|
|
assert.deepEqual(calls, ['https://provider.example/sub', 'null']);
|
|
|
|
const failure = new Error('provider failed');
|
|
await assert.rejects(createValidateSubscription(async () => { throw failure; })('url'), (error) => error === failure);
|
|
});
|
|
|
|
test('subscription validation route is the only method/path adapter', async () => {
|
|
const sent = [];
|
|
const route = createSubscriptionValidationRoute({
|
|
validateSubscription: async (url) => ({ servers: String(url).length }),
|
|
readBody: async () => ({ url: 'trimmed' }),
|
|
sendState: async (_res, extra) => { sent.push(extra); },
|
|
});
|
|
const response = {};
|
|
|
|
assert.equal(await route.handle({ method: 'GET', url: '/api/subscription/validate' }, response), false);
|
|
assert.equal(await route.handle({ method: 'POST', url: '/api/subscription/validate' }, response), true);
|
|
assert.deepEqual(sent, [{ servers: 7 }]);
|
|
|
|
const compositionSource = readFileSync(new URL('../../src/server/index.ts', import.meta.url), 'utf8');
|
|
assert.match(compositionSource, /createSubscriptionValidationRoute\(\{/);
|
|
assert.doesNotMatch(compositionSource, /req\.url === ['"]\/api\/subscription\/validate['"]/);
|
|
});
|