Assessor_2.0/tests/postgres/client-information-read.tes...

38 lines
2.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { Pool } from 'pg';
import { PostgresAssessorRepository } from '../../server/postgres/postgres-assessor-repository';
import { clientInformationReadPortFrom } from '../../src/persistence/client-information-read-port';
import { clientInformationFixture } from '../client-information-fixture';
describe('Client Information on a read-only PostgreSQL session', () => {
it('reads committed revisions, parameterizes client scope and includes superseded runs only explicitly', async () => {
const fixture = clientInformationFixture();
const statements: Array<{ sql: string; values?: unknown[] }> = [];
const query = vi.fn(async (sql: string, values?: unknown[]) => {
statements.push({ sql, values });
if (sql.startsWith('SHOW')) return { rows: [{ default_transaction_read_only: 'on' }] };
if (sql.includes('assessor_repository_state')) return { rows: [{ revision: '27' }] };
if (sql.includes('AS project_exists')) return { rows: [{ project_exists: true, assessment_exists: true }] };
if (sql.includes('count(*)')) return { rows: [{ count: '1' }] };
if (sql.includes('SELECT r.id')) return { rows: [{ id: 'run-1' }] };
if (sql.includes('SELECT s.data')) return { rows: fixture.sections.map(data => ({ data })) };
if (sql.includes('SELECT o.data')) return { rows: fixture.observations.map(data => ({ data })) };
throw new Error(`Unexpected SQL: ${sql}`);
});
const repository = await PostgresAssessorRepository.openReadOnly({ query } as unknown as Pool);
const reader = clientInformationReadPortFrom(repository, () => repository.getCommittedRevision());
const scope = { projectId: 'client-1', assessmentId: 'slot-1', scanRunIds: ['run-1'] };
const current = await reader.queryClientInformation(scope);
expect(current.repositoryRevision).toBe(27);
expect(current.totalObservations).toBe(6);
const scopeReads = statements.filter(item => item.sql.includes('JOIN assessor_assessments'));
expect(scopeReads).toHaveLength(4);
expect(scopeReads.every(item => item.sql.includes("r.data->>'supersededByScanRunId' IS NULL"))).toBe(true);
expect(scopeReads.every(item => JSON.stringify(item.values) === JSON.stringify(['client-1', 'slot-1', ['run-1']]))).toBe(true);
statements.length = 0;
await reader.queryClientInformation({ ...scope, includeSuperseded: true });
expect(statements.every(item => !item.sql.includes('supersededByScanRunId'))).toBe(true);
expect(statements.every(item => /^(SELECT|SHOW)\b/.test(item.sql.trim()))).toBe(true);
});
});