105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
/**
|
|
* GATE 3 — Store unit tests
|
|
*/
|
|
|
|
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
useTempStore,
|
|
seedFreeTenant,
|
|
seedEnterpriseTenant,
|
|
} from '../helpers.js';
|
|
import { getTenantByApiKey, resolveLimits, getTenant } from '../../src/store/tenants.js';
|
|
import {
|
|
createWebhook,
|
|
getActiveWebhooks,
|
|
listWebhooksForTenant,
|
|
deleteWebhook,
|
|
} from '../../src/store/webhooks.js';
|
|
import { getUsage, incrementUsage } from '../../src/store/usage.js';
|
|
import { reloadStore, getStore } from '../../src/store/db.js';
|
|
|
|
describe('stores', () => {
|
|
/** @type {ReturnType<typeof useTempStore>} */
|
|
let ctx;
|
|
|
|
beforeEach(() => {
|
|
ctx = useTempStore();
|
|
});
|
|
|
|
afterEach(() => {
|
|
ctx.cleanup();
|
|
});
|
|
|
|
it('create tenant → API key resolves to same tenant', () => {
|
|
const { tenant, apiKey } = seedFreeTenant();
|
|
const found = getTenantByApiKey(apiKey);
|
|
assert.ok(found);
|
|
assert.equal(found.id, tenant.id);
|
|
assert.equal(found.plan, 'free');
|
|
});
|
|
|
|
it('free plan limits applied; enterprise contract overrides', () => {
|
|
const { tenant: free } = seedFreeTenant();
|
|
const freeLimits = resolveLimits(free);
|
|
assert.equal(freeLimits.timestamps, 50);
|
|
assert.equal(freeLimits.batch, false);
|
|
|
|
const { tenant: ent } = seedEnterpriseTenant();
|
|
const entLimits = resolveLimits(ent);
|
|
assert.equal(entLimits.timestamps, 10);
|
|
assert.equal(entLimits.batch, true);
|
|
assert.equal(entLimits.batchMaxItems, 5);
|
|
});
|
|
|
|
it('webhook isolation per tenant', () => {
|
|
seedFreeTenant({ id: 'a' });
|
|
seedFreeTenant({ id: 'b', veraeUsername: 'u2' });
|
|
|
|
createWebhook({
|
|
tenantId: 'a',
|
|
targetUrl: 'https://hooks.example/a',
|
|
event: 'timestamp.completed',
|
|
});
|
|
createWebhook({
|
|
tenantId: 'b',
|
|
targetUrl: 'https://hooks.example/b',
|
|
event: 'timestamp.completed',
|
|
});
|
|
|
|
const aHooks = listWebhooksForTenant('a');
|
|
const bHooks = getActiveWebhooks('b', 'timestamp.completed');
|
|
assert.equal(aHooks.length, 1);
|
|
assert.equal(aHooks[0].targetUrl, 'https://hooks.example/a');
|
|
assert.equal(bHooks.length, 1);
|
|
assert.equal(bHooks[0].tenantId, 'b');
|
|
});
|
|
|
|
it('persist + reload round-trip preserves data', () => {
|
|
const { tenant, apiKey } = seedFreeTenant({ id: 'persist-me' });
|
|
incrementUsage(tenant.id, 'timestamps', 3);
|
|
createWebhook({
|
|
tenantId: tenant.id,
|
|
targetUrl: 'https://hooks.example/x',
|
|
event: 'timestamp.completed',
|
|
});
|
|
|
|
reloadStore(ctx.storePath);
|
|
|
|
assert.equal(getTenant('persist-me')?.name, tenant.name);
|
|
assert.equal(getTenantByApiKey(apiKey)?.id, 'persist-me');
|
|
assert.equal(getUsage('persist-me').timestamps, 3);
|
|
assert.equal(getStore().webhooks.length, 1);
|
|
});
|
|
|
|
it('deleteWebhook removes by id', () => {
|
|
seedFreeTenant({ id: 'w' });
|
|
const hook = createWebhook({
|
|
tenantId: 'w',
|
|
targetUrl: 'https://hooks.example/w',
|
|
event: 'timestamp.completed',
|
|
});
|
|
assert.equal(deleteWebhook({ tenantId: 'w', hookId: hook.id }), true);
|
|
assert.equal(listWebhooksForTenant('w').length, 0);
|
|
});
|
|
});
|