import { expect, it } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import type { ProviderPlugin, WebSearchProviderPlugin } from "../types.js";

export function installProviderPluginContractSuite(params: { provider: ProviderPlugin }) {
  it("satisfies the base provider plugin contract", () => {
    const { provider } = params;
    const authIds = provider.auth.map((method) => method.id);
    const wizardChoiceIds = new Set<string>();

    expect(provider.id).toMatch(/^[a-z0-9][a-z0-9-]*$/);
    expect(provider.label.trim()).not.toBe("");

    if (provider.docsPath) {
      expect(provider.docsPath.startsWith("/")).toBe(true);
    }
    if (provider.aliases) {
      expect(provider.aliases).toEqual([...new Set(provider.aliases)]);
    }
    if (provider.envVars) {
      expect(provider.envVars).toEqual([...new Set(provider.envVars)]);
      expect(provider.envVars.every((entry) => entry.trim().length > 0)).toBe(true);
    }

    expect(Array.isArray(provider.auth)).toBe(true);
    expect(authIds).toEqual([...new Set(authIds)]);
    for (const method of provider.auth) {
      expect(method.id.trim()).not.toBe("");
      expect(method.label.trim()).not.toBe("");
      if (method.hint !== undefined) {
        expect(method.hint.trim()).not.toBe("");
      }
      if (method.wizard) {
        if (method.wizard.choiceId) {
          expect(method.wizard.choiceId.trim()).not.toBe("");
          expect(wizardChoiceIds.has(method.wizard.choiceId)).toBe(false);
          wizardChoiceIds.add(method.wizard.choiceId);
        }
        if (method.wizard.methodId) {
          expect(authIds).toContain(method.wizard.methodId);
        }
        if (method.wizard.modelAllowlist?.allowedKeys) {
          expect(method.wizard.modelAllowlist.allowedKeys).toEqual([
            ...new Set(method.wizard.modelAllowlist.allowedKeys),
          ]);
        }
        if (method.wizard.modelAllowlist?.initialSelections) {
          expect(method.wizard.modelAllowlist.initialSelections).toEqual([
            ...new Set(method.wizard.modelAllowlist.initialSelections),
          ]);
        }
      }
      expect(typeof method.run).toBe("function");
    }

    if (provider.wizard?.setup || provider.wizard?.modelPicker) {
      expect(provider.auth.length).toBeGreaterThan(0);
    }
    if (provider.wizard?.setup) {
      if (provider.wizard.setup.choiceId) {
        expect(provider.wizard.setup.choiceId.trim()).not.toBe("");
        expect(wizardChoiceIds.has(provider.wizard.setup.choiceId)).toBe(false);
      }
      if (provider.wizard.setup.methodId) {
        expect(authIds).toContain(provider.wizard.setup.methodId);
      }
      if (provider.wizard.setup.modelAllowlist?.allowedKeys) {
        expect(provider.wizard.setup.modelAllowlist.allowedKeys).toEqual([
          ...new Set(provider.wizard.setup.modelAllowlist.allowedKeys),
        ]);
      }
      if (provider.wizard.setup.modelAllowlist?.initialSelections) {
        expect(provider.wizard.setup.modelAllowlist.initialSelections).toEqual([
          ...new Set(provider.wizard.setup.modelAllowlist.initialSelections),
        ]);
      }
    }
    if (provider.wizard?.modelPicker?.methodId) {
      expect(authIds).toContain(provider.wizard.modelPicker.methodId);
    }
  });
}

export function installWebSearchProviderContractSuite(params: {
  provider: WebSearchProviderPlugin;
  credentialValue: unknown;
}) {
  it("satisfies the base web search provider contract", () => {
    const { provider } = params;

    expect(provider.id).toMatch(/^[a-z0-9][a-z0-9-]*$/);
    expect(provider.label.trim()).not.toBe("");
    expect(provider.hint.trim()).not.toBe("");
    expect(provider.placeholder.trim()).not.toBe("");
    expect(provider.signupUrl.startsWith("https://")).toBe(true);
    if (provider.docsUrl) {
      expect(provider.docsUrl.startsWith("http")).toBe(true);
    }

    expect(provider.envVars).toEqual([...new Set(provider.envVars)]);
    expect(provider.envVars.every((entry) => entry.trim().length > 0)).toBe(true);

    const searchConfigTarget: Record<string, unknown> = {};
    provider.setCredentialValue(searchConfigTarget, params.credentialValue);
    expect(provider.getCredentialValue(searchConfigTarget)).toEqual(params.credentialValue);

    const config = {
      tools: {
        web: {
          search: {
            provider: provider.id,
            ...searchConfigTarget,
          },
        },
      },
    } as OpenClawConfig;
    const tool = provider.createTool({ config, searchConfig: searchConfigTarget });

    expect(tool).not.toBeNull();
    expect(tool?.description.trim()).not.toBe("");
    expect(tool?.parameters).toEqual(expect.any(Object));
    expect(typeof tool?.execute).toBe("function");
  });
}
