mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 10:48:20 +01:00
Drops the "isReturning" gate in decideHealPatch so the heal fires on first sign-in too. When a user signs in with OAuth (GitHub / Google / Twitter) and we can derive a unique username from their display name or email local-part, we set it + onboardedAt right there in the JWT callback. They land authenticated with onboardingComplete=true and middleware sends them straight to /control instead of via /onboarding. User-visible effect: GitHub OAuth → /control in ONE page load instead of two. The /control empty state's GitHubRepoSuggestions / LocalDev- Suggestions / FleetRunnerAutoMint then take over from there. Fallback (still works): users whose suggested username collides with an existing one, or whose display name + email can't yield ≥2 chars of [a-z0-9-], get null patch → middleware sends them to /onboarding to pick manually. The page is no longer a forced gate — just a fallback for the edge cases. Side benefit: also skips the wasted "onboardedAt-only" write that the old logic would do for returning users without a valid username, since onboardedAt without a valid username is a no-op (isOnboardingComplete still gates on both). The /settings → Profile editor lets users change the auto-picked username later, so we're not stealing their choice — just defaulting it. Tests: 7/7 pass (npm run test:onboarding-heal). Updated existing brand-new-user assertion to reflect the new policy and added a collision-fallback test case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
91 lines
3.5 KiB
TypeScript
91 lines
3.5 KiB
TypeScript
/**
|
|
* Inline self-tests for the heal decision logic.
|
|
* Run: npm run test:onboarding-heal
|
|
*
|
|
* Covers the four branches of decideHealPatch — the pure, DB-free decision
|
|
* extracted from healReturningUserOnboarding so it's testable without spinning
|
|
* up Postgres. The throw-safety of the surrounding wrapper is verified by
|
|
* inspection (try/catch around every await — see onboarding-heal.ts).
|
|
*/
|
|
import { decideHealPatch } from "@/lib/onboarding-heal-decision";
|
|
|
|
function assert(condition: boolean, message: string): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
const newUser = {
|
|
name: "Jane Doe",
|
|
email: "jane@example.com",
|
|
username: null,
|
|
onboardedAt: null,
|
|
} as const;
|
|
|
|
function runTests(): void {
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed += 1;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
check("brand-new user + free suggested username → auto-heal (sets both)", () => {
|
|
const patch = decideHealPatch(newUser, 0, true);
|
|
assert(patch !== null, "expected auto-heal patch on first sign-in");
|
|
assert(patch!.username === "jane-doe", `expected jane-doe, got ${patch!.username}`);
|
|
assert(patch!.onboardedAt instanceof Date, "expected onboardedAt date");
|
|
});
|
|
|
|
check("brand-new user + suggested username taken → no heal (manual /onboarding fallback)", () => {
|
|
const patch = decideHealPatch(newUser, 0, false);
|
|
assert(patch === null, "expected null patch when no username can be derived");
|
|
});
|
|
|
|
check("returning user with projects + no username + suggestion free → patch sets both", () => {
|
|
const patch = decideHealPatch(newUser, 3, true);
|
|
assert(patch !== null, "expected patch");
|
|
assert(patch!.username === "jane-doe", `expected jane-doe, got ${patch!.username}`);
|
|
assert(patch!.onboardedAt instanceof Date, "expected onboardedAt date");
|
|
});
|
|
|
|
check("returning user with projects + suggested username taken → no heal (avoid wasted write)", () => {
|
|
// Without a valid username, setting onboardedAt is a no-op because
|
|
// isOnboardingComplete still gates on hasValidUsername. Skip the write
|
|
// and let the user fall through to /onboarding to pick manually.
|
|
const patch = decideHealPatch(newUser, 3, false);
|
|
assert(patch === null, "expected null patch — no username, no point setting onboardedAt");
|
|
});
|
|
|
|
check("already-onboarded user with valid username → no heal needed", () => {
|
|
const patch = decideHealPatch(
|
|
{ name: "Jane Doe", email: "jane@example.com", username: "jane-doe", onboardedAt: new Date() },
|
|
5,
|
|
true,
|
|
);
|
|
assert(patch === null, "expected null patch when user is fully set up");
|
|
});
|
|
|
|
check("user with onboardedAt but no username (legacy migrated row) → patch sets username only", () => {
|
|
const patch = decideHealPatch(
|
|
{ name: "Jane Doe", email: "jane@example.com", username: null, onboardedAt: new Date("2025-01-01") },
|
|
0,
|
|
true,
|
|
);
|
|
assert(patch !== null, "expected patch");
|
|
assert(patch!.username === "jane-doe", `expected jane-doe, got ${patch!.username}`);
|
|
assert(patch!.onboardedAt === undefined, "expected no onboardedAt when already set");
|
|
});
|
|
|
|
check("zero projects + onboardedAt set → still treated as returning", () => {
|
|
const patch = decideHealPatch(
|
|
{ name: "Jane Doe", email: "jane@example.com", username: null, onboardedAt: new Date() },
|
|
0,
|
|
true,
|
|
);
|
|
assert(patch !== null, "onboardedAt alone should mark user as returning");
|
|
});
|
|
|
|
console.log(`\n${passed}/${passed} passed`);
|
|
}
|
|
|
|
runTests();
|