2026-11-05 13:06:45 +07:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
|
|
|
|
|
import { openApiSpec } from "@/lib/openapi";
|
|
|
|
|
|
|
|
|
|
// spec ถูก inline เป็น JSON ลงหน้า Swagger UI ตรง ๆ (app/api/swagger/route.ts) —
|
|
|
|
|
// $ref ที่ชี้หาย (แก้ชื่อ schema ใน lib/openapi.ts แล้วลืมแก้อีกฝั่ง) จะไม่มี error
|
|
|
|
|
// ตอน build เพราะเป็นแค่ string กลาง object literal ที่ serialize ได้ปกติ — จับได้ที่นี่ก่อน
|
|
|
|
|
// เห็นเป็นช่องว่าง ๆ บนหน้า /api/swagger
|
|
|
|
|
|
|
|
|
|
function collectRefs(node: unknown, out: string[] = []): string[] {
|
|
|
|
|
if (Array.isArray(node)) {
|
|
|
|
|
for (const item of node) collectRefs(item, out);
|
|
|
|
|
} else if (node !== null && typeof node === "object") {
|
|
|
|
|
for (const [key, value] of Object.entries(node)) {
|
|
|
|
|
if (key === "$ref" && typeof value === "string") out.push(value);
|
|
|
|
|
else collectRefs(value, out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** แกะ JSON pointer (#/a/b/c) — null = ชี้ไปที่ไม่มีจริง */
|
|
|
|
|
function resolveRef(spec: object, ref: string): unknown {
|
|
|
|
|
if (!ref.startsWith("#/")) return null;
|
|
|
|
|
let node: unknown = spec;
|
|
|
|
|
for (const part of ref.slice(2).split("/")) {
|
|
|
|
|
if (node === null || typeof node !== "object") return null;
|
|
|
|
|
const key = part.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
|
|
|
node = (node as Record<string, unknown>)[key];
|
|
|
|
|
if (node === undefined) return null;
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("openApiSpec", () => {
|
|
|
|
|
it("ทุก $ref ใน spec ชี้ถึงจุดที่มีจริง", () => {
|
|
|
|
|
const refs = collectRefs(openApiSpec);
|
|
|
|
|
expect(refs.length).toBeGreaterThan(0);
|
|
|
|
|
const broken = refs.filter((ref) => resolveRef(openApiSpec, ref) === null);
|
|
|
|
|
expect(broken).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-09 14:30:29 +07:00
|
|
|
it("กลุ่ม auth: /api/auth/* ครบทั้งคู่ + /api/documents/token (alias กลุ่ม docver) — เส้นของฝั่ง note ถอดแล้ว", () => {
|
2026-11-05 13:06:45 +07:00
|
|
|
const paths = openApiSpec.paths as Record<string, unknown>;
|
|
|
|
|
expect(paths["/api/auth/token"]).toBeDefined();
|
|
|
|
|
expect(paths["/api/auth/session"]).toBeDefined();
|
|
|
|
|
expect(paths["/api/documents/token"]).toBeDefined();
|
2026-11-09 14:30:29 +07:00
|
|
|
expect(paths["/api/note/auth/token"]).toBeUndefined();
|
|
|
|
|
expect(paths["/api/note/auth/session"]).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("ไม่มีเส้น /api/note/auth/* (ถอดแล้ว 2026-11-09) หลุดค้างใน paths หรือ description ใด ๆ ของ spec", () => {
|
|
|
|
|
// JSON.stringify ทั้ง spec ครอบทุก description/example — จับชื่อเส้นตายได้ทุกจุด
|
|
|
|
|
// (regression จาก code review 2026-11-09 รอบสอง: คู่มือ next-step ของ stage route เคยชี้เส้นตาย)
|
|
|
|
|
expect(JSON.stringify(openApiSpec)).not.toContain("/api/note/auth/");
|
2026-11-05 13:06:45 +07:00
|
|
|
});
|
|
|
|
|
});
|