All files / src/handlers new-room.ts

100% Statements 5/5
100% Branches 8/8
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                            7x 7x 7x 7x 7x              
/**
 * `GET /_new` and `GET /=_new` — redirect to a freshly-minted room id.
 *
 * Legacy (src/main.ls:91-96):
 *   - `/=_new` → `/=<room>/edit` if KEY set, else `/=<room>`
 *   - `/_new` → `/<room>/edit` if KEY set, else `/<room>`
 *
 * Pure-logic slice; the Hono glue wraps this with `c.redirect(url, 302)`.
 * Tests assert the `Location` shape.
 */
import { generateRoomId } from '../lib/room-name.ts';
 
/** Shape of a redirect that Hono can turn into an actual `Response`. */
export interface RedirectInfo {
  readonly status: 302;
  readonly headers: {
    readonly Location: string;
  };
}
 
export interface BuildNewRoomRedirectOpts {
  readonly basepath?: string;
  readonly hasKey: boolean;
  /** true for `/=_new` (multi-sheet), false for `/_new` (single-sheet). */
  readonly multi: boolean;
  /** Injectable for determinism in tests. Defaults to `generateRoomId`. */
  readonly idGen?: () => string;
}
 
export function buildNewRoomRedirect(opts: BuildNewRoomRedirectOpts): RedirectInfo {
  const room = (opts.idGen ?? generateRoomId)();
  const basepath = opts.basepath ?? '';
  const prefix = opts.multi ? '=' : '';
  const tail = opts.hasKey ? '/edit' : '';
  return {
    status: 302,
    headers: {
      Location: `${basepath}/${prefix}${room}${tail}`,
    },
  };
}