返回 CodeWhale
community-agent.test.ts
根目录 / web / lib / community-agent.test.ts
1 import { describe, it, expect, vi, beforeEach } from "vitest";
2 import { validateSession } from "./community-agent";
3
4 describe("validateSession", () => {
5 let mockKV: {
6 get: ReturnType<typeof vi.fn>;
7 put: ReturnType<typeof vi.fn>;
8 list: ReturnType<typeof vi.fn>;
9 delete: ReturnType<typeof vi.fn>;
10 };
11
12 beforeEach(() => {
13 mockKV = {
14 get: vi.fn(),
15 put: vi.fn(),
16 list: vi.fn(),
17 delete: vi.fn(),
18 };
19 });
20
21 const VALID_SID = "a".repeat(40);
22 const SESSION_PREFIX = "session:admin:";
23
24 it("should return false if kv is undefined", async () => {
25 const result = await validateSession(undefined, VALID_SID);
26 expect(result).toBe(false);
27 });
28
29 it("should return false if sid is undefined or null", async () => {
30 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
31 expect(await validateSession(kv, undefined)).toBe(false);
32 expect(await validateSession(kv, null)).toBe(false);
33 });
34
35 it("should return false if sid is empty", async () => {
36 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
37 expect(await validateSession(kv, "")).toBe(false);
38 });
39
40 it("should return false if sid length is less than 40", async () => {
41 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
42 expect(await validateSession(kv, "a".repeat(39))).toBe(false);
43 });
44
45 it("should return false if sid length is greater than 64", async () => {
46 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
47 expect(await validateSession(kv, "a".repeat(65))).toBe(false);
48 });
49
50 it("should return false if sid contains invalid characters", async () => {
51 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
52 // Length 40 but contains invalid char '!'
53 expect(await validateSession(kv, "a".repeat(39) + "!")).toBe(false);
54 });
55
56 it("should return false if session is not found in KV", async () => {
57 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
58 mockKV.get.mockResolvedValue(null);
59
60 const result = await validateSession(kv, VALID_SID);
61 expect(result).toBe(false);
62 expect(mockKV.get).toHaveBeenCalledWith(SESSION_PREFIX + VALID_SID);
63 });
64
65 it("should return true if session is found in KV", async () => {
66 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
67 mockKV.get.mockResolvedValue(JSON.stringify({ createdAt: Date.now() }));
68
69 const result = await validateSession(kv, VALID_SID);
70 expect(result).toBe(true);
71 expect(mockKV.get).toHaveBeenCalledWith(SESSION_PREFIX + VALID_SID);
72 });
73
74 it("should work with a 64 character sid", async () => {
75 const kv = mockKV as unknown as Parameters<typeof validateSession>[0];
76 mockKV.get.mockResolvedValue("{}");
77
78 const sid = "b".repeat(64);
79 const result = await validateSession(kv, sid);
80 expect(result).toBe(true);
81 expect(mockKV.get).toHaveBeenCalledWith(SESSION_PREFIX + sid);
82 });
83 });
84
84 lines TYPESCRIPT