|
1 | | -import { mkdtempSync, readFileSync, statSync, writeFileSync } from 'node:fs'; |
| 1 | +import { mkdtempSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { connect } from 'node:net'; |
3 | 3 | import { tmpdir } from 'node:os'; |
4 | 4 | import { join } from 'node:path'; |
5 | 5 | import { Hono } from 'hono'; |
6 | 6 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
7 | 7 | import { hubDefaultOrigins, initPangularHub, type PangularHubOptions } from '../hub.ts'; |
8 | 8 | import { httpRegistry } from '../http-rules.ts'; |
| 9 | +import { writeTokenOnce } from '../mcp-token.ts'; |
9 | 10 | import { BASE_FILES, makeProject } from './analog-fixture.ts'; |
10 | 11 |
|
11 | 12 | const hubs: { close: () => Promise<void> }[] = []; |
@@ -320,6 +321,21 @@ describe('Pangular Inspector hub MCP route', () => { |
320 | 321 | if (process.platform !== 'win32') expect(statSync(file).mode & 0o777).toBe(0o600); |
321 | 322 | }); |
322 | 323 |
|
| 324 | + it('keeps the token another server wrote first, so both servers share one token', () => { |
| 325 | + const dir = mkdtempSync(join(tmpdir(), 'pangular-token-')); |
| 326 | + const file = join(dir, 'mcp-token'); |
| 327 | + expect(writeTokenOnce(file, 'first-server-token-0001')).toBe('first-server-token-0001'); |
| 328 | + // The second server read no file, then the first one wrote it before the second could. |
| 329 | + expect(writeTokenOnce(file, 'second-server-token-002')).toBe('first-server-token-0001'); |
| 330 | + expect(readFileSync(file, 'utf8').trim()).toBe('first-server-token-0001'); |
| 331 | + if (process.platform !== 'win32') expect(statSync(file).mode & 0o777).toBe(0o600); |
| 332 | + // A file without a valid token is replaced. |
| 333 | + writeFileSync(file, 'bad\n'); |
| 334 | + expect(writeTokenOnce(file, 'third-server-token-0003')).toBe('third-server-token-0003'); |
| 335 | + expect(readFileSync(file, 'utf8').trim()).toBe('third-server-token-0003'); |
| 336 | + expect(readdirSync(dir)).toEqual(['mcp-token']); |
| 337 | + }); |
| 338 | + |
323 | 339 | it('keeps the token and the mcp setting next to the devtools config', async () => { |
324 | 340 | vi.stubEnv('PANGULAR_MCP_TOKEN', 'env-secret'); |
325 | 341 | const guarded = await bootMcp({ inspectors: { http: false }, agent: { readOnly: true } }); |
|
0 commit comments