Newer
Older
navi-1 / webclient / tests / unit / stores / synapseReactions.test.js
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { useSynapseReactionsStore } from '@/stores/synapseReactions.js'

vi.mock('@/api/index.js', () => ({
  getSynapseReactions: vi.fn(),
  updateSynapseReactions: vi.fn(),
  getSynapseReactionVersions: vi.fn(),
}))

import * as api from '@/api/index.js'

const SETTINGS = {
  reactions_enabled: true,
  push_target: 'app_synapse',
  completion_notify: 'important',
  instructions: 'gntodo → developer',
  source_ready: true,
}

describe('Synapse reactions store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    vi.clearAllMocks()
  })

  it('fetchSettings GETs and stores settings', async () => {
    api.getSynapseReactions.mockResolvedValue({ ...SETTINGS })
    const store = useSynapseReactionsStore()
    expect(api.getSynapseReactions).toHaveBeenCalledTimes(0)
    await store.fetchSettings()
    expect(api.getSynapseReactions).toHaveBeenCalledTimes(1)
    expect(store.settings).toEqual(SETTINGS)
    expect(store.loading).toBe(false)
  })

  it('fetchVersions unpacks the items array', async () => {
    api.getSynapseReactionVersions.mockResolvedValue({
      items: [{ content: 'a', edited_by: 'user', reason: null, created_at: '2026-10-07T00:00:00Z' }],
    })
    const store = useSynapseReactionsStore()
    await store.fetchVersions()
    expect(api.getSynapseReactionVersions).toHaveBeenCalledWith(20)
    expect(store.versions).toHaveLength(1)
  })

  it('save merges a partial patch over settings and adopts the server response', async () => {
    api.updateSynapseReactions.mockResolvedValue({ ...SETTINGS, reactions_enabled: false })
    const store = useSynapseReactionsStore()
    store.settings = { ...SETTINGS }
    await store.save({ reactions_enabled: false })
    expect(api.updateSynapseReactions).toHaveBeenCalledWith({
      reactions_enabled: false,
      push_target: 'app_synapse',
      completion_notify: 'important',
      instructions: 'gntodo → developer',
      source_ready: true,
    })
    expect(store.settings.reactions_enabled).toBe(false)
    expect(store.saving).toBe(false)
  })

  it('save rethrows on failure but clears the saving flag', async () => {
    api.updateSynapseReactions.mockRejectedValue(new Error('500'))
    const store = useSynapseReactionsStore()
    store.settings = { ...SETTINGS }
    await expect(store.save({ reactions_enabled: false })).rejects.toThrow('500')
    expect(store.saving).toBe(false)
  })
})