File size: 1,684 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { beforeEach, describe, expect, it, vi } from 'vitest'

const apiMocks = vi.hoisted(() => ({
  list: vi.fn(),
  open: vi.fn(),
}))

vi.mock('../api/openTargets', () => ({
  openTargetsApi: apiMocks,
}))

describe('openTargetStore', () => {
  beforeEach(async () => {
    vi.resetModules()
    apiMocks.list.mockReset()
    apiMocks.open.mockReset()
  })

  it('caches detected targets inside the TTL', async () => {
    const { useOpenTargetStore } = await import('./openTargetStore')
    apiMocks.list.mockResolvedValue({
      platform: 'darwin',
      targets: [{ id: 'finder', kind: 'file_manager', label: 'Finder', icon: 'finder', platform: 'darwin' }],
      primaryTargetId: 'finder',
      cachedAt: 1,
      ttlMs: 60_000,
    })

    await useOpenTargetStore.getState().refreshTargets()
    await useOpenTargetStore.getState().ensureTargets()

    expect(apiMocks.list).toHaveBeenCalledTimes(1)
    expect(useOpenTargetStore.getState().primaryTargetId).toBe('finder')
  })

  it('remembers the last successful target for this runtime', async () => {
    const { useOpenTargetStore } = await import('./openTargetStore')
    apiMocks.list.mockResolvedValue({
      platform: 'darwin',
      targets: [{ id: 'vscode', kind: 'ide', label: 'VS Code', icon: 'vscode', platform: 'darwin' }],
      primaryTargetId: 'vscode',
      cachedAt: 1,
      ttlMs: 60_000,
    })
    apiMocks.open.mockResolvedValue({ ok: true, targetId: 'vscode', path: '/repo' })

    await useOpenTargetStore.getState().refreshTargets()
    await useOpenTargetStore.getState().openTarget('vscode', '/repo')

    expect(useOpenTargetStore.getState().lastSuccessfulTargetId).toBe('vscode')
  })
})