Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

6.0 KB
Raw Blame
import test from 'node:test'
import assert from 'node:assert/strict'
import { buildSync } from 'esbuild'
import { createRequire } from 'node:module'
import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'

// Bundle the store with its real zustand dependency; the only other import is a
// type-only one (electron.d) which esbuild erases. The store reads the global
// `window.electron.workflows.*` bridge, which we stub per test.
function loadStore() {
  const outfile = join(mkdtempSync(join(tmpdir(), 'forge-wfstore-test-')), 'workflowsStore.cjs')
  const require = createRequire(import.meta.url)
  const result = buildSync({
    entryPoints: [resolve('src/shared/stores/workflowsStore.ts')],
    bundle: true,
    platform: 'node',
    format: 'cjs',
    write: false,
  })
  writeFileSync(outfile, result.outputFiles[0].text, 'utf8')
  return require(outfile).useWorkflowsStore
}

function stubBridge(workflows = {}) {
  globalThis.window = { electron: { workflows } }
}

const migrated = (id, over = {}) => ({
  id, name: id.toUpperCase(), description: '', nodes: [], edges: [],
  createdAt: '', updatedAt: '', ...over,
})

const legacyBlocks = (id) => ({
  id, name: id.toUpperCase(), description: '', input: 'image',
  blocks: [{ id: `${id}-blk`, extension: 'pack/x', enabled: true, params: {} }],
  createdAt: '', updatedAt: '',
})

// ─── load(): dedupe + migrate ──────────────────────────────────────────────────

test('load dedupes workflows that share an id, keeping the first', async () => {
  const useStore = loadStore()
  stubBridge({
    list: async () => [migrated('a', { name: 'FIRST' }), migrated('a', { name: 'SECOND' }), migrated('b')],
  })

  await useStore.getState().load()
  const { workflows, loading } = useStore.getState()

  assert.equal(loading, false)
  assert.deepEqual(workflows.map((w) => w.id), ['a', 'b'])
  assert.equal(workflows[0].name, 'FIRST') // the duplicate is dropped, first wins
})

test('load migrates legacy block-format workflows into nodes + edges', async () => {
  const useStore = loadStore()
  stubBridge({ list: async () => [legacyBlocks('w')] })

  await useStore.getState().load()
  const wf = useStore.getState().workflows[0]

  assert.deepEqual(wf.nodes.map((n) => n.type), ['inputNode', 'extensionNode'])
  assert.equal(wf.nodes[0].id, 'input-w')
  assert.equal(wf.nodes[1].id, 'w-blk')
  assert.equal(wf.edges.length, 1)
  assert.deepEqual({ source: wf.edges[0].source, target: wf.edges[0].target }, { source: 'input-w', target: 'w-blk' })
})

test('load flips loading back to false when the bridge throws', async () => {
  const useStore = loadStore()
  stubBridge({ list: async () => { throw new Error('disk error') } })

  await useStore.getState().load()
  assert.equal(useStore.getState().loading, false)
})

// ─── save(): insert vs update ──────────────────────────────────────────────────

test('save inserts a new workflow at the front, updates an existing one in place', async () => {
  const useStore = loadStore()
  stubBridge({ save: async () => ({ success: true }) })
  useStore.setState({ workflows: [migrated('a'), migrated('b')] })

  await useStore.getState().save(migrated('c'))
  assert.deepEqual(useStore.getState().workflows.map((w) => w.id), ['c', 'a', 'b'])

  await useStore.getState().save(migrated('b', { name: 'B2' }))
  const wf = useStore.getState().workflows.find((w) => w.id === 'b')
  assert.equal(wf.name, 'B2')
  assert.equal(useStore.getState().workflows.length, 3) // no duplicate
})

test('save does not touch state when the bridge reports failure', async () => {
  const useStore = loadStore()
  stubBridge({ save: async () => ({ success: false, error: 'nope' }) })
  useStore.setState({ workflows: [migrated('a')] })

  const result = await useStore.getState().save(migrated('z'))
  assert.equal(result.success, false)
  assert.deepEqual(useStore.getState().workflows.map((w) => w.id), ['a'])
})

// ─── remove(): filter + active reset ────────────────────────────────────────────

test('remove drops the workflow and clears activeId when it was the active one', async () => {
  const useStore = loadStore()
  stubBridge({ delete: async () => ({ success: true }) })
  useStore.setState({ workflows: [migrated('a'), migrated('b')], activeId: 'a' })

  await useStore.getState().remove('a')
  assert.deepEqual(useStore.getState().workflows.map((w) => w.id), ['b'])
  assert.equal(useStore.getState().activeId, null)
})

test('remove keeps activeId when a different workflow is deleted', async () => {
  const useStore = loadStore()
  stubBridge({ delete: async () => ({ success: true }) })
  useStore.setState({ workflows: [migrated('a'), migrated('b')], activeId: 'b' })

  await useStore.getState().remove('a')
  assert.equal(useStore.getState().activeId, 'b')
})

// ─── importFile() + setActive() ────────────────────────────────────────────────

test('importFile migrates, moves the workflow to the front, and makes it active', async () => {
  const useStore = loadStore()
  stubBridge({ import: async () => ({ success: true, workflow: legacyBlocks('imp') }) })
  useStore.setState({ workflows: [migrated('a')], activeId: 'a' })

  await useStore.getState().importFile()
  const { workflows, activeId } = useStore.getState()

  assert.deepEqual(workflows.map((w) => w.id), ['imp', 'a'])
  assert.equal(activeId, 'imp')
  assert.deepEqual(workflows[0].nodes.map((n) => n.type), ['inputNode', 'extensionNode'])
})

test('setActive updates only activeId', () => {
  const useStore = loadStore()
  stubBridge()
  useStore.setState({ workflows: [migrated('a')], activeId: null })

  useStore.getState().setActive('a')
  assert.equal(useStore.getState().activeId, 'a')
})