-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.test.ts
More file actions
224 lines (200 loc) · 6.37 KB
/
Copy pathsearch.test.ts
File metadata and controls
224 lines (200 loc) · 6.37 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { env } from "cloudflare:test";
import api from "./index";
import { createTestSession, createTestUser, cleanupDatabase } from "../../test/helpers";
import type {
ErrorResponseDto,
SearchBookByIsbnResponseDto,
SearchBooksResponseDto,
SuccessResponseDto,
} from "../../types/dto";
describe("Search API Integration", () => {
beforeEach(async () => {
await cleanupDatabase(env.DB);
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should return 401 without session", async () => {
const res = await api.request("/search/books?query=react", {}, env);
expect(res.status).toBe(401);
});
it("should return 400 for invalid query", async () => {
const user = await createTestUser(env.DB);
const sessionId = await createTestSession(env.KV, user.id);
const res = await api.request(
"/search/books?query=",
{
headers: { Cookie: `session_id=${sessionId}` },
},
env,
);
expect(res.status).toBe(400);
const body = (await res.json()) as ErrorResponseDto;
expect(body.success).toBe(false);
expect(body.error.code).toBe("VALIDATION_ERROR");
});
it("should return search results sorted by technical score and published date", async () => {
const user = await createTestUser(env.DB);
const sessionId = await createTestSession(env.KV, user.id);
const mockResponse = {
Items: [
{
Item: {
isbn: "9780000000001",
title: "Older React Book",
author: "Author A",
publisherName: "技術評論社",
salesDate: "2023年12月1日",
size: "200p",
itemCaption: "React book description",
largeImageUrl: "https://example.com/older.png",
affiliateUrl: "https://example.com/older",
},
},
{
Item: {
isbn: "9780000000002",
title: "Newer Novel",
author: "Author B",
publisherName: "Publisher B",
salesDate: "2024年1月2日",
size: "320p",
itemCaption: "Newer novel description",
largeImageUrl: "https://example.com/newer.png",
affiliateUrl: "https://example.com/newer",
},
},
],
pageCount: 1,
hits: 2,
};
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(JSON.stringify(mockResponse), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
),
);
const res = await api.request(
"/search/books?query=react&limit=5&page=1",
{
headers: { Cookie: `session_id=${sessionId}` },
},
env,
);
expect(res.status).toBe(200);
const body = (await res.json()) as SuccessResponseDto<SearchBooksResponseDto>;
expect(body.success).toBe(true);
expect(body.data.results).toHaveLength(2);
expect(body.data.results[0].title).toBe("Older React Book");
expect(body.data.results[0].publishedDate).toBe("2023年12月1日");
expect(body.data.results[0].techScore).toBeGreaterThan(body.data.results[1].techScore);
expect(body.data.results[0].scoreReasons).toBeUndefined();
expect(body.data.hits).toBe(2);
expect(body.data.pageCount).toBe(1);
});
it.each(["1", "true"])("should include score reasons when debug=%s", async (debug) => {
const user = await createTestUser(env.DB);
const sessionId = await createTestSession(env.KV, user.id);
const mockResponse = {
Items: [
{
Item: {
isbn: "9780000000003",
title: "Docker入門",
author: "Author C",
publisherName: "翔泳社",
salesDate: "2024年1月1日",
size: "240p",
itemCaption: "Linuxとコンテナの基礎",
largeImageUrl: "",
affiliateUrl: "",
},
},
],
pageCount: 1,
hits: 1,
};
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(JSON.stringify(mockResponse), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
),
);
const res = await api.request(
`/search/books?query=docker&debug=${debug}`,
{
headers: { Cookie: `session_id=${sessionId}` },
},
env,
);
expect(res.status).toBe(200);
const body = (await res.json()) as {
success: boolean;
data: {
results: Array<{
techScore: number;
scoreReasons?: Array<{ type: string; label: string; score: number }>;
}>;
};
};
expect(body.success).toBe(true);
expect(body.data.results[0].techScore).toBeGreaterThan(0);
expect(body.data.results[0].scoreReasons).toEqual(
expect.arrayContaining([
{ type: "tech_publisher", label: "翔泳社", score: 30 },
{ type: "title_keyword", label: "Docker", score: 15 },
]),
);
});
it("should return 400 for invalid isbn", async () => {
const user = await createTestUser(env.DB);
const sessionId = await createTestSession(env.KV, user.id);
const res = await api.request(
"/search/isbn/123",
{
headers: { Cookie: `session_id=${sessionId}` },
},
env,
);
expect(res.status).toBe(400);
const body = (await res.json()) as ErrorResponseDto;
expect(body.success).toBe(false);
expect(body.error.code).toBe("VALIDATION_ERROR");
expect(body.error.details).toContain("isbn must be matched");
});
it("should return null when ISBN search has no results", async () => {
const user = await createTestUser(env.DB);
const sessionId = await createTestSession(env.KV, user.id);
const mockResponse = { Items: [], pageCount: 0, hits: 0 };
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(JSON.stringify(mockResponse), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
),
);
const res = await api.request(
"/search/isbn/9784873119038",
{
headers: { Cookie: `session_id=${sessionId}` },
},
env,
);
expect(res.status).toBe(200);
const body = (await res.json()) as SuccessResponseDto<SearchBookByIsbnResponseDto>;
expect(body.success).toBe(true);
expect(body.data).toBeNull();
});
});