eCourt-note/tests/lib/openapi.test.ts
Your Name 5121970f31
Some checks failed
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Failing after 30s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Has been skipped
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Failing after 1s
refactor(auth): consolidate auth under /api/auth/* endpoints
- ยุบ route auth ที่เคย copy กันจน drift (flow N11 หายจาก /api/auth/session)
  เหลือโค้ด inline สองเส้นเดียว: POST /api/auth/token (ทางเข้า 3: x-api-key
  docver / x-api-key purpose:"login" mint ลิงก์ note / ticket ของ add-in)
  + POST /api/auth/session (แลก credential + register N8 + N11 fid-only)
- POST /api/documents/token คงไว้เป็น alias re-export (กลุ่ม docver/e-Court
  เดิม — contract เดิมเป๊ะ, มี test toBe กัน copy แยกชุดกลับมา)
- ถอดเส้นฝั่ง note ทั้งสอบ: /api/note/auth/token + /api/note/auth/session
  (e-Court ต้องย้ายมาทางเข้ารวม — แจ้งทีม e-Court ก่อน deploy)
- utils/auth-store.ts: primitives กลางฝั่ง client (jwtExp/isExpired/
  localStorage store + memory fallback) — lib/note-session ใช้ร่วม
  (expMs decode จาก JWT payload = นาฬิกา server) และวางไว้ให้ wordaddin
  ย้ายมาใช้รอบหน้าได้ (รอบนี้ไม่แต้ wordaddin ตามข้อตกลง)
- นโยบาย token หมดอายุกลาง session: PDFViewer reportActionError จับ
  auth-error ของ Server Action ทุกจุด → ล้าง session แล้วพาไป
  /note?sso=expired (จบที่ gate เดียวกับ bootstrap 401)
- openapi: ทางเข้า purpose:"login" + schema LoginTokenRequest + ลบ path
  เส้นที่ถอด + test กันเส้นตายหลุดค้างใน spec
- เอกสาร sync: PLAN-SSO §0.6, CLAUDE.md, PLAN-STAGED, .env.example, /note/dev
- tests: ย้าย/เพิ่มชุด auth (50 เคสกลุ่ม auth ผ่าน, ทั้งชุด 444 ผ่าน) +
  e2e ครบทั้ง API (mint/alias/ticket/404 เส้นเก่า/N11/D4) และ browser

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-09 14:30:29 +07:00

57 lines
3 KiB
TypeScript

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([]);
});
it("กลุ่ม auth: /api/auth/* ครบทั้งคู่ + /api/documents/token (alias กลุ่ม docver) — เส้นของฝั่ง note ถอดแล้ว", () => {
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();
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/");
});
});