152 lines
4.6 KiB
TypeScript
152 lines
4.6 KiB
TypeScript
/**
|
|
* Unit tests for src/validation/0pt-schemas.ts -- the Wave 2 request-body
|
|
* validation for 0pt's mutating capability routes. Confirms a valid body
|
|
* passes and a malformed body produces a structured 400.
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
parseBody,
|
|
documentCaptureSchema,
|
|
documentExtractionSchema,
|
|
sourceQuerySchema,
|
|
createSourceRatePolicySchema,
|
|
mapDatasetToIcebergTableSchema,
|
|
requestIcebergWriteSchema,
|
|
} from '@/src/validation/0pt-schemas';
|
|
|
|
describe('parseBody + documentCaptureSchema', () => {
|
|
const valid = {
|
|
mailbox: {
|
|
tenant_id: 't1',
|
|
client_id: 'c1',
|
|
client_secret: 's1',
|
|
mailbox_address: 'ap@x.io',
|
|
},
|
|
tenant_id: 't1',
|
|
project_id: 'p1',
|
|
};
|
|
|
|
it('accepts a valid capture body', () => {
|
|
const r = parseBody(documentCaptureSchema, valid);
|
|
expect(r.ok).toBe(true);
|
|
});
|
|
|
|
it('rejects a body missing tenant_id with a 400 + issues', () => {
|
|
const { tenant_id: _omit, ...bad } = valid;
|
|
const r = parseBody(documentCaptureSchema, bad);
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) {
|
|
expect(r.status).toBe(400);
|
|
expect(r.body.error).toBe('validation_error');
|
|
expect(r.body.issues.some((i) => i.path === 'tenant_id')).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('rejects a wrong-typed top field', () => {
|
|
const r = parseBody(documentCaptureSchema, { ...valid, top: 'ten' });
|
|
expect(r.ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('documentExtractionSchema', () => {
|
|
it('accepts minimal valid input', () => {
|
|
expect(parseBody(documentExtractionSchema, { tenant_id: 't', project_id: 'p' }).ok).toBe(true);
|
|
});
|
|
it('rejects empty body', () => {
|
|
expect(parseBody(documentExtractionSchema, {}).ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('sourceQuerySchema', () => {
|
|
it('accepts a valid query', () => {
|
|
const r = parseBody(sourceQuerySchema, {
|
|
query: 'SELECT 1',
|
|
tenant_id: 't',
|
|
project_id: 'p',
|
|
connection_config: { host: 'db' },
|
|
});
|
|
expect(r.ok).toBe(true);
|
|
});
|
|
it('rejects a blank query', () => {
|
|
const r = parseBody(sourceQuerySchema, {
|
|
query: ' ',
|
|
tenant_id: 't',
|
|
project_id: 'p',
|
|
connection_config: {},
|
|
});
|
|
expect(r.ok).toBe(false);
|
|
});
|
|
it('rejects a missing connection_config', () => {
|
|
const r = parseBody(sourceQuerySchema, { query: 'SELECT 1', tenant_id: 't', project_id: 'p' });
|
|
expect(r.ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('createSourceRatePolicySchema', () => {
|
|
const valid = {
|
|
source_rate_policy_id: 'rp1',
|
|
tenant_id: 't',
|
|
project_id: 'p',
|
|
source_id: 's',
|
|
rate_limit_per_minute: 60,
|
|
crawl_budget: 1000,
|
|
};
|
|
it('accepts a valid policy', () => {
|
|
expect(parseBody(createSourceRatePolicySchema, valid).ok).toBe(true);
|
|
});
|
|
it('rejects a bad access enum', () => {
|
|
expect(parseBody(createSourceRatePolicySchema, { ...valid, access: 'ftp' }).ok).toBe(false);
|
|
});
|
|
it('rejects a non-numeric rate_limit_per_minute', () => {
|
|
expect(parseBody(createSourceRatePolicySchema, { ...valid, rate_limit_per_minute: 'fast' }).ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('mapDatasetToIcebergTableSchema', () => {
|
|
const valid = {
|
|
tenant_id: 't',
|
|
project_id: 'p',
|
|
dataset_id: 'd',
|
|
dataset_iceberg_mapping_id: 'm',
|
|
namespace: 'ns',
|
|
table_name: 'tbl',
|
|
storage_uri: 's3://x',
|
|
catalog_ref: 'cat',
|
|
actor_id: 'a',
|
|
correlation_id: 'corr',
|
|
};
|
|
it('accepts a valid mapping', () => {
|
|
expect(parseBody(mapDatasetToIcebergTableSchema, valid).ok).toBe(true);
|
|
});
|
|
it('rejects a mapping missing storage_uri', () => {
|
|
const { storage_uri: _omit, ...bad } = valid;
|
|
expect(parseBody(mapDatasetToIcebergTableSchema, bad).ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('requestIcebergWriteSchema', () => {
|
|
const valid = {
|
|
// P014: tenant_id is now required -- the route cross-checks it against
|
|
// the caller's verified tenant and threads it into
|
|
// IcebergWriteService.requestIcebergWrite so a foreign mapping_id can no
|
|
// longer be written to (see getMapping's doc comment).
|
|
tenant_id: 't1',
|
|
actor_id: 'a',
|
|
correlation_id: 'corr',
|
|
schema_version: 'v1',
|
|
manifest_uri: 's3://m',
|
|
};
|
|
it('accepts a valid write request', () => {
|
|
expect(parseBody(requestIcebergWriteSchema, valid).ok).toBe(true);
|
|
});
|
|
it('rejects a write request missing manifest_uri', () => {
|
|
const { manifest_uri: _omit, ...bad } = valid;
|
|
expect(parseBody(requestIcebergWriteSchema, bad).ok).toBe(false);
|
|
});
|
|
it('rejects a write request missing tenant_id', () => {
|
|
const { tenant_id: _omit, ...bad } = valid;
|
|
expect(parseBody(requestIcebergWriteSchema, bad).ok).toBe(false);
|
|
});
|
|
});
|