import test from "node:test";
import assert from "node:assert/strict";

import { normalizeSettings } from "../shared/settings.js";
import { parseSettings } from "../shared/schema.js";

test("normalizeSettings applies sane defaults and trims inputs", () => {
  const settings = normalizeSettings({
    openaiApiKey: "  sk-demo  ",
    openaiBaseUrl: "https://example.com/v1///",
    openaiModel: "  gpt-test  ",
    systemPrompt: "  hello  ",
    fontScale: "giant",
    screenshotMode: "viewport",
    includePageContext: undefined,
    includeScreenshot: false,
    debugMode: "yes"
  });

  assert.equal(settings.openaiApiKey, "sk-demo");
  assert.equal(settings.openaiBaseUrl, "https://example.com/v1");
  assert.equal(settings.openaiModel, "gpt-test");
  assert.equal(settings.systemPrompt, "hello");
  assert.equal(settings.fontScale, "small");
  assert.equal(settings.screenshotMode, "viewport");
  assert.equal(settings.includePageContext, true);
  assert.equal(settings.includeScreenshot, false);
  assert.equal(settings.debugMode, false);
});

test("parseSettings recovers from invalid storage payload", () => {
  const settings = parseSettings({
    openaiBaseUrl: "",
    openaiModel: "",
    systemPrompt: "",
    fontScale: "bad",
    screenshotMode: "bad",
    includePageContext: false,
    includeScreenshot: true,
    debugMode: true
  });

  assert.equal(settings.openaiBaseUrl, "https://api.aixhan.com/v1");
  assert.equal(settings.openaiModel, "gpt-5.4");
  assert.ok(settings.systemPrompt.includes("Sensa基础版"));
  assert.equal(settings.fontScale, "small");
  assert.equal(settings.screenshotMode, "fullpage");
  assert.equal(settings.includePageContext, false);
  assert.equal(settings.includeScreenshot, true);
  assert.equal(settings.debugMode, true);
});
