import test from "node:test"
import assert from "node:assert/strict"
import { readFileSync } from "node:fs"

const sharedTypesSource = readFileSync(new URL("../../../packages/shared-types/src/index.ts", import.meta.url), "utf8")
const ipcSource = readFileSync(new URL("../src/main/ipc.ts", import.meta.url), "utf8")
const indexSource = readFileSync(new URL("../src/main/index.ts", import.meta.url), "utf8")
const preloadSource = readFileSync(new URL("../src/preload/index.ts", import.meta.url), "utf8")

test("shared automation task channels and desktop API are declared", () => {
  assert.match(sharedTypesSource, /automationTasksList:\s*"automation-tasks:list"/)
  assert.match(sharedTypesSource, /automationTasksCreate:\s*"automation-tasks:create"/)
  assert.match(sharedTypesSource, /automationTasksUpdate:\s*"automation-tasks:update"/)
  assert.match(sharedTypesSource, /automationTasksDelete:\s*"automation-tasks:delete"/)
  assert.match(sharedTypesSource, /automationTasksRunNow:\s*"automation-tasks:run-now"/)
  assert.match(sharedTypesSource, /automationTasksListRuns:\s*"automation-tasks:list-runs"/)
  assert.match(sharedTypesSource, /automationTasks:\s*\{/)
})

test("desktop IPC wires automation tasks to AutomationTaskService", () => {
  assert.match(indexSource, /new AutomationTaskService\(systemSummary\.userDataPath/)
  assert.match(indexSource, /automationTaskService\.start\(\)/)
  assert.match(ipcSource, /automationTaskService\.setExecutor/)
  assert.match(ipcSource, /ipcMain\.handle\(IPC_CHANNELS\.automationTasksList/)
  assert.match(ipcSource, /ipcMain\.handle\(IPC_CHANNELS\.automationTasksRunNow/)
  assert.match(preloadSource, /automationTasks:\s*\{/)
  assert.match(preloadSource, /IPC_CHANNELS\.automationTasksCreate/)
})
