File size: 2,840 Bytes
92a1d83 2b62b0c 92a1d83 2b62b0c 92a1d83 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | import { describe, expect, it, beforeAll, afterAll, mock } from "bun:test";
import { drizzle } from "drizzle-orm/pglite";
import * as schema from "../../../db/src/schema";
import { closeTestPGlite, getTestPGlite, createTestUserData } from "./test-setup";
mock.module("@pram/env/server", () => ({
env: {
DATABASE_URL: "postgres://localhost:5432/test",
BETTER_AUTH_SECRET: "a".repeat(32),
BETTER_AUTH_URL: "http://localhost:3000",
CORS_ORIGIN: "http://localhost:5173",
API_KEY_ENCRYPTION_KEY: "z".repeat(32),
REDIS_URL: "redis://localhost:6379",
SMTP_HOST: "localhost",
SMTP_USER: "test",
SMTP_PASS: "test",
SMTP_FROM: "test@test.com",
},
}));
describe("question router", () => {
let questionRouter: any;
let user1: any;
beforeAll(async () => {
const pg = await getTestPGlite();
const testDb = drizzle(pg, { schema });
mock.module("@pram/db", () => ({
__esModule: true,
...schema,
db: testDb,
}));
const data = await createTestUserData();
user1 = data.user1;
const mod = await import("../routers/question");
questionRouter = mod.questionRouter;
});
afterAll(async () => {
await closeTestPGlite();
});
let caller: any;
let protectedCaller: any;
beforeAll(async () => {
caller = questionRouter.createCaller({ session: null, auth: null });
protectedCaller = questionRouter.createCaller({
session: { user: { id: user1.id }, expiresAt: new Date() },
auth: null,
});
});
it("list returns public questions", async () => {
const result = await caller.list();
expect(result.questions).toHaveLength(1);
expect(result.questions[0]).not.toHaveProperty("correctAnswer");
expect(result.total).toBe(1);
});
it("list filters by examTypeId", async () => {
const result = await caller.list({ examTypeId: "IELTS" });
expect(result.questions).toHaveLength(1);
});
it("list returns empty when no match", async () => {
const result = await caller.list({ examTypeId: "TOEFL" });
expect(result.questions).toHaveLength(0);
});
it("getById returns null for non-existent question", async () => {
const { randomUUID } = await import("node:crypto");
const result = await caller.getById({ id: randomUUID() });
expect(result).toBeNull();
});
it("create inserts a new question", async () => {
const result = await protectedCaller.create({
examTypeId: "IELTS",
sectionTypeId: "READING",
format: "fill_blank",
passageText: "B".repeat(60),
questionText: "What is the answer?",
correctAnswer: "test answer",
difficulty: 2,
isCaseSensitive: false,
skillTags: ["vocabulary"],
isPublic: false,
});
expect(result).toHaveProperty("id");
expect(result.format).toBe("fill_blank");
});
});
|