-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.test.ts
More file actions
113 lines (98 loc) · 3.29 KB
/
Copy pathserver.test.ts
File metadata and controls
113 lines (98 loc) · 3.29 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { describe, it, expect, vi, beforeEach } from "vitest";
import request from "supertest";
import { app } from "./server";
// Mock dongnelibrary module
vi.mock("dongnelibrary", () => ({
search: vi.fn(
(
_options: { title: string; libraryName: string },
_unknown: unknown,
callback: (
err: Error | null,
result: Array<{ booklist: Array<{ title: string; exist: boolean }> }>
) => void
) => {
callback(null, [
{
booklist: [
{ title: "테스트 책", exist: true },
{ title: "다른 책", exist: false },
],
},
]);
}
),
getLibraryNames: vi.fn(() => ["판교", "동탄", "성남"]),
getAllLibraryNames: vi.fn(() => ["판교", "동탄", "성남"]),
getAllModuleNames: vi.fn(() => ["성남시도서관", "수원시도서관"]),
isModuleName: vi.fn((name: string) =>
["성남시도서관", "수원시도서관"].includes(name)
),
}));
describe("Server API", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("GET /libraryList", () => {
it("returns array of library names", async () => {
const res = await request(app).get("/libraryList");
expect(res.status).toBe(200);
expect(res.body).toEqual(["판교", "동탄", "성남"]);
});
});
describe("GET /moduleList", () => {
it("returns array of objects with name and libraries", async () => {
const res = await request(app).get("/moduleList");
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBeGreaterThan(0);
res.body.forEach(
(mod: { name: string; libraries: string[] }) => {
expect(mod).toHaveProperty("name");
expect(mod).toHaveProperty("libraries");
expect(typeof mod.name).toBe("string");
expect(Array.isArray(mod.libraries)).toBe(true);
}
);
});
});
describe("GET /search", () => {
it("returns search results as JSON", async () => {
const res = await request(app).get(
"/search?title=테스트&libraryName=판교"
);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body[0]).toHaveProperty("booklist");
});
it("returns empty results when no books found", async () => {
const dl = await import("dongnelibrary");
vi.mocked(dl.search).mockImplementationOnce(
(_options, _unknown, callback) => {
callback?.(null, []);
}
);
const res = await request(app).get(
"/search?title=없는책&libraryName=판교"
);
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});
});
describe("GET /:title/:libraryName", () => {
it("returns HTML with availability markers", async () => {
const res = await request(app).get("/테스트/판교");
expect(res.status).toBe(200);
expect(res.text).toContain("✓");
expect(res.text).toContain("✖");
expect(res.text).toContain("테스트 책");
});
});
describe("404 handling", () => {
it("returns 404 for unknown routes", async () => {
const res = await request(app).get("/unknown/route/test");
expect(res.status).toBe(404);
expect(res.body).toHaveProperty("message");
});
});
});