文件
tennis-training-hub/server/features.test.ts

238 行
6.6 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from "vitest";
import { appRouter } from "./routers";
import { COOKIE_NAME } from "../shared/const";
import type { TrpcContext } from "./_core/context";
type AuthenticatedUser = NonNullable<TrpcContext["user"]>;
function createTestUser(overrides?: Partial<AuthenticatedUser>): AuthenticatedUser {
return {
id: 1,
openId: "test-user-001",
email: "test@example.com",
name: "TestPlayer",
loginMethod: "username",
role: "user",
skillLevel: "beginner",
trainingGoals: null,
ntrpRating: 1.5,
totalSessions: 0,
totalMinutes: 0,
createdAt: new Date(),
updatedAt: new Date(),
lastSignedIn: new Date(),
...overrides,
};
}
function createMockContext(user: AuthenticatedUser | null = null): {
ctx: TrpcContext;
clearedCookies: { name: string; options: Record<string, unknown> }[];
setCookies: { name: string; value: string; options: Record<string, unknown> }[];
} {
const clearedCookies: { name: string; options: Record<string, unknown> }[] = [];
const setCookies: { name: string; value: string; options: Record<string, unknown> }[] = [];
return {
ctx: {
user,
req: {
protocol: "https",
headers: {},
} as TrpcContext["req"],
res: {
clearCookie: (name: string, options: Record<string, unknown>) => {
clearedCookies.push({ name, options });
},
cookie: (name: string, value: string, options: Record<string, unknown>) => {
setCookies.push({ name, value, options });
},
} as TrpcContext["res"],
},
clearedCookies,
setCookies,
};
}
describe("auth.me", () => {
it("returns null for unauthenticated users", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.me();
expect(result).toBeNull();
});
it("returns user data for authenticated users", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.me();
expect(result).toBeDefined();
expect(result?.name).toBe("TestPlayer");
expect(result?.openId).toBe("test-user-001");
});
});
describe("auth.logout", () => {
it("clears the session cookie and reports success", async () => {
const user = createTestUser();
const { ctx, clearedCookies } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.logout();
expect(result).toEqual({ success: true });
expect(clearedCookies).toHaveLength(1);
expect(clearedCookies[0]?.name).toBe(COOKIE_NAME);
expect(clearedCookies[0]?.options).toMatchObject({
maxAge: -1,
secure: true,
sameSite: "none",
httpOnly: true,
path: "/",
});
});
});
describe("profile.stats", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.profile.stats()).rejects.toThrow();
});
});
describe("plan.generate input validation", () => {
it("rejects invalid skill level", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "expert" as any,
durationDays: 7,
})
).rejects.toThrow();
});
it("rejects invalid duration", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "beginner",
durationDays: 0,
})
).rejects.toThrow();
});
it("rejects duration over 30", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "beginner",
durationDays: 31,
})
).rejects.toThrow();
});
});
describe("video.upload input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.video.upload({
title: "test",
format: "mp4",
fileSize: 1000,
fileBase64: "dGVzdA==",
})
).rejects.toThrow();
});
});
describe("analysis.save input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.analysis.save({
videoId: 1,
overallScore: 75,
})
).rejects.toThrow();
});
});
describe("analysis.getCorrections input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.analysis.getCorrections({
poseMetrics: {},
exerciseType: "forehand",
detectedIssues: [],
})
).rejects.toThrow();
});
});
describe("record.create input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.record.create({
exerciseName: "正手挥拍",
durationMinutes: 30,
})
).rejects.toThrow();
});
it("accepts valid exercise name", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
// This should not throw on input validation (may throw on DB)
// We just verify the input schema accepts a valid name
try {
await caller.record.create({
exerciseName: "正手挥拍",
durationMinutes: 30,
});
} catch (e: any) {
// DB errors are expected in test env, but input validation should pass
expect(e.message).not.toContain("invalid_type");
}
});
});
describe("rating.history", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.rating.history()).rejects.toThrow();
});
});
describe("rating.current", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.rating.current()).rejects.toThrow();
});
});