|
5 | 5 | * |
6 | 6 | * Copyright Oxide Computer Company |
7 | 7 | */ |
8 | | -import { matchRoutes } from 'react-router' |
| 8 | +import { matchRoutes, type RouteObject } from 'react-router' |
9 | 9 | import * as R from 'remeda' |
10 | 10 | import { expect, test } from 'vitest' |
11 | 11 |
|
| 12 | +import { nexusConsoleRoutes } from '~/api/__generated__/nexus-console' |
12 | 13 | import { matchesToCrumbs } from '~/hooks/use-crumbs' |
13 | 14 | import { routes } from '~/routes' |
14 | 15 |
|
@@ -224,3 +225,96 @@ test('every page reachable by breadcrumb should have a self-referential breadcru |
224 | 225 | expect(dropFinalSlash(path)).toEqual(dropFinalSlash(last.path)) |
225 | 226 | } |
226 | 227 | }) |
| 228 | + |
| 229 | +// Full path of every leaf route, with optional segments expanded both ways. |
| 230 | +// Only leaves are pages: a route with children renders at its own path only |
| 231 | +// through an index child, which is itself a leaf. |
| 232 | +function routePaths(routes: RouteObject[], parent = ''): string[] { |
| 233 | + return routes.flatMap((route) => { |
| 234 | + const full = route.path?.startsWith('/') |
| 235 | + ? route.path |
| 236 | + : [parent, route.path].filter(Boolean).join('/') |
| 237 | + if (route.children) return routePaths(route.children, full) |
| 238 | + return expandOptional(full.split('/').filter(Boolean)).map((s) => '/' + s.join('/')) |
| 239 | + }) |
| 240 | +} |
| 241 | + |
| 242 | +const expandOptional = (segments: string[]): string[][] => |
| 243 | + segments.reduce<string[][]>( |
| 244 | + (acc, seg) => |
| 245 | + seg.endsWith('?') |
| 246 | + ? [...acc, ...acc.map((prefix) => [...prefix, seg.slice(0, -1)])] |
| 247 | + : acc.map((prefix) => [...prefix, seg]), |
| 248 | + [[]] |
| 249 | + ) |
| 250 | + |
| 251 | +// Port of dropshot's lookup_route for a single template. Dropshot splits paths |
| 252 | +// on `/` and drops empty segments, then walks a trie: `{name}` matches one |
| 253 | +// segment and `{name:.*}` matches the rest, including nothing, so |
| 254 | +// /projects/{path:.*} serves /projects. Checking templates one at a time gives |
| 255 | +// the same answer as the trie because dropshot refuses to register a node with |
| 256 | +// both literal and variable children, so a path matches at most one route. |
| 257 | +// https://github.com/oxidecomputer/dropshot/blob/4ff9cb3/dropshot/src/router.rs#L438-L503 |
| 258 | +// https://github.com/oxidecomputer/dropshot/blob/4ff9cb3/dropshot/src/router.rs#L254-L270 |
| 259 | +function nexusServes(template: string, pathname: string) { |
| 260 | + const want = template.split('/').filter(Boolean) |
| 261 | + const have = pathname.split('/').filter(Boolean) |
| 262 | + for (const [i, seg] of want.entries()) { |
| 263 | + if (/^\{\w+:\.\*\}$/.test(seg)) return true |
| 264 | + if (i >= have.length) return false |
| 265 | + // a route param like :project only matches a Nexus param, not a literal |
| 266 | + if (!/^\{\w+\}$/.test(seg) && seg !== have[i]) return false |
| 267 | + } |
| 268 | + return want.length === have.length |
| 269 | +} |
| 270 | + |
| 271 | +// The Nexus test below is only as good as nexusServes, so check that it matches |
| 272 | +// paths the same way dropshot does. These are the cases from dropshot's router |
| 273 | +// tests, plus the empty wildcard match, which dropshot doesn't test but the |
| 274 | +// console relies on for paths like /projects. |
| 275 | +// https://github.com/oxidecomputer/dropshot/blob/4ff9cb3/dropshot/src/router.rs#L1242-L1643 |
| 276 | +test.each([ |
| 277 | + ['/', '/', true], |
| 278 | + ['/', '//', true], |
| 279 | + ['/foo', '/foo', true], |
| 280 | + ['/foo', '/foo/', true], |
| 281 | + ['/foo', '//foo//', true], |
| 282 | + ['/foo', '/', false], |
| 283 | + ['/not{a}variable', '/not{a}variable', true], |
| 284 | + ['/not{a}variable', '/not{b}variable', false], |
| 285 | + ['/projects/{project_id}', '/projects', false], |
| 286 | + ['/projects/{project_id}', '/projects/', false], |
| 287 | + ['/projects/{project_id}', '/projects/p12345', true], |
| 288 | + ['/projects/{project_id}', '/projects/p12345/', true], |
| 289 | + ['/projects/{project_id}', '/projects///p12345//', true], |
| 290 | + ['/projects/{project_id}', '/projects/p12345/child', false], |
| 291 | + [ |
| 292 | + '/projects/{project_id}/instances/{instance_id}/fwrules/{fwrule_id}/info', |
| 293 | + '/projects/p1/instances/i2/fwrules/fw3/info', |
| 294 | + true, |
| 295 | + ], |
| 296 | + ['/projects/{project_id}/instances', '/projects/instances', false], |
| 297 | + ['/projects/{project_id}/instances', '/projects//instances', false], |
| 298 | + ['/projects/{project_id}/instances', '/projects/foo/instances', true], |
| 299 | + ['/console/{path:.*}', '/console/missiles/launch', true], |
| 300 | + ['/console/{path:.*}', '/console', true], |
| 301 | + ['/console/{path:.*}', '/', false], |
| 302 | + // console route params only match Nexus params, not literals |
| 303 | + ['/projects/{project_id}', '/projects/:project', true], |
| 304 | + ['/projects/new', '/projects/:project', false], |
| 305 | +])('nexusServes(%s, %s) is %s', (template, pathname, expected) => { |
| 306 | + expect(nexusServes(template, pathname)).toBe(expected) |
| 307 | +}) |
| 308 | + |
| 309 | +// Nexus only serves index.html on paths it knows about, so a console route |
| 310 | +// outside them works on client-side navigation but 404s on reload or direct |
| 311 | +// link. If this fails, add an endpoint for the new path in omicron next to the |
| 312 | +// existing ones, then bump OMICRON_VERSION once it merges. |
| 313 | +// https://github.com/oxidecomputer/omicron/blob/7e18e52/nexus/external-api/src/lib.rs#L9206 |
| 314 | +test('Nexus serves the console on every route', () => { |
| 315 | + const unserved = R.unique(routePaths(routes)) |
| 316 | + // the catch-all is for unknown paths, which Nexus is right to 404 |
| 317 | + .filter((p) => !p.split('/').includes('*')) |
| 318 | + .filter((p) => !nexusConsoleRoutes.some((t) => nexusServes(t, p))) |
| 319 | + expect(unserved).toEqual([]) |
| 320 | +}) |
0 commit comments