import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("$app/env", () => ({ browser: true }));
const { markOnboardingDone, markProfileImported, onboardingDone, profileImported } =
await import("./onboarding");
const alice = "did:plc:alice";
const bob = "did:plc:bob";
const store = new Map<string, string>();
const fakeStorage = {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => void store.set(key, value),
removeItem: (key: string) => void store.delete(key)
};
beforeEach(() => {
store.clear();
vi.stubGlobal("localStorage", fakeStorage);
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe("onboarding flag", () => {
it("is per account, and absent until the flow finishes", () => {
expect(onboardingDone(alice)).toBe(false);
markOnboardingDone(alice);
expect(onboardingDone(alice)).toBe(true);
expect(onboardingDone(bob)).toBe(false);
});
it("records when it finished, not just that it did", () => {
markOnboardingDone(alice);
expect(JSON.parse(store.get(`tangled.onboarding.${alice}`)!).done).toMatch(
/^\d{4}-\d{2}-\d{2}T/
);
});
it("records the profile import as its own fact", () => {
expect(profileImported(alice)).toBe(false);
markProfileImported(alice);
expect(profileImported(alice)).toBe(true);
expect(onboardingDone(alice)).toBe(false);
markOnboardingDone(alice);
expect(profileImported(alice)).toBe(true);
});
it("still reads the bare timestamp this key used to hold", () => {
store.set(`tangled.onboarding.${alice}`, JSON.stringify("2026-09-17T00:00:00.000Z"));
expect(onboardingDone(alice)).toBe(true);
expect(profileImported(alice)).toBe(false);
});
it("treats a browser that refuses storage as not onboarded, and does not throw", () => {
vi.stubGlobal("localStorage", {
getItem: () => {
throw new Error("denied");
},
setItem: () => {
throw new Error("denied");
}
});
expect(() => markOnboardingDone(alice)).not.toThrow();
expect(onboardingDone(alice)).toBe(false);
});
});