SPB Git

Repository Public

Setup.test.ts 1.8 KB 36 lines typescript

1import { describe, expect, it, vi } from "vitest";2import { takeTokenFromFragment } from "./Setup";34function fakeWindow(url: string) {5  const u = new URL(url);6  const location = { hash: u.hash, pathname: u.pathname, search: u.search };7  const replaceState = vi.fn((_s: unknown, _t: string, next: string) => { location.hash = ""; void next; });8  return { win: { location, history: { replaceState } } as unknown as Pick<Window, "location" | "history">, replaceState };9}1011describe("takeTokenFromFragment", () => {12  it("returns the token and removes the fragment from the current history entry", () => {13    const token = "Zm9vYmFyLXRva2VuLTMyLWJ5dGVzLWJhc2U2NC1fLTE";14    const { win, replaceState } = fakeWindow(`https://clustr.spboucher.ai/setup#${token}`);15    expect(takeTokenFromFragment(win)).toBe(token);16    expect(replaceState).toHaveBeenCalledWith(null, "", "/setup");17  });18  it("keeps the query string but never the fragment", () => {19    const { win, replaceState } = fakeWindow("http://localhost:8750/setup?lang=en#abcdefghijklmnopqrstuvwxyz012345");20    takeTokenFromFragment(win);21    expect(replaceState).toHaveBeenCalledWith(null, "", "/setup?lang=en");22  });23  it("returns null without touching history when there is no fragment", () => {24    const { win, replaceState } = fakeWindow("https://clustr.spboucher.ai/setup");25    expect(takeTokenFromFragment(win)).toBeNull();26    expect(replaceState).not.toHaveBeenCalled();27  });28  it("still clears a malformed fragment and reports it as unusable", () => {29    for (const bad of ["short", "has spaces in the token value", "<script>alert(1)</script>aaaaaaaa", "a".repeat(600)]) {30      const { win, replaceState } = fakeWindow(`https://clustr.spboucher.ai/setup#${encodeURIComponent(bad)}`);31      expect(takeTokenFromFragment(win)).toBe("");32      expect(replaceState).toHaveBeenCalledTimes(1);33    }34  });35});36
Files main
Menu

Theme