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 preloadSource = readFileSync(new URL("../../desktop/src/preload/index.ts", import.meta.url), "utf8")
const ipcSource = readFileSync(new URL("../../desktop/src/main/ipc.ts", import.meta.url), "utf8")

test("config reveal secret IPC is whitelisted in shared types, preload, and main IPC", () => {
  assert.match(sharedTypesSource, /configRevealSecret:\s*"config:reveal-secret"/)
  assert.match(sharedTypesSource, /export type ConfigSecretId =/)
  assert.match(sharedTypesSource, /revealSecret\(secretId: ConfigSecretId\): Promise<string \| null>/)
  assert.match(preloadSource, /type ConfigSecretId/)
  assert.match(preloadSource, /revealSecret: \(secretId: ConfigSecretId\) => ipcRenderer\.invoke\(IPC_CHANNELS\.configRevealSecret, secretId\)/)
  assert.match(ipcSource, /const revealConfigSecret = async \(secretId: ConfigSecretId\): Promise<string \| null> =>/)
  assert.match(ipcSource, /ipcMain\.handle\(IPC_CHANNELS\.configRevealSecret/)
  assert.match(ipcSource, /revealSecret: \(secretId: ConfigSecretId\) => revealConfigSecret\(secretId\)/)
})
