Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/navigation/__tests__/link-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ describe('an id is taken exactly as it came', () => {
});
});

describe('/command-center/<section> is held to the same limits as ?tab=<section>', () => {
it('reads a section of PARAM_VALUE_MAX_LENGTH at the most: Studio at the cap, nothing one over', () => {
const section = (length: number) => `/command-center/${'a'.repeat(length)}`;
expect(appRouteForWebLink(section(PARAM_VALUE_MAX_LENGTH), WEB)).toEqual({
external: `${WEB}${section(PARAM_VALUE_MAX_LENGTH)}`,
});
expect(appRouteForWebLink(section(PARAM_VALUE_MAX_LENGTH + 1), WEB)).toBeNull();
expect(open(`${WEB}${section(PARAM_VALUE_MAX_LENGTH)}`)).toBe(routes.lost);
expect(open(`${WEB}${section(PARAM_VALUE_MAX_LENGTH + 1)}`)).toBe(routes.lost);
});

it('refuses more than the query pairs cap, a repeated task_id or question, and any tab', () => {
const extra = Array.from({ length: QUERY_PAIRS_MAX }, (_, at) => `x${at}=1`).join('&');
for (const link of [
`${WEB}/command-center/board?task_id=1&${extra}`,
`${WEB}/command-center/board?task_id=1&task_id=2`,
`${WEB}/command-center/board?task_id=1&tas%6B_id=2`,
`${WEB}/command-center/board?question=a&question=b`,
`${WEB}/command-center/board?tab=board`,
`${WEB}/command-center/board?tab=board&tab=board`,
]) {
expect(open(link)).toBe(routes.lost);
}
expect(appRouteForWebLink('/command-center/board?task_id=1&task_id=2', WEB)).toBeNull();
expect(appRouteForWebLink('/command-center/board?tab=board', WEB)).toBeNull();
});

it('takes a task id exactly as it came: padded, non-numeric or double-encoded opens the Board', () => {
for (const id of ['%20419', '419%20', 'abc', '4%2F1', '4%252F1']) {
expect(open(`${WEB}/command-center/board?task_id=${id}`)).toBe(routes.cc.board);
expect(open(`${WEB}/command-center?tab=board&task_id=${id}`)).toBe(routes.cc.board);
}
expect(open(`${WEB}/command-center/board?task_id=419&question=%09g-9`)).toBe(
routes.cc.ticket(419),
);
});
});

