import { describe, expect, it } from "vitest";
import { toUpdatedTimeRange } from "../documentSearch";

describe("Q2-F datetime-local到UTC查询边界", () => {
  it("按浏览器本地时间解析并转换为UTC Z格式，而不是直接拼接Z", () => {
    const localValue = "2026-08-04T09:30";
    const result = toUpdatedTimeRange(localValue, "");
    expect(result.error).toBeNull();
    expect(result.query.updatedFrom).toBe(new Date(localValue).toISOString());
    expect(result.query.updatedFrom).toMatch(/Z$/);
    expect(result.query.updatedFrom).not.toBe(`${localValue}Z`);
    expect(result.query).not.toHaveProperty("updatedTo");
  });

  it("支持仅结束时间和闭区间双边时间", () => {
    const onlyTo = toUpdatedTimeRange("", "2026-08-04T18:00");
    expect(onlyTo.query).toEqual({
      updatedTo: new Date("2026-08-04T18:00").toISOString(),
    });

    const both = toUpdatedTimeRange("2026-08-04T09:00", "2026-08-04T09:00");
    expect(both.error).toBeNull();
    expect(both.query.updatedFrom).toBe(both.query.updatedTo);
  });

  it("空条件不产生空字符串参数", () => {
    const result = toUpdatedTimeRange("", "");
    expect(result).toEqual({
      query: {},
      error: null,
    });
    expect(Object.values(result.query)).not.toContain("");
  });

  it("反向区间和非法时间返回中文错误且不生成查询边界", () => {
    expect(
      toUpdatedTimeRange("2026-08-04T10:00", "2026-08-04T09:00"),
    ).toEqual({ query: {}, error: "更新时间从不能晚于更新时间至" });
    expect(toUpdatedTimeRange("not-a-date", "")).toEqual({
      query: {},
      error: "“更新时间从”不是有效时间",
    });
  });
});
