File size: 9,895 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import * as fs from 'node:fs/promises'
import * as os from 'node:os'
import * as path from 'node:path'
import {
  findPrivateLanAddress,
  H5AccessService,
  resolveEffectiveH5PublicBaseUrl,
} from '../services/h5AccessService.js'
import { ProviderService } from '../services/providerService.js'

let tmpDir: string
let originalConfigDir: string | undefined
let originalH5PublicBaseUrl: string | undefined
let originalH5AutoPublicUrl: string | undefined

function getManagedSettingsPath(): string {
  return path.join(tmpDir, 'cc-haha', 'settings.json')
}

beforeEach(async () => {
  tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'h5-access-service-test-'))
  originalConfigDir = process.env.CLAUDE_CONFIG_DIR
  originalH5PublicBaseUrl = process.env.CLAUDE_H5_PUBLIC_BASE_URL
  originalH5AutoPublicUrl = process.env.CLAUDE_H5_AUTO_PUBLIC_URL
  process.env.CLAUDE_CONFIG_DIR = tmpDir
})

afterEach(async () => {
  if (originalConfigDir === undefined) delete process.env.CLAUDE_CONFIG_DIR
  else process.env.CLAUDE_CONFIG_DIR = originalConfigDir
  if (originalH5PublicBaseUrl === undefined) delete process.env.CLAUDE_H5_PUBLIC_BASE_URL
  else process.env.CLAUDE_H5_PUBLIC_BASE_URL = originalH5PublicBaseUrl
  if (originalH5AutoPublicUrl === undefined) delete process.env.CLAUDE_H5_AUTO_PUBLIC_URL
  else process.env.CLAUDE_H5_AUTO_PUBLIC_URL = originalH5AutoPublicUrl
  await fs.rm(tmpDir, { recursive: true, force: true })
})

