Merge branch 'forgejo-dev' into newfeat

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-11-09 10:15:38 +07:00
commit c57a75e36a
50 changed files with 2760 additions and 961 deletions

View file

@ -75,8 +75,7 @@ describe("verifyAddinTicket", () => {
});
// verifyAddinTicketHmac = ตัวเดียวกันแต่ไม่ตรวจ exp — route ใช้ตรวจ HMAC ก่อน
// แล้วตัดสิน "ticket หมดอายุ + เอกสารนี้เคยทำงานโดย uid เดียวกัน" จาก DB เอง
// (requirement ขั้น 7 — แก้บั๊ก 2026-10-28)
// แล้วตัดสิน exp เองภายหลัง (ลายเซ็นถูกแต่หมดอายุ = 403 — contract 2026-11-04)
describe("verifyAddinTicketHmac", () => {
it("exp อดีต + HMAC ถูก = ผ่าน (ได้ ticket ครบ) — ต่างจาก verifyAddinTicket ที่ไม่ผ่าน", () => {
const expired = JSON.stringify({ exp: 1, fid: "f", uid: "u", uname: "n" });
@ -122,7 +121,7 @@ describe("parseTicketTokenBody", () => {
}
});
it("ขาด field (ไม่ส่ง/ว่าง/null) = missing (→ 401 กัน oracle)", () => {
it("ขาด field (ไม่ส่ง/ว่าง/null) = missing (→ 400 — ฟอร์แม็ตพัง, contract 2026-11-04)", () => {
for (const raw of [{}, { ticket_data: "" }, { HmacSha256: "y" }, { ticket_data: null, HmacSha256: "y" }]) {
const result = parseTicketTokenBody(raw);
expect(result.ok, JSON.stringify(raw)).toBe(false);

51
tests/lib/openapi.test.ts Normal file
View file

@ -0,0 +1,51 @@
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: path สั้น /api/auth/* ครบทั้งคู่ และ path เดิมที่เป็นต้นทางยังอยู่", () => {
const paths = openApiSpec.paths as Record<string, unknown>;
expect(paths["/api/auth/token"]).toBeDefined();
expect(paths["/api/auth/session"]).toBeDefined();
// path เดิม (ที่ /api/auth/* alias มา) ต้องยัง document อยู่
expect(paths["/api/documents/token"]).toBeDefined();
expect(paths["/api/note/auth/token"]).toBeDefined();
});
});