|
| 1 | +import { devWebUiOrigin } from './login.js'; |
| 2 | +import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A checkout `tryResolveRepoContext` accepts, sitting on `branch`. It needs the |
| 9 | + * root package name plus every REQUIRED_SOURCES entry to exist. |
| 10 | + */ |
| 11 | +function repoOnBranch(branch: string): string { |
| 12 | + const root = mkdtempSync(join(tmpdir(), 'bai-cli-login-')); |
| 13 | + writeFileSync( |
| 14 | + join(root, 'package.json'), |
| 15 | + JSON.stringify({ name: 'backend.ai-webui', version: '0.0.0-test' }), |
| 16 | + ); |
| 17 | + mkdirSync(join(root, 'data'), { recursive: true }); |
| 18 | + writeFileSync(join(root, 'data', 'schema.graphql'), 'type Query { a: Int }\n'); |
| 19 | + mkdirSync(join(root, 'resources', 'i18n'), { recursive: true }); |
| 20 | + mkdirSync(join(root, 'packages', 'backend.ai-webui-docs'), { recursive: true }); |
| 21 | + mkdirSync(join(root, '.git')); |
| 22 | + writeFileSync(join(root, '.git', 'HEAD'), `ref: refs/heads/${branch}\n`); |
| 23 | + return root; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * A Portless state dir. `pid` defaults to this test process, whose cwd is the |
| 28 | + * package directory — i.e. a live route that this checkout does NOT own, which is |
| 29 | + * what the liveness-but-not-ownership fallback is about. |
| 30 | + */ |
| 31 | +function portlessState( |
| 32 | + routes: Array<{ hostname: string; pid?: number }>, |
| 33 | +): string { |
| 34 | + const dir = mkdtempSync(join(tmpdir(), 'bai-cli-portless-')); |
| 35 | + writeFileSync( |
| 36 | + join(dir, 'routes.json'), |
| 37 | + JSON.stringify( |
| 38 | + routes.map((route, i) => ({ |
| 39 | + hostname: route.hostname, |
| 40 | + port: 4000 + i, |
| 41 | + pid: route.pid ?? process.pid, |
| 42 | + })), |
| 43 | + ), |
| 44 | + ); |
| 45 | + return dir; |
| 46 | +} |
| 47 | + |
| 48 | +/** A pid that is certainly not running, for the stale-entry cases. */ |
| 49 | +const DEAD_PID = 2 ** 22 - 1; |
| 50 | + |
| 51 | +describe('devWebUiOrigin', () => { |
| 52 | + const env = (stateDir?: string) => ({ |
| 53 | + PORTLESS_PORT: '1357', |
| 54 | + ...(stateDir ? { PORTLESS_STATE_DIR: stateDir } : {}), |
| 55 | + }); |
| 56 | + |
| 57 | + it('falls back to the bare issue key when no route is registered', () => { |
| 58 | + const cwd = repoOnBranch('fix/FR-3665-portless-app-name'); |
| 59 | + expect(devWebUiOrigin(cwd, env(portlessState([])))).toBe( |
| 60 | + 'https://fr-3665.localhost:1357', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + // FR-3665: dev.mjs now names the server `<issue>-<pr>-<word>`, so the issue key |
| 65 | + // on its own is no longer a hostname once a PR exists. |
| 66 | + it('resolves to the composed route that is actually running', () => { |
| 67 | + const cwd = repoOnBranch('fix/FR-3665-portless-app-name'); |
| 68 | + const state = portlessState([ |
| 69 | + { hostname: 'fr-3654.localhost' }, |
| 70 | + { hostname: 'fr-3665-pr9049-statusline.localhost' }, |
| 71 | + ]); |
| 72 | + expect(devWebUiOrigin(cwd, env(state))).toBe( |
| 73 | + 'https://fr-3665-pr9049-statusline.localhost:1357', |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('ignores a route whose owning process is gone', () => { |
| 78 | + const cwd = repoOnBranch('FR-3665'); |
| 79 | + const state = portlessState([ |
| 80 | + { hostname: 'fr-3665-pr9049-statusline.localhost', pid: DEAD_PID }, |
| 81 | + ]); |
| 82 | + expect(devWebUiOrigin(cwd, env(state))).toBe('https://fr-3665.localhost:1357'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('skips a dead route in favour of a live one', () => { |
| 86 | + const cwd = repoOnBranch('FR-3665'); |
| 87 | + const state = portlessState([ |
| 88 | + { hostname: 'fr-3665-old.localhost', pid: DEAD_PID }, |
| 89 | + { hostname: 'fr-3665-pr9049-statusline.localhost' }, |
| 90 | + ]); |
| 91 | + expect(devWebUiOrigin(cwd, env(state))).toBe( |
| 92 | + 'https://fr-3665-pr9049-statusline.localhost:1357', |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('ignores an alias route, which has no owning process', () => { |
| 97 | + const cwd = repoOnBranch('FR-3665'); |
| 98 | + const state = portlessState([{ hostname: 'fr-3665-alias.localhost', pid: 0 }]); |
| 99 | + expect(devWebUiOrigin(cwd, env(state))).toBe('https://fr-3665.localhost:1357'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('prefers an exact match over a composed one', () => { |
| 103 | + const cwd = repoOnBranch('FR-3665'); |
| 104 | + const state = portlessState([ |
| 105 | + { hostname: 'fr-3665-pr9049-statusline.localhost' }, |
| 106 | + { hostname: 'fr-3665.localhost' }, |
| 107 | + ]); |
| 108 | + expect(devWebUiOrigin(cwd, env(state))).toBe('https://fr-3665.localhost:1357'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('takes the shortest when several extend the issue key', () => { |
| 112 | + const cwd = repoOnBranch('FR-3665'); |
| 113 | + const state = portlessState([ |
| 114 | + { hostname: 'fr-3665-pr9049-statusline-and-more.localhost' }, |
| 115 | + { hostname: 'fr-3665-alt.localhost' }, |
| 116 | + ]); |
| 117 | + expect(devWebUiOrigin(cwd, env(state))).toBe( |
| 118 | + 'https://fr-3665-alt.localhost:1357', |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('does not match a different issue that merely shares a prefix', () => { |
| 123 | + const cwd = repoOnBranch('FR-366'); |
| 124 | + const state = portlessState([ |
| 125 | + { hostname: 'fr-3665-pr9049-statusline.localhost' }, |
| 126 | + ]); |
| 127 | + expect(devWebUiOrigin(cwd, env(state))).toBe('https://fr-366.localhost:1357'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('survives a missing or malformed routes.json', () => { |
| 131 | + const cwd = repoOnBranch('FR-3665'); |
| 132 | + const missing = mkdtempSync(join(tmpdir(), 'bai-cli-portless-')); |
| 133 | + expect(devWebUiOrigin(cwd, env(missing))).toBe('https://fr-3665.localhost:1357'); |
| 134 | + |
| 135 | + const bad = mkdtempSync(join(tmpdir(), 'bai-cli-portless-')); |
| 136 | + writeFileSync(join(bad, 'routes.json'), '{not json'); |
| 137 | + expect(devWebUiOrigin(cwd, env(bad))).toBe('https://fr-3665.localhost:1357'); |
| 138 | + |
| 139 | + const wrongShape = mkdtempSync(join(tmpdir(), 'bai-cli-portless-')); |
| 140 | + writeFileSync(join(wrongShape, 'routes.json'), '{"hostname":"x"}'); |
| 141 | + expect(devWebUiOrigin(cwd, env(wrongShape))).toBe( |
| 142 | + 'https://fr-3665.localhost:1357', |
| 143 | + ); |
| 144 | + |
| 145 | + const noPid = mkdtempSync(join(tmpdir(), 'bai-cli-portless-')); |
| 146 | + writeFileSync( |
| 147 | + join(noPid, 'routes.json'), |
| 148 | + JSON.stringify([{ hostname: 'fr-3665-x.localhost', port: 1 }]), |
| 149 | + ); |
| 150 | + expect(devWebUiOrigin(cwd, env(noPid))).toBe('https://fr-3665.localhost:1357'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('has no issue key to resolve on a branch without one', () => { |
| 154 | + const cwd = repoOnBranch('main'); |
| 155 | + const state = portlessState([ |
| 156 | + { hostname: 'fr-3665-pr9049-statusline.localhost' }, |
| 157 | + ]); |
| 158 | + expect(devWebUiOrigin(cwd, env(state))).toBe('https://localhost:1357'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments