test(11-01): add failing tender-query.builder spec (RED)
Extends TenderQueryDto with validated filter/sort params (q, openOnly, deadlineFrom/To, valueMin/Max, includeNullValue, sort) and adds the RED-first spec for the not-yet-implemented tender-query.builder.ts: NULL-graceful value filter (D-05), openOnly deadline default (D-04), explicit deadline range, and sort whitelist (UI-01). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildOrderBy, buildTenderWhere } from './tender-query.builder';
|
||||
import type { TenderQueryDto } from './dto/tender-query.dto';
|
||||
|
||||
/**
|
||||
* tender-query.builder.spec — RED-first (TDD) proof for FILTER-01/04/05,
|
||||
* UI-01 (D-01, D-04, D-05, D-06).
|
||||
*
|
||||
* Core test (Pitfall 3 / D-05): an active value filter must NEVER
|
||||
* eliminate estimatedValue=null rows — 91.6% of the live Tender data has
|
||||
* no estimatedValue, so a naive range filter would collapse the list.
|
||||
*/
|
||||
|
||||
function dto(overrides: Partial<TenderQueryDto> = {}): TenderQueryDto {
|
||||
return overrides as TenderQueryDto;
|
||||
}
|
||||
|
||||
describe('buildTenderWhere', () => {
|
||||
it('empty DTO: defaults to status=active and openOnly (deadline in future OR null)', () => {
|
||||
const where = buildTenderWhere(dto());
|
||||
|
||||
expect(where.status).toBe('active');
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
OR: [{ deadlineAt: { gte: expect.any(Date) } }, { deadlineAt: null }],
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('openOnly=false omits the deadline-open constraint entirely', () => {
|
||||
const where = buildTenderWhere(dto({ openOnly: false }));
|
||||
|
||||
const and = (where.AND as unknown[]) ?? [];
|
||||
const hasOpenClause = and.some(
|
||||
(clause) =>
|
||||
typeof clause === 'object' &&
|
||||
clause !== null &&
|
||||
'OR' in (clause as Record<string, unknown>) &&
|
||||
JSON.stringify(clause).includes('deadlineAt'),
|
||||
);
|
||||
expect(hasOpenClause).toBe(false);
|
||||
});
|
||||
|
||||
it('deadlineFrom/deadlineTo set: adds an explicit deadlineAt range, combinable with openOnly', () => {
|
||||
const from = new Date('2026-08-01T00:00:00.000Z');
|
||||
const to = new Date('2026-08-31T23:59:59.000Z');
|
||||
const where = buildTenderWhere(dto({ deadlineFrom: from, deadlineTo: to }));
|
||||
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([{ deadlineAt: { gte: from, lte: to } }]),
|
||||
);
|
||||
// openOnly default (true) still applied alongside the explicit range.
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
OR: [{ deadlineAt: { gte: expect.any(Date) } }, { deadlineAt: null }],
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('deadlineFrom only: range uses gte without lte', () => {
|
||||
const from = new Date('2026-08-01T00:00:00.000Z');
|
||||
const where = buildTenderWhere(dto({ deadlineFrom: from }));
|
||||
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([{ deadlineAt: { gte: from } }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('q set: adds case-insensitive OR over title + buyerName', () => {
|
||||
const where = buildTenderWhere(dto({ q: 'Bau' }));
|
||||
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
OR: [
|
||||
{ title: { contains: 'Bau', mode: 'insensitive' } },
|
||||
{ buyerName: { contains: 'Bau', mode: 'insensitive' } },
|
||||
],
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('value filter with default includeNullValue: OR(range, null) — NULL rows survive', () => {
|
||||
const where = buildTenderWhere(dto({ valueMin: 1000, valueMax: 5000 }));
|
||||
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
OR: [
|
||||
{ estimatedValue: { gte: 1000, lte: 5000 } },
|
||||
{ estimatedValue: null },
|
||||
],
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('value filter with includeNullValue=false: pure range, no null branch', () => {
|
||||
const where = buildTenderWhere(
|
||||
dto({ valueMin: 1000, valueMax: 5000, includeNullValue: false }),
|
||||
);
|
||||
|
||||
expect(where.AND).toEqual(
|
||||
expect.arrayContaining([{ estimatedValue: { gte: 1000, lte: 5000 } }]),
|
||||
);
|
||||
const and = (where.AND as unknown[]) ?? [];
|
||||
const hasOrNullBranch = and.some(
|
||||
(clause) =>
|
||||
typeof clause === 'object' &&
|
||||
clause !== null &&
|
||||
JSON.stringify(clause).includes('"estimatedValue":null'),
|
||||
);
|
||||
expect(hasOrNullBranch).toBe(false);
|
||||
});
|
||||
|
||||
it('valueMin only: range has gte but no lte key', () => {
|
||||
const where = buildTenderWhere(dto({ valueMin: 1000 }));
|
||||
|
||||
const and = (where.AND as Array<Record<string, unknown>>) ?? [];
|
||||
const valueClause = and.find((c) => 'OR' in c) as
|
||||
| { OR: Array<Record<string, unknown>> }
|
||||
| undefined;
|
||||
const rangeBranch = valueClause?.OR.find((b) => 'estimatedValue' in b && b.estimatedValue !== null);
|
||||
expect(rangeBranch).toEqual({ estimatedValue: { gte: 1000 } });
|
||||
});
|
||||
|
||||
it('explicit status overrides the active default', () => {
|
||||
const where = buildTenderWhere(dto({ status: 'expired' }));
|
||||
expect(where.status).toBe('expired');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildOrderBy', () => {
|
||||
it('sort=deadline maps to { deadlineAt: asc }', () => {
|
||||
expect(buildOrderBy('deadline')).toEqual({ deadlineAt: 'asc' });
|
||||
});
|
||||
|
||||
it('sort=value maps to { estimatedValue: desc }', () => {
|
||||
expect(buildOrderBy('value')).toEqual({ estimatedValue: 'desc' });
|
||||
});
|
||||
|
||||
it('sort=published maps to { publishedAt: desc }', () => {
|
||||
expect(buildOrderBy('published')).toEqual({ publishedAt: 'desc' });
|
||||
});
|
||||
|
||||
it('unknown/missing sort key defaults to { publishedAt: desc }', () => {
|
||||
expect(buildOrderBy(undefined)).toEqual({ publishedAt: 'desc' });
|
||||
expect(buildOrderBy('not-a-real-key')).toEqual({ publishedAt: 'desc' });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user