57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { sourceQuerySchema } from '../src/accounting/source.schemas';
|
|
import {
|
|
sourceScope,
|
|
encodeSourceCursor,
|
|
decodeSourceCursor,
|
|
} from '../src/accounting/source-cursor';
|
|
|
|
it('round-trips validated positions and binds them to scope, window and filters', () => {
|
|
const query = sourceQuerySchema.parse({
|
|
from: '2026-01-01T00:00:00Z',
|
|
to: '2026-01-02T00:00:00Z',
|
|
});
|
|
const scope = sourceScope(randomUUID(), query);
|
|
const row = {
|
|
occurredAt: query.from,
|
|
eventType: 'SUPPLIER_INVOICE',
|
|
sourceId: randomUUID(),
|
|
};
|
|
const token = encodeSourceCursor(row, scope);
|
|
expect(decodeSourceCursor(token, scope, query)).toMatchObject({
|
|
at: query.from,
|
|
type: row.eventType,
|
|
id: row.sourceId,
|
|
scope,
|
|
});
|
|
expect(decodeSourceCursor(undefined, scope, query)).toBeNull();
|
|
expect(() =>
|
|
decodeSourceCursor(token, sourceScope(randomUUID(), query), query),
|
|
).toThrow();
|
|
expect(() => decodeSourceCursor('not-json', scope, query)).toThrow();
|
|
expect(() =>
|
|
decodeSourceCursor(
|
|
encodeSourceCursor({ ...row, occurredAt: query.to }, scope),
|
|
scope,
|
|
query,
|
|
),
|
|
).toThrow();
|
|
const extra = Buffer.from(
|
|
JSON.stringify({
|
|
at: query.from,
|
|
type: row.eventType,
|
|
id: row.sourceId,
|
|
scope,
|
|
organizationId: randomUUID(),
|
|
}),
|
|
).toString('base64url');
|
|
expect(() => decodeSourceCursor(extra, scope, query)).toThrow();
|
|
expect(
|
|
sourceQuerySchema.safeParse({
|
|
from: query.from.toISOString(),
|
|
to: query.to.toISOString(),
|
|
kind: 'UNKNOWN',
|
|
}).success,
|
|
).toBe(false);
|
|
});
|