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

test("status panel applies remaining-limit tones and shows chat account only inside a chat", async () => {
  const [panel, css, app] = await Promise.all([
    readFile(new URL("./StatusPanel.jsx", import.meta.url), "utf8"),
    readFile(new URL("./status.css", import.meta.url), "utf8"),
    readFile(new URL("../../App.jsx", import.meta.url), "utf8")
  ]);

  assert.match(panel, /rateLimitTone/);
  assert.match(panel, /"primary"/);
  assert.match(panel, /"weekly"/);
  assert.match(panel, /usage-percent/);
  assert.match(panel, /usage-reset/);
  assert.doesNotMatch(panel, /useState|aria-expanded|formatExpandedResetDetail|usage-reset-detail|onClick/);
  assert.match(panel, /formatUsageError/);
  assert.match(panel, /error=\{status\.error\}/);
  const hook = await readFile(new URL("./useStatus.js", import.meta.url), "utf8");
  assert.match(hook, /reduceUsageStatus/);
  assert.doesNotMatch(hook, /error:\s*error\.message/);
  assert.match(css, /\.usage-good \.usage-percent\{[^}]*color:/s);
  assert.match(css, /\.usage-warning \.usage-percent\{[^}]*color:/s);
  assert.match(css, /\.usage-low \.usage-percent\{[^}]*color:/s);
  assert.doesNotMatch(css, /\.usage-good \.usage-reset\{[^}]*color:/s);
  assert.doesNotMatch(css, /\.usage-status\.expanded|\.usage-reset-detail|cursor:pointer/);
  assert.doesNotMatch(panel, /resets in|resets/i);
  assert.match(panel, /t\("status\.lastAccount"\)/);
  assert.match(panel, /export function ConversationAccountBar/);
  assert.match(panel, /className="chat-account-row"/);
  assert.doesNotMatch(panel, /showConversationAccount|last-conversation-account/);
  assert.match(panel, /connectionError &&/);
  assert.match(panel, /status-connection-error/);
  assert.match(app, /<ConversationAccountBar session=\{visibleSession\} accounts=\{app\.accounts\} accountMosaic=\{accountMosaic\} \/>/);
  assert.match(app, /connectionError=\{connectionMessageKey \? t\(connectionMessageKey\) : ""\}/);
  assert.match(css, /\.chat-account-row\{[^}]*position:absolute[^}]*top:0[^}]*height:27px[^}]*box-shadow:inset 0 -1px #2b2d33[^}]*animation:/);
  assert.doesNotMatch(css, /\.chat-account-row\{[^}]*box-shadow:inset 0 1px #2b2d33/);
  assert.match(css, /@keyframes chat-account-row-in/);
  assert.match(css, /html\[data-theme="light"\] \.app-shell \.chat-account-row\{[^}]*box-shadow:inset 0 -1px #d0d7de!important/);
  assert.doesNotMatch(css, /last-conversation-account/);
  assert.match(app, /motion-screen[^]*chat-account-offset/);
  assert.match(css, /\.status-connection-error\{[^}]*color:#ff7b86[^}]*font-weight:600[^}]*white-space:normal[^}]*-webkit-line-clamp:2/s);
});