describe('H5AccessService', () => {
  test('defaults to disabled state with sanitized settings', async () => {
    const service = new H5AccessService()

    await expect(service.getSettings()).resolves.toEqual({
      enabled: false,
      tokenPreview: null,
      allowedOrigins: [],
      publicBaseUrl: null,
    })

    await expect(service.validateToken('missing-token')).resolves.toBe(false)
  })

  test('enable generates a token and persists only hash plus preview', async () => {
    const service = new H5AccessService()

    const result = await service.enable()
    const raw = await fs.readFile(getManagedSettingsPath(), 'utf-8')
    const saved = JSON.parse(raw) as {
      h5Access: {
        enabled: boolean
        tokenHash: string
        tokenPreview: string
      }
    }

    expect(result.token).toMatch(/^h5_[A-Za-z0-9_-]{43}$/)
    expect(result.settings).toEqual({
      enabled: true,
      tokenPreview: saved.h5Access.tokenPreview,
      allowedOrigins: [],
      publicBaseUrl: null,
    })
    expect(saved.h5Access.enabled).toBe(true)
    expect(saved.h5Access.tokenHash).toHaveLength(64)
    expect(saved.h5Access.tokenPreview).toBe(
      `${result.token.slice(0, 7)}...${result.token.slice(-4)}`,
    )
    expect(raw).not.toContain(result.token)
    expect(await service.validateToken(result.token)).toBe(true)
  })

  test('enabled public settings use the packaged app LAN URL when provided', async () => {
    process.env.CLAUDE_H5_PUBLIC_BASE_URL = 'http://192.168.1.20:28670/'
    process.env.CLAUDE_H5_AUTO_PUBLIC_URL = '1'
    const service = new H5AccessService()

    const result = await service.enable()

    expect(result.settings.publicBaseUrl).toBe('http://192.168.1.20:28670')
  })

  test('configured public URL overrides stale stored local URLs', async () => {
    const service = new H5AccessService()
    await service.updateSettings({
      publicBaseUrl: 'http://192.168.0.102:5179',
    })

    process.env.CLAUDE_H5_PUBLIC_BASE_URL = 'https://chat.example.com/app/'
    const result = await service.enable()

    expect(result.settings.publicBaseUrl).toBe('https://chat.example.com/app')
  })

  test('auto LAN mode fills blank or loopback URLs but preserves manual LAN URLs', () => {
    expect(resolveEffectiveH5PublicBaseUrl({
      enabled: true,
      storedPublicBaseUrl: null,
      configuredPublicBaseUrl: null,
      autoPublicBaseUrl: 'http://192.168.0.102:39876',
    })).toBe('http://192.168.0.102:39876')

    expect(resolveEffectiveH5PublicBaseUrl({
      enabled: true,
      storedPublicBaseUrl: 'http://127.0.0.1:5179',
      configuredPublicBaseUrl: null,
      autoPublicBaseUrl: 'http://192.168.0.102:39876',
    })).toBe('http://192.168.0.102:39876')

    expect(resolveEffectiveH5PublicBaseUrl({
      enabled: true,
      storedPublicBaseUrl: 'http://192.168.1.100:54064',
      configuredPublicBaseUrl: null,
      autoPublicBaseUrl: 'http://172.20.16.1:39876',
    })).toBe('http://192.168.1.100:54064')

    expect(resolveEffectiveH5PublicBaseUrl({
      enabled: true,
      storedPublicBaseUrl: 'https://chat.example.com/app',
      configuredPublicBaseUrl: null,
      autoPublicBaseUrl: 'http://192.168.0.102:39876',
    })).toBe('https://chat.example.com/app')
  })

  test('auto LAN detection prefers physical adapters over WSL and Docker virtual adapters', () => {
    expect(findPrivateLanAddress({
      'vEthernet (WSL)': [{
        address: '172.20.16.1',
        netmask: '255.255.240.0',
        family: 'IPv4',
        mac: '00:15:5d:00:00:01',
        internal: false,
        cidr: '172.20.16.1/20',
      }],
      'Docker Desktop': [{
        address: '172.17.0.1',
        netmask: '255.255.0.0',
        family: 'IPv4',
        mac: '02:42:ac:11:00:01',
        internal: false,
        cidr: '172.17.0.1/16',
      }],
      'Wi-Fi': [{
        address: '192.168.1.100',
        netmask: '255.255.255.0',
        family: 'IPv4',
        mac: 'aa:bb:cc:dd:ee:ff',
        internal: false,
        cidr: '192.168.1.100/24',
      }],
    })).toBe('192.168.1.100')
  })

  test('regenerateToken invalidates the previous token', async () => {
    const service = new H5AccessService()

    const first = await service.enable()
    const second = await service.regenerateToken()

    expect(second.token).toMatch(/^h5_/)
    expect(second.token).not.toBe(first.token)
    expect(await service.validateToken(first.token)).toBe(false)
    expect(await service.validateToken(second.token)).toBe(true)
  })

  test('preserves unknown managed settings fields when updating h5Access', async () => {
    await fs.mkdir(path.dirname(getManagedSettingsPath()), { recursive: true })
    await fs.writeFile(
      getManagedSettingsPath(),
      JSON.stringify(
        {
          env: {
            ANTHROPIC_MODEL: 'keep-me',
          },
          futureField: {
            keep: true,
          },
        },
        null,
        2,
      ),
      'utf-8',
    )

    const service = new H5AccessService()
    await service.enable()

    const saved = JSON.parse(await fs.readFile(getManagedSettingsPath(), 'utf-8')) as {
      env: {
        ANTHROPIC_MODEL: string
      }
      futureField: {
        keep: boolean
      }
      h5Access: unknown
    }

    expect(saved.env.ANTHROPIC_MODEL).toBe('keep-me')
    expect(saved.futureField).toEqual({ keep: true })
    expect(saved.h5Access).toBeDefined()
  })

  test('updateSettings normalizes origins and rejects invalid ones', async () => {
    const service = new H5AccessService()

    await expect(
      service.updateSettings({
        allowedOrigins: ['https://example.com/path', 'http://localhost:3000/foo'],
        publicBaseUrl: 'https://public.example.com/app/',
      }),
    ).resolves.toEqual({
      enabled: false,
      tokenPreview: null,
      allowedOrigins: ['https://example.com', 'http://localhost:3000'],
      publicBaseUrl: 'https://public.example.com/app',
    })

    await expect(
      service.updateSettings({
        allowedOrigins: ['https://*.example.com'],
      }),
    ).rejects.toMatchObject({
      statusCode: 400,
    })
  })

  test('isOriginAllowed requires enabled state and matches normalized origins', async () => {
    const service = new H5AccessService()

    await service.updateSettings({
      allowedOrigins: ['https://example.com/path'],
    })

    await expect(service.isOriginAllowed('https://example.com')).resolves.toBe(false)

    await service.enable()

    await expect(service.isOriginAllowed('https://example.com')).resolves.toBe(true)
    await expect(service.isOriginAllowed('https://other.example.com')).resolves.toBe(false)
    await expect(service.isOriginAllowed('notaurl')).resolves.toBe(false)
  })

  test('malformed persisted enabled state without token hash is treated as disabled', async () => {
    await fs.mkdir(path.dirname(getManagedSettingsPath()), { recursive: true })
    await fs.writeFile(
      getManagedSettingsPath(),
      JSON.stringify({
        h5Access: {
          enabled: true,
          allowedOrigins: ['https://example.com/path'],
          publicBaseUrl: 'https://public.example.com',
        },
      }),
      'utf-8',
    )

    const service = new H5AccessService()

    await expect(service.getSettings()).resolves.toEqual({
      enabled: false,
      tokenPreview: null,
      allowedOrigins: ['https://example.com'],
      publicBaseUrl: 'https://public.example.com',
    })
    await expect(service.validateToken('anything')).resolves.toBe(false)
    await expect(service.isOriginAllowed('https://example.com')).resolves.toBe(false)
  })

  test('concurrent h5 enable and provider managed settings update preserve both fields', async () => {
    const h5Service = new H5AccessService()
    const providerService = new ProviderService()

    await Promise.all([
      h5Service.enable(),
      providerService.updateManagedSettings({
        env: {
          ANTHROPIC_MODEL: 'keep-me',
        },
      }),
    ])

    const saved = JSON.parse(await fs.readFile(getManagedSettingsPath(), 'utf-8')) as {
      env?: {
        ANTHROPIC_MODEL?: string
      }
      h5Access?: {
        enabled?: boolean
        tokenHash?: string | null
      }
    }

    expect(saved.env?.ANTHROPIC_MODEL).toBe('keep-me')
    expect(saved.h5Access?.enabled).toBe(true)
    expect(saved.h5Access?.tokenHash).toEqual(expect.any(String))
  })
})