import assert from "node:assert/strict";
import test from "node:test";
import { readFile } from "node:fs/promises";

const { default: worker } = await import("../dist/server/index.js");
const env = { ASSETS: { fetch: async () => new Response("Not found", { status: 404 }) } };
const ctx = { waitUntil() {}, passThroughOnException() {} };
test("SSR contains the complete snapshot before any browser JavaScript or upstream request", async () => {
  const snapshot = JSON.parse(await readFile(new URL("../lib/liquidity-snapshot.json", import.meta.url), "utf8"));
  const response = await worker.fetch(new Request("http://localhost/", { headers: { accept: "text/html" } }), env, ctx);
  const html = await response.text();
  assert.equal(response.status, 200);
  assert.match(html, /Correlated pairs/);
  assert.match(html, /SNAPSHOT/);
  assert.doesNotMatch(html, /codex-preview/);
  for (const node of snapshot.nodes) assert.ok(html.includes(node.symbol), `Missing ${node.symbol}`);
  assert.match(html, /amm-correlated-pairs-liquidity\.git/);
  assert.match(html, /Chain/);
  assert.match(html, /Ethereum L1/);
  assert.match(html, /Base/);
  assert.match(html, /summary_large_image/);
  assert.match(html, /property="og:image"[^>]*\/og\.png/);
  assert.match(html, /Hayden’s Post on AMM Correlated Pairs/);
});
test("localized HTML, canonical URL, and unknown routes are correct without hydration", async () => {
  const response = await worker.fetch(new Request("http://localhost/zh"), env, ctx);
  const html = await response.text();
  assert.equal(response.status, 200);
  assert.match(html, /lang="zh-CN"/);
  assert.match(html, /相关交易对/);
  assert.match(html, /rel="canonical"[^>]*\/zh/);
  const missing = await worker.fetch(new Request("http://localhost/nothing-here"), env, ctx);
  assert.equal(missing.status, 404);
  const wrongChain = await worker.fetch(new Request("http://localhost/api/liquidity?chain=solana"), env, ctx);
  assert.equal(wrongChain.status, 400);
});
test("upstream failure returns a complete HTTP 200 snapshot and does not block", async () => {
  const original = globalThis.fetch;
  globalThis.fetch = async () => new Response("rate limited", { status: 429 });
  const pending = [];
  try {
    const response = await worker.fetch(new Request("http://localhost/api/liquidity"), env, { ...ctx, waitUntil(p) { pending.push(p); } });
    const data = await response.json();
    assert.equal(response.status, 200);
    assert.equal(data.stale, true);
    assert.deepEqual(Object.keys(data.chains), ["robinhood", "ethereum", "base"]);
    for (const [id, snapshot] of Object.entries(data.chains)) { assert.equal(snapshot.chainId, id); assert.ok(snapshot.totals.pools >= 70); }
    assert.ok(data.totals.pools >= 70);
    assert.ok(data.nodes.length >= 30);
    assert.equal(response.headers.get("cache-control"), "no-store");
    await Promise.all(pending);
  } finally { globalThis.fetch = original; }
});


test("every shareable chain link renders its own data and contains the full three-chain bootstrap", async () => {
  for (const chain of ["ethereum", "base"]) {
    const response = await worker.fetch(new Request(`http://localhost/?chain=${chain}`), env, ctx);
    const html = await response.text();
    assert.equal(response.status, 200);
    assert.match(html, /summary_large_image/);
    assert.ok(html.includes(chain === "ethereum" ? "Fluid" : "Aerodrome"));
    for (const network of ["robinhood_", "eth_", "base_"]) assert.ok(html.includes(network));
  }
});
