|
| 1 | +/** |
| 2 | + * Harness Tests — booking |
| 3 | + * |
| 4 | + * Guards against regressions found during install simulation. |
| 5 | + * No browser, no network — runs in milliseconds. |
| 6 | + * |
| 7 | + * Lessons encoded here: |
| 8 | + * - isLoggedIn must return FALSE on an unauthenticated page (never use body-content heuristics) |
| 9 | + * - accountMenu/auth selectors must NOT match public page elements |
| 10 | + * - package.json must include "files" → dist or new users get source-only packages |
| 11 | + * - repository.url required for npm provenance publishing |
| 12 | + * - prepublishOnly ensures build runs before every publish |
| 13 | + */ |
| 14 | +import { describe, it, expect } from "vitest"; |
| 15 | +import adapter from "../src/index.js"; |
| 16 | +import { readFileSync } from "node:fs"; |
| 17 | +import { fileURLToPath } from "node:url"; |
| 18 | +import path from "node:path"; |
| 19 | + |
| 20 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 21 | +const pkg = JSON.parse(readFileSync(path.join(__dirname, "../package.json"), "utf8")); |
| 22 | + |
| 23 | +// ── package.json structural guards ──────────────────────────────────────────── |
| 24 | + |
| 25 | +describe("package.json harness guards", () => { |
| 26 | + it('has "files" field containing "dist"', () => { |
| 27 | + expect(pkg.files, 'Missing "files" in package.json — dist/ will be excluded from npm publish').toBeDefined(); |
| 28 | + expect(pkg.files).toContain("dist"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("has repository.url — required for npm provenance publishing", () => { |
| 32 | + expect(pkg.repository?.url, 'Missing repository.url — npm provenance will reject the publish').toBeTruthy(); |
| 33 | + expect(pkg.repository.url).toContain("github.com"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("has prepublishOnly script — ensures build runs before every publish", () => { |
| 37 | + expect(pkg.scripts?.prepublishOnly, 'Missing prepublishOnly — packages may publish without compiling').toBeTruthy(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +// ── Minimal Page mock (no browser, no network) ──────────────────────────────── |
| 42 | + |
| 43 | +/** |
| 44 | + * isLoggedIn contract: |
| 45 | + * - MUST return false on an unauthenticated page |
| 46 | + * - MUST return false on the login page |
| 47 | + * - MUST return true when the account/nav element is present |
| 48 | + * |
| 49 | + * This was violated in the Booking adapter (bodyText.length > 100 heuristic) |
| 50 | + * causing health_check to report loggedIn=true for unauthenticated sessions. |
| 51 | + */ |
| 52 | +function makeMockPage(url: string, hasAuthElement = false) { |
| 53 | + return { |
| 54 | + url: () => url, |
| 55 | + locator: (sel: string) => { |
| 56 | + const found = hasAuthElement && sel.includes("account-menu"); |
| 57 | + return { |
| 58 | + count: async () => (found ? 1 : 0), |
| 59 | + isVisible: async () => (found ? true : false), |
| 60 | + first: () => ({ |
| 61 | + isVisible: async (_opts?: unknown) => (found ? true : false), |
| 62 | + click: async () => {}, |
| 63 | + }), |
| 64 | + }; |
| 65 | + }, |
| 66 | + evaluate: async (_fn: unknown) => "", |
| 67 | + waitForTimeout: async () => {}, |
| 68 | + goto: async (_url: string) => null, |
| 69 | + waitForSelector: async () => null, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +describe("isLoggedIn contract — unauthenticated", () => { |
| 74 | + it("returns false on adapter domain with no auth elements present", async () => { |
| 75 | + const page = makeMockPage(`https://${adapter.domain}/`, false); |
| 76 | + expect(await adapter.isLoggedIn(page as never)).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns false on the login URL", async () => { |
| 80 | + const page = makeMockPage(adapter.loginUrl, false); |
| 81 | + expect(await adapter.isLoggedIn(page as never)).toBe(false); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("isLoggedIn contract — authenticated", () => { |
| 86 | + it("returns true when the auth nav element is present", async () => { |
| 87 | + const page = makeMockPage(`https://${adapter.domain}/`, true); |
| 88 | + expect(await adapter.isLoggedIn(page as never)).toBe(true); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +// ── Tool registry ───────────────────────────────────────────────────────────── |
| 93 | + |
| 94 | +describe("tool registry", () => { |
| 95 | + it("tools() returns a non-empty array", () => { |
| 96 | + expect(adapter.tools().length).toBeGreaterThan(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("every tool has a non-empty name and description", () => { |
| 100 | + for (const tool of adapter.tools()) { |
| 101 | + expect(tool.name.length, `tool missing name`).toBeGreaterThan(0); |
| 102 | + expect(tool.description?.length ?? 0, `"${tool.name}" missing description`).toBeGreaterThan(10); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + it("every tool has a handler", () => { |
| 107 | + for (const tool of adapter.tools()) { |
| 108 | + expect(typeof tool.handler, `"${tool.name}" missing handler`).toBe("function"); |
| 109 | + } |
| 110 | + }); |
| 111 | +}); |
0 commit comments