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

test("chat list uses downward pull-to-refresh", async () => {
  const panel = await readFile(new URL("../features/chats/ChatsPanel.jsx", import.meta.url), "utf8");
  const app = await readFile(new URL("../App.jsx", import.meta.url), "utf8");

  assert.match(panel, /usePullToRefresh\(\{ direction: "down", onRefresh, mouse: false \}\)/);
  assert.match(panel, /<PullToRefreshIndicator pull=\{pull\} direction="down"/);
  assert.match(panel, /className="thread-list"[^>]*\{\.\.\.pullHandlers\}/);
  assert.match(app, /onRefresh=\{chats\.reload\}/);
});

test("conversation does not expose pull-to-refresh", async () => {
  const panel = await readFile(new URL("../features/conversation/ConversationPanel.jsx", import.meta.url), "utf8");
  const app = await readFile(new URL("../App.jsx", import.meta.url), "utf8");
  const hook = await readFile(new URL("../features/conversation/useConversation.js", import.meta.url), "utf8");

  assert.doesNotMatch(panel, /usePullToRefresh|PullToRefreshIndicator|pullHandlers|onRefresh/);
  assert.doesNotMatch(app, /onRefresh=\{conversation\.refresh\}/);
  assert.doesNotMatch(hook, /readSessionSnapshot|async function refresh\(\)/);
});

test("pull indicators overlay content without moving fixed chrome", async () => {
  const [indicator, css] = await Promise.all([
    readFile(new URL("./PullToRefreshIndicator.jsx", import.meta.url), "utf8"),
    readFile(new URL("./pull-to-refresh.css", import.meta.url), "utf8")
  ]);

  assert.match(indicator, /pull\.active \? "pulling"/);
  assert.match(indicator, /"--pull-progress": Math\.min\(pull\.distance \/ PULL_REFRESH_THRESHOLD, 1\)/);
  assert.match(css, /\.pull-refresh-indicator\{[^}]*position:absolute/);
  assert.match(css, /\.pull-refresh-indicator\.down\{[^}]*top:/);
  assert.match(css, /\.pull-refresh-indicator\.up\{[^}]*bottom:/);
  assert.match(css, /\.pull-refresh-indicator\.pulling\{[^}]*transition:opacity/);
  assert.match(css, /rotate\(calc\(-90deg \+ var\(--pull-progress\) \* 270deg\)\)/);
  assert.doesNotMatch(css, /\.pull-refresh-indicator\[aria-hidden="true"\]\{[^}]*visibility:hidden/);
  assert.match(css, /\.pull-refresh-indicator\.refreshing[^}]*animation:/);
});
