~james/core

core/web/src/lib/auth/onboarding.test.ts -rw-r--r-- 2.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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);
	});
});