import { describe, it, expect, beforeEach } from "vitest";
import { mount } from "@vue/test-utils";
import { setActivePinia, createPinia } from "pinia";
import { useScriptsStore } from "../../stores/scripts.js";
import ScriptsActionsPage from "../../features/scripts/pages/ScriptsActionsPage.vue";

describe("Script execution integration", () => {
  beforeEach(() => {
    setActivePinia(createPinia());
  });

  it("loads action scripts on mount", async () => {
    const wrapper = mount(ScriptsActionsPage);
    await new Promise((resolve) => setTimeout(resolve, 50));

    const store = useScriptsStore();
    expect(store.actions.length).toBeGreaterThan(0);
    expect(store.actions[0].alias).toBe("kitchen_light");
  });

  it("runs an action script and stores result", async () => {
    const store = useScriptsStore();
    await store.loadActions();

    const script = store.actions[0];
    expect(script.state).toBe("enabled");

    await store.runScript(script.alias);

    expect(store.lastRunResult).toMatchObject({
      alias: script.alias,
      ok: true,
      data: { ok: true },
      execTime: "0.042 seconds",
    });
  });
});
