import { describe, expect, it } from "vitest";
import { SEARCH_REQUEST_TRANSPORT_CONTRACT, buildSearchRequest } from "./request";

describe("buildSearchRequest", () => {
  it("combines标题过滤与时间范围", () => {
    const payload = buildSearchRequest({
      workspaceId: "ws-1",
      activeDocumentId: "doc-1",
      query: "demo",
      filters: {
        titleOnly: true,
        exact: false,
        onlyCurrentPage: false,
        includeOcr: false,
        timeField: "updated",
        customRange: undefined,
      },
      timeRange: "7d",
    });

    expect(payload).not.toBeNull();
    expect(payload?.filters.titleOnly).toBe(true);
    expect(payload?.filters.timeRange).toBe("7d");
  });

  it("自动重置仅当前页面过滤条件", () => {
    const payload = buildSearchRequest({
      workspaceId: "ws-2",
      activeDocumentId: null,
      query: "",
      filters: {
        titleOnly: false,
        exact: false,
        onlyCurrentPage: true,
        includeOcr: false,
        timeField: "created",
        customRange: { from: "2025-02-01", to: "2025-02-07" },
      },
      timeRange: "any",
    });

    expect(payload?.filters.onlyCurrentPage).toBe(false);
    expect(payload?.documentId).toBeUndefined();
  });

  it("固定搜索请求将由 Rust Web search.documents transport 拥有", () => {
    const payload = buildSearchRequest({
      workspaceId: "ws-3",
      activeDocumentId: "doc-3",
      query: "Rust Web",
      filters: {
        titleOnly: false,
        exact: true,
        onlyCurrentPage: true,
        includeOcr: true,
        timeField: "updated",
        customRange: undefined,
      },
      timeRange: "any",
    });

    expect(payload).toMatchObject({
      workspaceId: "ws-3",
      documentId: "doc-3",
      query: "Rust Web",
      filters: {
        exact: true,
        includeOcr: true,
        onlyCurrentPage: true,
      },
    });
  });
});


describe("SEARCH_REQUEST_TRANSPORT_CONTRACT", () => {
  it("搜索请求 contract 固定为 Rust Web transport", () => {
    expect(SEARCH_REQUEST_TRANSPORT_CONTRACT).toEqual({
      shellOwner: "mnote-web",
      queryName: "search.documents",
      endpoint: "/api/search/documents",
      runtimeRole: "rust_web_transport",
    });
  });
});
