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

import initialView from "../../fixtures/player-view/initial.json";
import type { PlayerView, TurnRequest, TurnResult } from "@contracts";

const invokeMock = vi.hoisted(() => vi.fn());

vi.mock("@tauri-apps/api/core", () => ({
  invoke: invokeMock
}));

import { submitTurn } from "./bridge";

const request: TurnRequest = {
  storyId: initialView.storyId,
  branchId: initialView.branchId,
  expectedNodeId: initialView.nodeId,
  actionId: "action_bridge_test",
  intent: "continue",
  input: ""
};

describe("Tauri bridge", () => {
  beforeEach(() => {
    invokeMock.mockReset();
    window.__TAURI_INTERNALS__ = {};
  });

  afterEach(() => {
    delete window.__TAURI_INTERNALS__;
  });

  it("sends only TurnRequest to the submit_turn command", async () => {
    const expected: TurnResult = {
      committedNodeId: "node_002",
      playerView: {
        ...(initialView as PlayerView),
        nodeId: "node_002"
      }
    };
    invokeMock.mockResolvedValue(expected);

    await expect(submitTurn(request, initialView as PlayerView)).resolves.toEqual(expected);
    expect(invokeMock).toHaveBeenCalledWith("submit_turn", { request });
  });
});
