Merge branch 'test-coverage'
Some checks failed
CI / ci (push) Has been cancelled

Add test utilities and ast-grep rules for code quality.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
igm
2026-01-12 15:05:07 +08:00
38 changed files with 247 additions and 172 deletions

View File

@@ -95,11 +95,11 @@ export function formatRelativeDate(
return "Yesterday";
}
if (diffDays < 7) {
return `${String(diffDays)} days ago`;
return `${diffDays.toLocaleString()} days ago`;
}
if (diffDays < 30) {
const weeks = Math.floor(diffDays / 7);
return weeks === 1 ? "1 week ago" : `${String(weeks)} weeks ago`;
return weeks === 1 ? "1 week ago" : `${weeks.toLocaleString()} weeks ago`;
}
// For older dates, show the actual date

View File

@@ -63,7 +63,7 @@ export const hashPassword = async (password: string): Promise<string> => {
const saltB64 = toBase64(salt.buffer);
const hashB64 = toBase64(derivedBits);
return `$${ALGORITHM}$${String(ITERATIONS)}$${saltB64}$${hashB64}`;
return `$${ALGORITHM}$${ITERATIONS.toString()}$${saltB64}$${hashB64}`;
};
export const verifyPassword = async (

View File

@@ -16,3 +16,4 @@ export {
truncateAllTables,
} from "./test-db.js";
export { withTestTransaction } from "./test-transaction.js";
export { uniqueTestId } from "./test-utils.js";

View File

@@ -9,6 +9,7 @@ import { join } from "node:path";
import { createDb } from "@reviq/db";
import { sql } from "kysely";
import pg from "pg";
import { uniqueTestId } from "./test-utils.js";
const { Client } = pg;
@@ -192,7 +193,7 @@ export async function runMigrations(): Promise<void> {
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text();
throw new Error(
`Migration failed with code ${String(exitCode)}: ${stderr}`,
`Migration failed with code ${exitCode.toString()}: ${stderr}`,
);
}
}
@@ -224,7 +225,7 @@ export async function createTestUser(
isSuperuser: boolean;
}> = {},
): Promise<{ id: number; email: string }> {
const email = overrides.email ?? `test-${String(Date.now())}@example.com`;
const email = overrides.email ?? `test-${uniqueTestId()}@example.com`;
const result = await db
.insertInto("users")

View File

@@ -0,0 +1,15 @@
/**
* Test utility functions
*/
/**
* Generates a unique test ID using timestamp and random string.
* Useful for creating unique emails, slugs, tokens, etc. in tests.
*
* @example
* const email = `user-${uniqueTestId()}@example.com`
* const slug = `org-${uniqueTestId()}`
*/
export function uniqueTestId(): string {
return `${Date.now().toString()}-${Math.random().toString(36).slice(2, 8)}`;
}