Files
memento-frontend/tests/e2e/memento-api-mode.spec.ts

141 lines
3.5 KiB
TypeScript

import { expect, test } from "@playwright/test";
test("development profile composes Memento HTTP capability instead of local mock", async ({
context,
page,
}) => {
await context.addCookies([
{
name: "XSRF-TOKEN",
value: "memento-e2e-csrf",
url: "http://127.0.0.1:4176/",
},
]);
const requests: Array<Readonly<{
method: string;
path: string;
headers: Readonly<Record<string, string>>;
}>> = [];
await page.route("**/app-api/v1/**", async (route) => {
const request = route.request();
const url = new URL(request.url());
const headers = await request.allHeaders();
requests.push({
method: request.method(),
path: `${url.pathname}${url.search}`,
headers,
});
if (
request.method() === "GET" &&
url.pathname === "/app-api/v1/memories"
) {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({ items: [], nextCursor: null }),
});
return;
}
if (
request.method() === "GET" &&
url.pathname === "/app-api/v1/memory-candidates"
) {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({
items: [
{
id: "candidate-api-e2e",
title: "제주, 여름의 끝",
mediaCount: 84,
placeCount: 4,
momentCount: 3,
},
],
nextCursor: null,
}),
});
return;
}
if (
request.method() === "POST" &&
url.pathname === "/app-api/v1/albums"
) {
await route.fulfill({
status: 201,
contentType: "application/json",
body: JSON.stringify({
id: "album-api-e2e",
title: "새로운 Album",
version: 1,
}),
});
return;
}
if (
request.method() === "GET" &&
url.pathname === "/app-api/v1/albums"
) {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({ items: [], nextCursor: null }),
});
return;
}
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({}),
});
});
await page.goto("/memories");
const runtime = await page.evaluate(async () =>
(await fetch("/config.json")).json() as Promise<{
APP_ENV: string;
AUTH_MODE: string;
API_BASE_URL: string;
}>,
);
expect(runtime.APP_ENV).toBe("development");
expect(runtime.AUTH_MODE).toBe("external");
expect(runtime.API_BASE_URL).toBe("http://127.0.0.1:4176/");
await expect.poll(() =>
requests.some(
(request) =>
request.method === "GET" &&
request.path.startsWith("/app-api/v1/memories?"),
),
).toBe(true);
await expect.poll(() =>
requests.some(
(request) =>
request.method === "GET" &&
request.path === "/app-api/v1/memory-candidates?status=SUGGESTED",
),
).toBe(true);
await page.locator("[data-create-album]").first().click();
await page.getByRole("button", { name: "Album 만들기" }).click();
await expect(page).toHaveURL(/\/albums$/);
const command = requests.find(
(request) =>
request.method === "POST" &&
request.path === "/app-api/v1/albums",
);
expect(command).toBeDefined();
expect(command?.headers["x-xsrf-token"]).toBe("memento-e2e-csrf");
expect(command?.headers["idempotency-key"]).toMatch(
/^[0-9a-f]{8}-[0-9a-f-]{27}$/i,
);
});