test(unit): add lightweight vitest coverage for auth duration and seq pagination utils

This commit is contained in:
nav
2026-05-12 11:53:46 +00:00
parent ec796ae609
commit 41a4172267
11 changed files with 2430 additions and 37 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,8 @@
"start:dev": "ts-node src/main.ts",
"lint": "eslint 'src/**/*.ts'",
"lint:fix": "eslint 'src/**/*.ts' --fix",
"format": "prettier --write 'src/**/*.ts'"
"format": "prettier --write 'src/**/*.ts'",
"test:unit": "vitest run"
},
"dependencies": {
"@nestjs/common": "^10.4.8",
@@ -34,6 +35,7 @@
"eslint-config-prettier": "^10.1.8",
"prettier": "^3.8.3",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
"typescript": "^5.7.2",
"vitest": "^4.1.6"
}
}

View File

@@ -17,6 +17,7 @@ import { Channel } from '../entities/channel.entity';
import { Message } from '../entities/message.entity';
import { IdempotencyRecord } from '../entities/idempotency-record.entity';
import { EventsService } from '../events/events.service';
import { clampLimit, computeNextExpectedSeq } from './pagination.util';
const EDIT_WINDOW_MS = 15 * 60 * 1000;
const DEFAULT_PAGE_LIMIT = 50;
@@ -214,11 +215,7 @@ export class MessagingController {
) {
const from = seqFrom ? Number(seqFrom) : 1;
const to = seqTo ? Number(seqTo) : Number.MAX_SAFE_INTEGER;
const requestedLimit = limit ? Number(limit) : DEFAULT_PAGE_LIMIT;
const safeLimit =
Number.isFinite(requestedLimit) && requestedLimit > 0
? Math.min(requestedLimit, MAX_PAGE_LIMIT)
: DEFAULT_PAGE_LIMIT;
const safeLimit = clampLimit(limit, DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT);
if (from > to) {
return {
@@ -251,15 +248,10 @@ export class MessagingController {
const rows = await qb.limit(safeLimit).getMany();
const items = rows.map((m) => this.toView(m));
let nextExpectedSeq = from;
for (const row of rows) {
if (row.seq > nextExpectedSeq) {
break;
}
if (row.seq === nextExpectedSeq) {
nextExpectedSeq += 1;
}
}
const nextExpectedSeq = computeNextExpectedSeq(
from,
rows.map((row) => row.seq),
);
return {
items,

View File

@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { clampLimit, computeNextExpectedSeq } from './pagination.util';
describe('pagination utils', () => {
it('clamps limit safely', () => {
expect(clampLimit(undefined, 50, 200)).toBe(50);
expect(clampLimit('500', 50, 200)).toBe(200);
expect(clampLimit('-1', 50, 200)).toBe(50);
});
it('computes next expected seq', () => {
expect(computeNextExpectedSeq(1, [1, 2, 3])).toBe(4);
expect(computeNextExpectedSeq(1, [1, 3, 4])).toBe(2);
});
});

View File

@@ -0,0 +1,14 @@
export function clampLimit(input: string | undefined, defaultLimit: number, maxLimit: number): number {
const requested = input ? Number(input) : defaultLimit;
if (!Number.isFinite(requested) || requested <= 0) return defaultLimit;
return Math.min(requested, maxLimit);
}
export function computeNextExpectedSeq(from: number, seqs: number[]): number {
let next = from;
for (const seq of seqs) {
if (seq > next) break;
if (seq === next) next += 1;
}
return next;
}