import { describe, expect, it, vi } from "vitest";

vi.mock("../internal/prompt-runtime.js", () => ({
  resolveTextPrompt: vi.fn(async () => ({ text: "远端系统 prompt" })),
}));

import {
  buildOfflineBatchCustomId,
  buildOfflineBatchRequestLines,
  parseOfflineBatchCustomId,
} from "./offline-batch-request.ts";

describe("buildOfflineBatchRequestLines", () => {
  it("uses prompt text returned by prompt store runtime", async () => {
    const lines = await buildOfflineBatchRequestLines({
      taskId: "11111111-1111-4111-8111-111111111111",
      model: "glm-5.1",
      sources: [
        {
          id: "22222222-2222-4222-8222-222222222222",
          batch_id: "33333333-3333-4333-8333-333333333333",
          material_name: "不锈钢护栏",
          specification: "304 立柱式",
          brand: "",
          quantity: 20,
          unit: "米",
          remarks: "",
          technical_standard: "",
          material_requirement: "304 不锈钢",
          technical_requirement: "",
          applicable_conditions: "",
          test_requirement: "",
          acceptance_note: "",
          warranty_period: "",
          deadline: "",
          invoice_type: "专票",
          delivery_address: "上海",
          source_file: "demo.xlsx",
          created_at: "2026-04-20T00:00:00.000Z",
          created_by: "",
        },
      ],
    });

    expect(lines).toHaveLength(1);
    expect(lines[0]?.body.messages[0]?.content).toBe("远端系统 prompt");
    expect(lines[0]?.body.messages[1]?.content).toContain("名称：不锈钢护栏");
  });
});

describe("offline batch custom id", () => {
  it("builds a provider-safe custom id and parses it back", () => {
    const taskId = "1920c4e3-911b-4d74-bfe6-e3482346869f";
    const materialId = "7b8522c5-f4e1-402c-ae39-49adca27ff74";

    const customId = buildOfflineBatchCustomId(taskId, materialId);

    expect(customId.length).toBeLessThanOrEqual(64);
    expect(parseOfflineBatchCustomId(customId)).toEqual({ taskId, materialId });
  });

  it("keeps parsing legacy taskId:materialId custom ids", () => {
    const legacyCustomId = "1920c4e3-911b-4d74-bfe6-e3482346869f:7b8522c5-f4e1-402c-ae39-49adca27ff74";

    expect(parseOfflineBatchCustomId(legacyCustomId)).toEqual({
      taskId: "1920c4e3-911b-4d74-bfe6-e3482346869f",
      materialId: "7b8522c5-f4e1-402c-ae39-49adca27ff74",
    });
  });
});