File size: 4,319 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { afterEach, describe, expect, it, spyOn } from 'bun:test'
import { handleOpenTargetsApi } from '../api/open-targets.js'
import { openTargetService } from '../services/openTargetService.js'

let listTargetsSpy: ReturnType<typeof spyOn> | undefined
let openTargetSpy: ReturnType<typeof spyOn> | undefined
let getTargetIconSpy: ReturnType<typeof spyOn> | undefined

function makeRequest(
  method: string,
  urlStr: string,
  body?: Record<string, unknown> | string,
): { req: Request; url: URL; segments: string[] } {
  const url = new URL(urlStr, 'http://localhost:3456')
  const init: RequestInit = { method }
  if (body !== undefined) {
    init.headers = { 'Content-Type': 'application/json' }
    init.body = typeof body === 'string' ? body : JSON.stringify(body)
  }
  const req = new Request(url.toString(), init)
  return {
    req,
    url,
    segments: url.pathname.split('/').filter(Boolean),
  }
}

describe('open-targets API', () => {
  afterEach(() => {
    listTargetsSpy?.mockRestore()
    listTargetsSpy = undefined
    openTargetSpy?.mockRestore()
    openTargetSpy = undefined
    getTargetIconSpy?.mockRestore()
    getTargetIconSpy = undefined
  })

  it('returns detected targets from GET /api/open-targets', async () => {
    listTargetsSpy = spyOn(openTargetService, 'listTargets').mockResolvedValue({
      platform: 'darwin',
      targets: [
        { id: 'vscode', kind: 'ide', label: 'VS Code', icon: 'vscode', platform: 'darwin' },
      ],
      primaryTargetId: 'vscode',
      cachedAt: 123,
      ttlMs: 1_000,
    })

    const { req, url, segments } = makeRequest('GET', '/api/open-targets')
    const res = await handleOpenTargetsApi(req, url, segments)

    expect(res.status).toBe(200)
    expect(listTargetsSpy).toHaveBeenCalledTimes(1)
    await expect(res.json()).resolves.toMatchObject({
      platform: 'darwin',
      primaryTargetId: 'vscode',
      targets: [{ id: 'vscode', kind: 'ide' }],
    })
  })

  it('opens an allowed target from POST /api/open-targets/open', async () => {
    openTargetSpy = spyOn(openTargetService, 'openTarget').mockResolvedValue({
      ok: true,
      targetId: 'vscode',
      path: '/Users/nanmi/project',
    })

    const { req, url, segments } = makeRequest('POST', '/api/open-targets/open', {
      targetId: 'vscode',
      path: '/Users/nanmi/project',
    })
    const res = await handleOpenTargetsApi(req, url, segments)

    expect(res.status).toBe(200)
    expect(openTargetSpy).toHaveBeenCalledWith({
      targetId: 'vscode',
      path: '/Users/nanmi/project',
    })
    await expect(res.json()).resolves.toMatchObject({
      ok: true,
      targetId: 'vscode',
      path: '/Users/nanmi/project',
    })
  })

  it('rejects invalid request bodies before opening', async () => {
    openTargetSpy = spyOn(openTargetService, 'openTarget')

    const { req, url, segments } = makeRequest('POST', '/api/open-targets/open', { targetId: 'vscode' })
    const res = await handleOpenTargetsApi(req, url, segments)

    expect(res.status).toBe(400)
    expect(openTargetSpy).not.toHaveBeenCalled()
    await expect(res.json()).resolves.toMatchObject({
      error: 'BAD_REQUEST',
      message: 'Missing or invalid "path" in request body',
    })
  })

  it('rejects invalid JSON bodies', async () => {
    const { req, url, segments } = makeRequest('POST', '/api/open-targets/open', '{not json')
    const res = await handleOpenTargetsApi(req, url, segments)

    expect(res.status).toBe(400)
    await expect(res.json()).resolves.toMatchObject({
      error: 'BAD_REQUEST',
      message: 'Invalid JSON body',
    })
  })

  it('returns a target icon as cacheable PNG', async () => {
    getTargetIconSpy = spyOn(openTargetService, 'getTargetIcon').mockResolvedValue({
      contentType: 'image/png',
      data: new Uint8Array([1, 2, 3]),
    })

    const { req, url, segments } = makeRequest('GET', '/api/open-targets/icons/vscode')
    const res = await handleOpenTargetsApi(req, url, segments)

    expect(res.status).toBe(200)
    expect(res.headers.get('Content-Type')).toBe('image/png')
    expect(res.headers.get('Cache-Control')).toBe('private, max-age=86400')
    expect(getTargetIconSpy).toHaveBeenCalledWith('vscode')
    expect(Array.from(new Uint8Array(await res.arrayBuffer()))).toEqual([1, 2, 3])
  })
})