-
Notifications
You must be signed in to change notification settings - Fork 1
fix: persist post-auth valuation intent to sessionStorage so the run resumes after the OTP #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sweetmantech
wants to merge
1
commit into
main
Choose a base branch
from
fix/valuation-intent-resume
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { clearPendingIntent } from "../clearPendingIntent"; | ||
| import { readPendingIntent } from "../readPendingIntent"; | ||
| import { savePendingIntent } from "../savePendingIntent"; | ||
| import { fakeSessionStorage } from "./fakeSessionStorage"; | ||
|
|
||
| const artist = { id: "spotify-123", name: "Del Water Gap" }; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("clearPendingIntent", () => { | ||
| it("removes a stored intent", () => { | ||
| vi.stubGlobal("sessionStorage", fakeSessionStorage()); | ||
| savePendingIntent(artist, 1_000); | ||
| clearPendingIntent(); | ||
| expect(readPendingIntent(2_000)).toBeNull(); | ||
| }); | ||
|
|
||
| it("is a no-op when sessionStorage is unavailable (SSR)", () => { | ||
| expect(() => clearPendingIntent()).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Map-backed Storage stand-in so the pending-intent helpers can be tested in | ||
| * Node (vitest runs without a DOM, so there is no real `sessionStorage`). | ||
| */ | ||
| export function fakeSessionStorage(): Storage { | ||
| const store = new Map<string, string>(); | ||
| return { | ||
| getItem: (key: string) => store.get(key) ?? null, | ||
| setItem: (key: string, value: string) => { | ||
| store.set(key, String(value)); | ||
| }, | ||
| removeItem: (key: string) => { | ||
| store.delete(key); | ||
| }, | ||
| clear: () => store.clear(), | ||
| key: (index: number) => [...store.keys()][index] ?? null, | ||
| get length() { | ||
| return store.size; | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { readPendingIntent } from "../readPendingIntent"; | ||
| import { savePendingIntent } from "../savePendingIntent"; | ||
| import { | ||
| PENDING_INTENT_KEY, | ||
| PENDING_INTENT_MAX_AGE_MS, | ||
| } from "../pendingIntent"; | ||
| import { fakeSessionStorage } from "./fakeSessionStorage"; | ||
|
|
||
| const artist = { | ||
| id: "spotify-123", | ||
| name: "Del Water Gap", | ||
| image: "https://i.scdn.co/x.jpg", | ||
| followers: 42, | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("readPendingIntent", () => { | ||
| it("returns null when nothing is stored", () => { | ||
| vi.stubGlobal("sessionStorage", fakeSessionStorage()); | ||
| expect(readPendingIntent()).toBeNull(); | ||
| }); | ||
|
|
||
| it("round-trips an artist saved by savePendingIntent", () => { | ||
| vi.stubGlobal("sessionStorage", fakeSessionStorage()); | ||
| savePendingIntent(artist, 1_000); | ||
| expect(readPendingIntent(2_000)).toEqual(artist); | ||
| }); | ||
|
|
||
| it("returns the intent just inside the max age", () => { | ||
| vi.stubGlobal("sessionStorage", fakeSessionStorage()); | ||
| savePendingIntent(artist, 1_000); | ||
| expect(readPendingIntent(1_000 + PENDING_INTENT_MAX_AGE_MS)).toEqual( | ||
| artist, | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores intents older than the max age", () => { | ||
| vi.stubGlobal("sessionStorage", fakeSessionStorage()); | ||
| savePendingIntent(artist, 1_000); | ||
| expect(readPendingIntent(1_001 + PENDING_INTENT_MAX_AGE_MS)).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for malformed JSON", () => { | ||
| const storage = fakeSessionStorage(); | ||
| storage.setItem(PENDING_INTENT_KEY, "{not json"); | ||
| vi.stubGlobal("sessionStorage", storage); | ||
| expect(readPendingIntent()).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when the stored shape is invalid", () => { | ||
| const storage = fakeSessionStorage(); | ||
| storage.setItem( | ||
| PENDING_INTENT_KEY, | ||
| JSON.stringify({ artist: { name: "no id" }, savedAt: 1_000 }), | ||
| ); | ||
| vi.stubGlobal("sessionStorage", storage); | ||
| expect(readPendingIntent(2_000)).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when the timestamp is missing", () => { | ||
| const storage = fakeSessionStorage(); | ||
| storage.setItem(PENDING_INTENT_KEY, JSON.stringify({ artist })); | ||
| vi.stubGlobal("sessionStorage", storage); | ||
| expect(readPendingIntent(2_000)).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when sessionStorage is unavailable (SSR)", () => { | ||
| expect(readPendingIntent()).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { savePendingIntent } from "../savePendingIntent"; | ||
| import { PENDING_INTENT_KEY } from "../pendingIntent"; | ||
| import { fakeSessionStorage } from "./fakeSessionStorage"; | ||
|
|
||
| const artist = { id: "spotify-123", name: "Del Water Gap" }; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("savePendingIntent", () => { | ||
| it("persists the artist and timestamp under the namespaced key", () => { | ||
| const storage = fakeSessionStorage(); | ||
| vi.stubGlobal("sessionStorage", storage); | ||
|
|
||
| savePendingIntent(artist, 1_000); | ||
|
|
||
| const raw = storage.getItem(PENDING_INTENT_KEY); | ||
| expect(raw).not.toBeNull(); | ||
| expect(JSON.parse(raw as string)).toEqual({ artist, savedAt: 1_000 }); | ||
| }); | ||
|
|
||
| it("is a no-op when sessionStorage is unavailable (SSR)", () => { | ||
| expect(() => savePendingIntent(artist)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("swallows storage write errors (private mode quotas)", () => { | ||
| vi.stubGlobal("sessionStorage", { | ||
| ...fakeSessionStorage(), | ||
| setItem: () => { | ||
| throw new Error("QuotaExceededError"); | ||
| }, | ||
| }); | ||
| expect(() => savePendingIntent(artist)).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { PENDING_INTENT_KEY } from "@/lib/valuation/pendingIntent"; | ||
|
|
||
| /** | ||
| * Drop the persisted post-auth valuation intent — after a resume consumes it, | ||
| * or when the user clears their pick (matching the ref-clearing semantic). | ||
| */ | ||
| export function clearPendingIntent(): void { | ||
| try { | ||
| if (typeof sessionStorage === "undefined") return; | ||
| sessionStorage.removeItem(PENDING_INTENT_KEY); | ||
| } catch { | ||
| // Storage unavailable — nothing to clear. | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { Artist } from "@/components/valuation/types"; | ||
|
|
||
| /** | ||
| * The "run valuation after auth" intent persisted at the sign-in gate | ||
| * (chat#1850). It lives in sessionStorage — not a React ref — because fresh | ||
| * signups churn enough state to remount the valuation component mid-auth, | ||
| * and a ref dies with its component instance. Versioned key per the | ||
| * `recoupable-theme:v1` convention. | ||
| */ | ||
| export const PENDING_INTENT_KEY = "recoupable-valuation-pending-intent:v1"; | ||
|
|
||
| /** Ignore intents older than this — a stale tab shouldn't auto-run a valuation. */ | ||
| export const PENDING_INTENT_MAX_AGE_MS = 15 * 60 * 1000; | ||
|
|
||
| export type PendingIntent = { artist: Artist; savedAt: number }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { Artist } from "@/components/valuation/types"; | ||
| import { | ||
| PENDING_INTENT_KEY, | ||
| PENDING_INTENT_MAX_AGE_MS, | ||
| } from "@/lib/valuation/pendingIntent"; | ||
|
|
||
| /** | ||
| * Read the persisted post-auth valuation intent. Returns null when absent, | ||
| * malformed, stale (> max age — a stale tab must not auto-run), or when | ||
| * storage is unavailable (SSR / private mode). Read-only: the caller clears. | ||
| */ | ||
| export function readPendingIntent(now: number = Date.now()): Artist | null { | ||
| try { | ||
| if (typeof sessionStorage === "undefined") return null; | ||
| const raw = sessionStorage.getItem(PENDING_INTENT_KEY); | ||
| if (!raw) return null; | ||
| const parsed: unknown = JSON.parse(raw); | ||
| if (typeof parsed !== "object" || parsed === null) return null; | ||
| const { artist, savedAt } = parsed as Partial<{ | ||
| artist: Artist; | ||
| savedAt: number; | ||
| }>; | ||
| if (typeof artist?.id !== "string" || typeof artist.name !== "string") { | ||
| return null; | ||
| } | ||
| if (typeof savedAt !== "number") return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Malformed timestamps can bypass the expiry guard and still resume a valuation intent. The Prompt for AI agents |
||
| if (now - savedAt > PENDING_INTENT_MAX_AGE_MS) return null; | ||
| return artist; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { Artist } from "@/components/valuation/types"; | ||
| import { PENDING_INTENT_KEY } from "@/lib/valuation/pendingIntent"; | ||
|
|
||
| /** | ||
| * Persist the picked artist before opening the auth modal so the run can | ||
| * resume even if the component remounts (or the page reloads) mid-auth. | ||
| * Best-effort: storage failures fall back to the in-memory ref path. | ||
| */ | ||
| export function savePendingIntent( | ||
| artist: Artist, | ||
| now: number = Date.now(), | ||
| ): void { | ||
| try { | ||
| if (typeof sessionStorage === "undefined") return; | ||
| sessionStorage.setItem( | ||
| PENDING_INTENT_KEY, | ||
| JSON.stringify({ artist, savedAt: now }), | ||
| ); | ||
| } catch { | ||
| // Private-mode quota / disabled storage — the ref fast path still works. | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Stale signed-out intents can still auto-run when the component instance survives auth, because
pendingRun.currentbypasses the 15-minute expiry enforced only byreadPendingIntent(). Consider storing{ artist, savedAt }in the ref or validating the ref against the same max-age before preferring it.Prompt for AI agents