describe("Expo Go's addresses open Lost in a release build", () => {
it('opens Lost for exp://, exps:// and the /--/ form in any scheme or path', () => {
for (const link of [
Expand Down
55 changes: 55 additions & 0 deletions src/navigation/__tests__/system-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,61 @@ describe('appPathForSystemLink: a web link opens the screen it means', () => {
});
});

describe('appPathForSystemLink: /command-center/<section> opens what ?tab=<section> opens', () => {
it('opens the same screen as the tab form, from https, automatos:// or a path', () => {
const pairs: readonly (readonly [string, string, string])[] = [
['/command-center/board', '/command-center?tab=board', routes.cc.board],
['/command-center/summary', '/command-center?tab=summary', routes.cc.board],
['/command-center/questions', '/command-center?tab=questions', routes.cc.questions],
['/command-center/calendar', '/command-center?tab=calendar', routes.cc.calendar],
[
'/command-center/board?task_id=419',
'/command-center?tab=board&task_id=419',
routes.cc.ticket(419),
],
[
'/command-center/board?task_id=419&question=g-9',
'/command-center?tab=board&task_id=419&question=g-9',
routes.cc.ticket(419, 'g-9'),
],
];
for (const [section, tab, route] of pairs) {
for (const prefix of [WEB, 'automatos://', '']) {
const join = (path: string) => `${prefix}${prefix === 'automatos://' ? path.slice(1) : path}`;
expect(open(join(section))).toBe(route);
expect(open(join(tab))).toBe(route);
}
}
});

it('opens Lost for the sections the phone has no screen for, as the tab form does', () => {
for (const section of ['activity', 'feed', 'history', 'watchlist', 'governance', 'unknown']) {
expect(open(`${WEB}/command-center/${section}`)).toBe(routes.lost);
expect(open(`${WEB}/command-center?tab=${section}`)).toBe(routes.lost);
}
});

it('opens Lost for a hostile section or query', () => {
for (const link of [
`${WEB}/command-center/..`,
`${WEB}/command-center/board/x`,
`${WEB}/command-center/board%2Fx`,
`${WEB}/command-center/bo%61rd`,
`${WEB}/command-center/board?tab=calendar`,
`${WEB}/command-center/board?task_id=1&task_id=2`,
`automatos://command-center/${'a'.repeat(LINK_MAX_LENGTH)}`,
]) {
expect(open(link)).toBe(routes.lost);
}
});

it('opens the Board for a task id that is no row id, as the tab form does', () => {
for (const id of ['abc', '%20419', '%230419']) {
expect(open(`${WEB}/command-center/board?task_id=${id}`)).toBe(routes.cc.board);
}
});
});

describe('appPathForSystemLink: the pairing link opens its screen once it reads as one', () => {
it('opens the pairing screen with the server and the code, from https, automatos:// or a path', () => {
for (const link of [
Expand Down
74 changes: 74 additions & 0 deletions src/navigation/__tests__/web-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,69 @@ describe('appRouteForWebLink: the web links the phone has screens for', () => {
});
});

describe('appRouteForWebLink: /command-center/<section> opens what ?tab=<section> opens', () => {
it('opens the Board, Questions and Calendar sections, and Summary on the Board', () => {
expect(open('/command-center/board')).toEqual({ app: routes.cc.board });
expect(open('/command-center/summary')).toEqual({ app: routes.cc.board });
expect(open('/command-center/questions')).toEqual({ app: routes.cc.questions });
expect(open('/command-center/calendar')).toEqual({ app: routes.cc.calendar });
expect(open('/command-center/calendar/#today')).toEqual({ app: routes.cc.calendar });
});

it('opens a ticket, and the question it opens at, by the same ids as the tab form', () => {
expect(open('/command-center/board?task_id=42')).toEqual({ app: routes.cc.ticket(42) });
expect(open('/command-center/board?task_id=42&question=g-9')).toEqual({
app: routes.cc.ticket(42, 'g-9'),
});
expect(open('/command-center/board?question=g-9')).toEqual({ app: routes.cc.questions });
expect(open('/command-center/board?task_id=42&runtime=1')).toEqual({
app: routes.cc.ticket(42),
});
});

it('keeps the tab form opening what it opened', () => {
expect(open('/command-center?tab=board&task_id=42&question=g-9')).toEqual({
app: routes.cc.ticket(42, 'g-9'),
});
expect(open('/command-center?tab=questions')).toEqual({ app: routes.cc.questions });
expect(open('/command-center?tab=calendar')).toEqual({ app: routes.cc.calendar });
});

it('opens the Board for a task id that is no row id, as the tab form does', () => {
for (const id of ['%230419', 'abc', '%20419', '']) {
expect(open(`/command-center/board?task_id=${id}`)).toEqual(
open(`/command-center?tab=board&task_id=${id}`),
);
expect(open(`/command-center/board?task_id=${id}`)).toEqual({ app: routes.cc.board });
}
});

it('refuses a link whose query names tab: two sections in one link', () => {
for (const link of [
'/command-center/board?tab=calendar',
'/command-center/board?task_id=42&tab=board',
'/command-center/calendar?ta%62=board',
'/command-center/board?tab=%E0%A4%A',
]) {
expect(open(link)).toBeNull();
}
});

it('opens Studio for a section that is no tab: a second segment, dots, an encoded slash', () => {
for (const section of ['board/x', '..', '.', 'board%2Fx', 'bo%61rd', 'Board', 'board%00']) {
expect(open(`/command-center/${section}`)).toEqual({
external: `${WEB}/command-center/${section}`,
});
}
expect(open('/command-center//board')).toEqual({ external: `${WEB}/command-center//board` });
});

it('refuses a repeated task_id, as the tab form does', () => {
expect(open('/command-center/board?task_id=1&task_id=2')).toBeNull();
expect(open('/command-center?tab=board&task_id=1&task_id=2')).toBeNull();
});
});

describe('appRouteForWebLink: everything else opens Studio in the system browser', () => {
it('sends an agent link to EXPO_PUBLIC_WEB_URL + the path, query and all', () => {
expect(open('/agents?agent=a-1&panel=reports')).toEqual({
Expand All @@ -87,6 +150,17 @@ describe('appRouteForWebLink: everything else opens Studio in the system browser
}
});

it('sends the section addresses the phone has no screen for to Studio, as the tab form goes', () => {
for (const section of ['activity', 'feed', 'history', 'watchlist', 'governance', 'unknown']) {
expect(open(`/command-center/${section}`)).toEqual({
external: `${WEB}/command-center/${section}`,
});
expect(open(`/command-center?tab=${section}`)).toEqual({
external: `${WEB}/command-center?tab=${section}`,
});
}
});

it('sends a page under a mapped one to Studio (the Deliverables explorer)', () => {
expect(open('/deliverables/explorer?path=reports')).toEqual({
external: `${WEB}/deliverables/explorer?path=reports`,
Expand Down
48 changes: 46 additions & 2 deletions src/navigation/web-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// name it reads given twice, or a value longer than PARAM_VALUE_MAX_LENGTH refuses the whole link;
// any other name is ignored, since the route is rebuilt from the names the table reads. Every id
// is held to isUsableId (routes.ts) exactly as it came: a padded one is refused, never trimmed.
// Command Center links come in two forms that open the same screen: the old `?tab=<section>` (kept
// in stored rows forever) and the web's own addresses `/command-center/<section>` (core PRD-292),
// whose section is taken as written and whose query may not name `tab`.
// Parsed by hand, not with URL: React Native's URL is a partial polyfill.

import { withoutTrailingSlash } from '../api/url';
Expand Down Expand Up @@ -159,12 +162,51 @@ function boardRoute(query: Query): string {
return questionId ? routes.cc.questions : routes.cc.board;
}

function commandCentreRoute({ query }: ReadLink): string | null {
const tab = query.get('tab') || 'summary';
/** A tab's route (the same table for `?tab=<tab>` and `/command-center/<tab>`); null: no screen. */
function tabRoute(tab: string, query: Query): string | null {
if (BOARD_TABS.has(tab)) return boardRoute(query);
return TAB_ROUTES.get(tab) ?? null;
}

function commandCentreRoute({ query }: ReadLink): string | null {
return tabRoute(query.get('tab') || 'summary', query);
}

const COMMAND_CENTRE_PATH = '/command-center';
const COMMAND_CENTRE_SECTION_NAMES: readonly string[] = ['task_id', 'question'];

/**
* The section of a /command-center/<section> link (the web's own addresses for its tabs, core
* PRD-292): the one segment after the path, as written (never decoded, so an encoded slash or
* '%62oard' is no section), or null when the path is not that form. A path with a second segment
* has a section with a slash in it, which no tab is, so it is the unknown tab's answer.
*/
function commandCentreSection(path: string): string | null {
const prefix = `${COMMAND_CENTRE_PATH}/`;
return path.startsWith(prefix) ? path.slice(prefix.length) : null;
}

/**
* /command-center/<section>[?task_id=<row id>[&question=<grant id>]]: the screen `?tab=<section>`
* opens, by the same limits (readQuery's). Its query may not carry `tab` at all (the stricter of
* ignoring it and refusing the link: two sections in one link is how two readers disagree), and a
* section longer than PARAM_VALUE_MAX_LENGTH is refused as a `tab` value that long is.
*/
function sectionAnswer(link: SplitLink, section: string, from: From): Answer {
const entry = LINKS.get(COMMAND_CENTRE_PATH);
if (entry === undefined || !entry.from.includes(from)) return 'unmapped';
if (section.length > PARAM_VALUE_MAX_LENGTH) return 'refused';
const query = readQuery(link.search, COMMAND_CENTRE_SECTION_NAMES);
if (query === null || namesTab(link.search)) return 'refused';
const route = tabRoute(section, query);
return route === null ? 'unmapped' : { route };
}

/** Whether the query names `tab` (written or escaped), even with a value that cannot be decoded. */
function namesTab(search: string): boolean {
return search !== '' && search.split('&').some((part) => pairOf(part).name === 'tab');
}

/** /deliverables?deliverable=<id> (DELIVERABLE_PARAM), or the gallery. */
function deliverablesRoute({ query }: ReadLink): string {
const id = idParam(query, 'deliverable');
Expand Down Expand Up @@ -210,6 +252,8 @@ const LINKS: ReadonlyMap<string, Entry> = new Map<string, Entry>([
]);

function routeFor(link: SplitLink, from: From): Answer {
const section = commandCentreSection(link.path);
if (section !== null) return sectionAnswer(link, section, from);
const entry = LINKS.get(link.path);
if (entry === undefined || !entry.from.includes(from)) return 'unmapped';
const query = readQuery(link.search, entry.reads);
Expand Down
Loading