|
| 1 | +import type { DevframeNodeContext, DevframeRpcClientFunctions, DevframeRpcServerFunctions } from '../../types' |
1 | 2 | import { mkdtempSync, writeFileSync } from 'node:fs' |
2 | 3 | import { tmpdir } from 'node:os' |
3 | 4 | import { join } from 'node:path' |
| 5 | +import { createRpcClient } from 'devframe/rpc/client' |
| 6 | +import { createWsRpcChannel } from 'devframe/rpc/transports/ws-client' |
4 | 7 | import { getPort } from 'get-port-please' |
5 | | -import { describe, expect, it } from 'vitest' |
| 8 | +import { describe, expect, it, vi } from 'vitest' |
6 | 9 | import { WebSocket } from 'ws' |
| 10 | +import { getTempAuthCode } from '../../node/auth/state' |
7 | 11 | import { defineDevframe } from '../../types/devframe' |
8 | 12 | import { createDevServer, resolveDevServerPort } from '../dev' |
9 | 13 |
|
| 14 | +function connectWsClient(host: string, port: number, authToken?: string) { |
| 15 | + return createRpcClient<DevframeRpcServerFunctions, DevframeRpcClientFunctions>( |
| 16 | + {} as DevframeRpcClientFunctions, |
| 17 | + { channel: createWsRpcChannel({ url: `ws://${host}:${port}/__devframe_ws`, authToken }) }, |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +const HANDSHAKE = { authToken: '', ua: 'test', origin: 'http://localhost' } |
| 22 | + |
10 | 23 | function makeTmpDist(): string { |
11 | 24 | const dir = mkdtempSync(join(tmpdir(), 'devframe-dev-')) |
12 | 25 | writeFileSync(join(dir, 'index.html'), '<!doctype html><title>test</title>', 'utf-8') |
@@ -271,6 +284,103 @@ describe('adapters/dev', () => { |
271 | 284 | } |
272 | 285 | }) |
273 | 286 |
|
| 287 | + it('gates by default: an unset `auth` auto-wires the interactive OTP handler', async () => { |
| 288 | + const devframe = defineDevframe({ |
| 289 | + id: 'devframe-auth-default', |
| 290 | + name: 'Auth Default', |
| 291 | + version: '0.0.0', |
| 292 | + packageName: 'devframe-test', |
| 293 | + homepage: 'https://example.test', |
| 294 | + description: 'Test devframe.', |
| 295 | + setup: (ctx: DevframeNodeContext) => { |
| 296 | + ctx.rpc.register({ name: 'test:probe', type: 'query', handler: () => 'ok' }) |
| 297 | + }, |
| 298 | + }) |
| 299 | + const host = '127.0.0.1' |
| 300 | + const port = await getPort({ port: 19410, host }) |
| 301 | + // No `banner` override is exposed through the adapter, so silence the |
| 302 | + // default stdout banner for the test run. |
| 303 | + const spy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 304 | + const handle = await createDevServer(devframe, { host, port, openBrowser: false }) |
| 305 | + |
| 306 | + try { |
| 307 | + const client = connectWsClient(host, port) |
| 308 | + // Untrusted: only `anonymous:` methods are reachable; the probe rejects. |
| 309 | + const handshake = await client.$call('anonymous:devframe:auth' as any, HANDSHAKE) |
| 310 | + expect(handshake).toEqual({ isTrusted: false }) |
| 311 | + await expect(client.$call('test:probe' as any)).rejects.toThrow() |
| 312 | + |
| 313 | + // The interactive exchange method is wired — the printed code trusts. |
| 314 | + const code = getTempAuthCode() |
| 315 | + const exchange = await client.$call('anonymous:devframe:auth:exchange' as any, { code, ua: 'test', origin: 'http://localhost' }) as { authToken: string | null } |
| 316 | + expect(exchange.authToken).toBeTruthy() |
| 317 | + await expect(client.$call('test:probe' as any)).resolves.toBe('ok') |
| 318 | + client.$close() |
| 319 | + } |
| 320 | + finally { |
| 321 | + spy.mockRestore() |
| 322 | + await handle.close() |
| 323 | + } |
| 324 | + }) |
| 325 | + |
| 326 | + it('opts out with `auth: false`: the server auto-trusts and skips the gate', async () => { |
| 327 | + const devframe = defineDevframe({ |
| 328 | + id: 'devframe-auth-off', |
| 329 | + name: 'Auth Off', |
| 330 | + version: '0.0.0', |
| 331 | + packageName: 'devframe-test', |
| 332 | + homepage: 'https://example.test', |
| 333 | + description: 'Test devframe.', |
| 334 | + cli: { auth: false }, |
| 335 | + setup: (ctx: DevframeNodeContext) => { |
| 336 | + ctx.rpc.register({ name: 'test:probe', type: 'query', handler: () => 'ok' }) |
| 337 | + }, |
| 338 | + }) |
| 339 | + const host = '127.0.0.1' |
| 340 | + const port = await getPort({ port: 19420, host }) |
| 341 | + const handle = await createDevServer(devframe, { host, port, openBrowser: false }) |
| 342 | + |
| 343 | + try { |
| 344 | + const client = connectWsClient(host, port) |
| 345 | + const handshake = await client.$call('anonymous:devframe:auth' as any, HANDSHAKE) |
| 346 | + expect(handshake).toEqual({ isTrusted: true }) |
| 347 | + // Ungated: the probe resolves without any code exchange. |
| 348 | + await expect(client.$call('test:probe' as any)).resolves.toBe('ok') |
| 349 | + client.$close() |
| 350 | + } |
| 351 | + finally { |
| 352 | + await handle.close() |
| 353 | + } |
| 354 | + }) |
| 355 | + |
| 356 | + it('the `--no-auth` flag (flags.auth === false) opts out of the gate', async () => { |
| 357 | + const devframe = defineDevframe({ |
| 358 | + id: 'devframe-auth-flag', |
| 359 | + name: 'Auth Flag', |
| 360 | + version: '0.0.0', |
| 361 | + packageName: 'devframe-test', |
| 362 | + homepage: 'https://example.test', |
| 363 | + description: 'Test devframe.', |
| 364 | + setup: (ctx: DevframeNodeContext) => { |
| 365 | + ctx.rpc.register({ name: 'test:probe', type: 'query', handler: () => 'ok' }) |
| 366 | + }, |
| 367 | + }) |
| 368 | + const host = '127.0.0.1' |
| 369 | + const port = await getPort({ port: 19430, host }) |
| 370 | + const handle = await createDevServer(devframe, { host, port, openBrowser: false, flags: { auth: false } }) |
| 371 | + |
| 372 | + try { |
| 373 | + const client = connectWsClient(host, port) |
| 374 | + const handshake = await client.$call('anonymous:devframe:auth' as any, HANDSHAKE) |
| 375 | + expect(handshake).toEqual({ isTrusted: true }) |
| 376 | + await expect(client.$call('test:probe' as any)).resolves.toBe('ok') |
| 377 | + client.$close() |
| 378 | + } |
| 379 | + finally { |
| 380 | + await handle.close() |
| 381 | + } |
| 382 | + }) |
| 383 | + |
274 | 384 | it('resolveDevServerPort honors def.cli.port as the preferred default', async () => { |
275 | 385 | const preferred = await getPort({ port: 19500, host: '127.0.0.1' }) |
276 | 386 | const devframe = defineDevframe({ |
|
0 commit comments