import { describe, it, expect } from "vitest";
import { _internal, buildSystemPrompt } from "./provider";
import { buildChart } from "@/lib/tuvi/engine";

describe("AI provider", () => {
  it("buildSystemPrompt includes chart info", () => {
    const chart = buildChart({
      date: "1990-3-15",
      timeIndex: 0,
      gender: "Nam",
      calendar: "solar",
      fullName: "Nguyễn Văn A",
    });
    const prompt = buildSystemPrompt(chart);
    expect(prompt).toContain("Văn Khúc");
    expect(prompt).toContain("Thổ Ngũ Cục");
    expect(prompt).toContain("Mệnh (Kỷ Mão)");
    expect(prompt).toContain("Nguyễn Văn A");
    expect(prompt).toContain("tiếng Việt");
  });

  it("pickChapter heuristic", () => {
    expect(_internal.pickChapter("năm nay tình duyên ra sao?")).toBe("tình duyên");
    expect(_internal.pickChapter("sự nghiệp tôi thế nào?")).toBe("sự nghiệp");
    expect(_internal.pickChapter("năm nay kiếm tiền dễ không?")).toBe("tài lộc");
    expect(_internal.pickChapter("sức khỏe tôi sao?")).toBe("sức khỏe");
    expect(_internal.pickChapter("hôm nay thế nào?")).toBe("tổng quan vận mệnh");
  });

  it("MockProvider yields a non-empty stream of chunks", async () => {
    const chart = buildChart({
      date: "1990-3-15",
      timeIndex: 0,
      gender: "Nam",
      calendar: "solar",
    });
    const provider = new _internal.MockProvider();
    const chunks: string[] = [];
    for await (const c of provider.stream({
      chart,
      question: "Năm 2026 sự nghiệp tôi ra sao?",
    })) {
      chunks.push(c);
      if (chunks.length > 200) break; // safety
    }
    const full = chunks.join("");
    expect(chunks.length).toBeGreaterThan(5);
    expect(full).toContain("sự nghiệp");
    expect(full).toContain("[Phản hồi mock");
  });
});
