|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +// Source-level guard for fully qualified (LTS) versions in link-hextra. |
| 6 | +// |
| 7 | +// link-hextra infers the target version by regex-matching the page's permalink. |
| 8 | +// The regexes originally accepted only `X.Y.x` (2.3.x), `latest`, and `main`. |
| 9 | +// When a product ships a fully qualified LTS tree (e.g. |
| 10 | +// `/agentgateway/2026.7.1/...`), inference fell through to the "latest" |
| 11 | +// fallback: every reuse-nested link on those pages pointed at `/latest/...` |
| 12 | +// instead of the LTS tree, and the build emitted a `link-hextra called with no |
| 13 | +// version` WARN for each one (which hugo-warnings.spec.ts fails on). |
| 14 | +// |
| 15 | +// Why a SOURCE check, not a rendered-output check: the bundled fixture has no |
| 16 | +// fully qualified version tree, and adding one shifts the page URLs the rest of |
| 17 | +// the suite asserts on. So this extracts the version alternation from the |
| 18 | +// shipped shortcode and exercises it directly. Self-skips when the file isn't |
| 19 | +// at the module-relative path (a consumer build, where the module lives under |
| 20 | +// hugo_cache rather than ../layouts). |
| 21 | + |
| 22 | +const SHORTCODE = path.resolve( |
| 23 | + __dirname, |
| 24 | + "../layouts/_shortcodes/link-hextra.html", |
| 25 | +); |
| 26 | + |
| 27 | +// Strip Go/Hugo template comments (`{{- /* … */ -}}`) so the assertions match |
| 28 | +// ACTIVE code, not the explanatory comments (which also spell out version |
| 29 | +// patterns). |
| 30 | +function activeSrc(): string { |
| 31 | + return fs |
| 32 | + .readFileSync(SHORTCODE, "utf8") |
| 33 | + .replace(/\{\{-?\s*\/\*[\s\S]*?\*\/\s*-?\}\}/g, ""); |
| 34 | +} |
| 35 | + |
| 36 | +// The two version-inference regexes, as written in the template: |
| 37 | +// 1. product-prefixed, e.g. `/agentgateway/2026.7.1/security/waf/overview/` |
| 38 | +// 2. root-relative, e.g. `/2026.7.1/security/waf/overview/` (preview builds |
| 39 | +// whose baseURL path is the product) |
| 40 | +const PATTERNS: Array<{ label: string; extract: RegExp }> = [ |
| 41 | + { |
| 42 | + label: "product-prefixed", |
| 43 | + extract: /findRE\s+`((?:\(\?:)?kgateway[^`]*)`/, |
| 44 | + }, |
| 45 | + { |
| 46 | + label: "root-relative", |
| 47 | + extract: /findRE\s+`(\^\/\([^`]*)`/, |
| 48 | + }, |
| 49 | +]; |
| 50 | + |
| 51 | +// Versions that must infer, and the permalinks they appear in. |
| 52 | +const MUST_MATCH: Array<[string, string, string]> = [ |
| 53 | + ["2026.7.1", "/agentgateway/2026.7.1/security/waf/overview/", "/2026.7.1/security/waf/overview/"], |
| 54 | + ["2.3.x", "/agentgateway/2.3.x/security/waf/overview/", "/2.3.x/security/waf/overview/"], |
| 55 | + ["latest", "/agentgateway/latest/security/waf/overview/", "/latest/security/waf/overview/"], |
| 56 | + ["main", "/kgateway/main/security/waf/overview/", "/main/security/waf/overview/"], |
| 57 | +]; |
| 58 | + |
| 59 | +test.describe("link-hextra infers fully qualified (LTS) versions", () => { |
| 60 | + test.skip( |
| 61 | + !fs.existsSync(SHORTCODE), |
| 62 | + "link-hextra.html not at the module-relative path (consumer build)", |
| 63 | + ); |
| 64 | + |
| 65 | + for (const { label, extract } of PATTERNS) { |
| 66 | + test(`the ${label} version regex accepts X.Y.Z as well as X.Y.x`, () => { |
| 67 | + const src = activeSrc(); |
| 68 | + const m = src.match(extract); |
| 69 | + expect( |
| 70 | + m, |
| 71 | + `${label} version-inference \`findRE\` not found — the shortcode ` + |
| 72 | + "changed shape; re-check that fully qualified versions still infer.", |
| 73 | + ).not.toBeNull(); |
| 74 | + |
| 75 | + // Go's regexp syntax is RE2, but these patterns use only constructs JS |
| 76 | + // shares, so they can be exercised directly. |
| 77 | + const re = new RegExp(m![1]); |
| 78 | + const idx = label === "product-prefixed" ? 1 : 2; |
| 79 | + |
| 80 | + for (const row of MUST_MATCH) { |
| 81 | + const [version, ...permalinks] = row; |
| 82 | + const permalink = permalinks[idx - 1]; |
| 83 | + const hit = permalink.match(re); |
| 84 | + expect( |
| 85 | + hit?.[1], |
| 86 | + `\`${version}\` did not infer from ${permalink} — links on that ` + |
| 87 | + "version tree fall back to `latest` and the build WARNs.", |
| 88 | + ).toBe(version); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + test("the version alternation is not narrowed back to X.Y.x only", () => { |
| 94 | + const src = activeSrc(); |
| 95 | + // Both regexes must allow a numeric third segment. Guards against a |
| 96 | + // revert of one branch while the other keeps working. |
| 97 | + const narrowed = src.match(/findRE\s+`[^`]*\\d\+\\\.\\d\+\\\.x[|)]/g) ?? []; |
| 98 | + expect( |
| 99 | + narrowed, |
| 100 | + "a version-inference regex still accepts only `\\d+\\.\\d+\\.x` — " + |
| 101 | + "fully qualified LTS versions (e.g. 2026.7.1) will not infer.", |
| 102 | + ).toEqual([]); |
| 103 | + }); |
| 104 | +}); |
0 commit comments