test(unit): add lightweight vitest coverage for auth duration and seq pagination utils
This commit is contained in:
@@ -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,
|
||||
|
||||
15
Fabric.Backend.Guild/src/messaging/pagination.util.spec.ts
Normal file
15
Fabric.Backend.Guild/src/messaging/pagination.util.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
14
Fabric.Backend.Guild/src/messaging/pagination.util.ts
Normal file
14
Fabric.Backend.Guild/src/messaging/pagination.util.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user