Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3adfac34aa5faadc17b89edfc8e8e7b0be872da0:e2e/core-flow.spec.ts:generic-api-key:10
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to loopkit are documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [0.8.1] - 2026-12-02

### Added

- Playwright core-flow e2e: a vendor creates a stamp card program, a customer joins from the shop link with a phone number and a birthday, and the vendor stamps that card at the counter and sees the count update. The Supabase stand-in now keeps state across requests (table functions, RPC handlers).

## [0.8.0] - 2026-12-02

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ See `AGENTS.md` for engineering rules and skills. `AGENTS.md` is kept under 200
- `.github/` — CI workflows
- `.husky/` — git hook scripts (pre-commit/commit-msg/pre-push)
- `docs/` — deploy runbook, superpowers specs/plans, CONSTITUTION
- `e2e/` — Playwright end-to-end smoke + signed-out route-protection tests (both run without Supabase provisioning)
- `e2e/` — Playwright end-to-end smoke, signed-out route-protection and core vendor/customer flow tests (all run without Supabase provisioning)
- `src/` — application source (App Router pages, lib, components)
- `supabase/` — SQL migrations and seed data
- `test/` — Vitest unit/integration tests
Expand Down
3 changes: 2 additions & 1 deletion e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Playwright end-to-end smoke tests — runnable with only `pnpm dev` +
- `route-protection.spec.ts` — signed-out route protection: `/dashboard`, `/dashboard/customers`, and `/setup` redirect to `/login`; `/admin` 404s rather than revealing the route exists; `/c` without `?v=` and `/earn` without `?order=` (copy: "This link is incomplete. Ask the shop for a new one.") render their no-DB-call fallback copy instead of attempting a lookup. Like `smoke.spec.ts`, runnable without Supabase provisioning — middleware's `getUser()` resolves `user: null` locally with no session cookie, so these gates fire without a live DB.
- `smoke.spec.ts` — public smoke: the landing page renders its hero heading and "Join the waitlist" link; `/login` renders its "Continue with Google" button. The hero heading locator (here and in `csp.spec.ts`) accepts either hyphen in "one-time", because the hero uses a non-breaking hyphen (U+2011)
- `signed-in-smoke.spec.ts`: signed in as an admin vendor with MFA cleared (`aal2`), `renderSmoke` over every dashboard (including the Customers referrals route and the old referrals/settings redirects), setup (`/setup` and `/setup/new`) and admin page, failing on a 5xx, error boundary, page error, CSP violation or 404; a password-only (`aal1`) admin session is sent to `/mfa`. It starts the stand-in below itself and runs in its own Playwright project after the others. It sees empty data, not seeded rows.
- `mock-supabase.ts`: not a spec; a small `http` server standing in for GoTrue and PostgREST, plus `signedInCookie()`, which builds the Supabase auth cookie for a signed-in session.
- `core-flow.spec.ts`: the core path end to end against a stateful stand-in: the vendor creates a stamp card at `/setup/new`, a customer in a separate browser context opens `/c?v=<vendor>`, enters a phone number and picks a birthday with the shared Select, then the vendor looks the number up at `/dashboard/counter` and adds a stamp; asserts the stored program, card, birthday and the on-screen `0 / 5` to `1 / 5` stamp count. It runs in its own `core-flow` Playwright project after `signed-in`, because it binds the same stand-in port. Camera scanning is not driven; the counter's phone lookup stands in for it.
- `mock-supabase.ts`: not a spec; a small `http` server standing in for GoTrue and PostgREST, honouring `offset`/`limit` and the app's keyset cursors so paged reads end, plus `signedInCookie()`, which builds the Supabase auth cookie for a signed-in session.

## Parent

Expand Down
279 changes: 279 additions & 0 deletions e2e/core-flow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
import type { Server } from "node:http";
import { test, expect } from "@playwright/test";
import { signedInCookie, startSupabaseMock } from "./mock-supabase";

const SUPABASE_URL =
process.env.NEXT_PUBLIC_SUPABASE_URL ?? "http://127.0.0.1:54321";
const USER_ID = "6df824a1-9da2-4608-ad13-2400a9114ec0";
const PROGRAM_ID = "0b1c2d3e-4f50-4a6b-8c7d-9e0f1a2b3c4d";
const CARD_ID = "1c2d3e4f-5061-4b7c-9d8e-0f1a2b3c4d5e";
const CARD_TOKEN = "0".repeat(31) + "1";
// Singapore mobile: normalizePhone stores it as +65 followed by the 8 digits.
const PHONE_INPUT = "9123 4567";
const PHONE_E164 = "+6591234567";
const STAMPS_REQUIRED = 5;
const PROGRAM_NAME = "Coffee card";
const REWARD_TEXT = "Free coffee";

type ProgramRow = {
id: string;
name: string;
stamps_required: number;
reward_text: string;
type: string;
config: unknown;
active: boolean;
expiry_days: number | null;
reward_expiry_days: number | null;
reward_cost_cents: number | null;
head_start: boolean;
head_start_percent: number;
replaced_by: string | null;
carry_over_stamps: boolean;
birthday_bonus_enabled: boolean;
};

type CardRow = {
id: string;
phone: string;
program_id: string;
stamp_count: number;
reward_count: number;
state: unknown;
cycle_started_at: string;
updated_at: string;
};

// The vendor creates a stamp card, a customer joins from the shop link and the
// vendor stamps that card, against a stateful stand-in for Supabase: the
// stand-in's RPCs keep programs and cards in memory so each step reads what the
// previous one wrote.
test.describe("core vendor and customer flow", () => {
// Each step compiles routes on first hit under `next dev`.
test.describe.configure({ mode: "serial", timeout: 120_000 });

let server: Server;
let listening = false;
let programs: ProgramRow[] = [];
let cards: CardRow[] = [];
let birthday: { month: unknown; day: unknown } | null = null;

const programSummary = (program: ProgramRow, card: CardRow) => ({
program_id: program.id,
name: program.name,
type: program.type,
config: program.config,
state: card.state,
stamp_count: card.stamp_count,
card_token: CARD_TOKEN,
reward_text: program.reward_text,
stamps_required: program.stamps_required,
expiry_days: program.expiry_days,
cycle_started_at: card.cycle_started_at,
active: program.active,
replaced_by_name: null,
replaced_by_stamp_count: null,
vendor_avatar_url: null,
active_vouchers: [],
});

test.beforeAll(async () => {
programs = [];
cards = [];
birthday = null;
const url = new URL(SUPABASE_URL);
server = startSupabaseMock({
tables: {
programs: () => programs,
cards: () => cards,
legal_check_state: () => [
{
email: "e2e-vendor@merqo.test",
checked_at: new Date().toISOString(),
is_current: true,
},
],
},
rpc: {
check_rate_limit: () => true,
create_program: (args: Record<string, unknown>) => {
programs.push({
id: PROGRAM_ID,
name: String(args.p_name),
stamps_required: Number(args.p_stamps_required),
reward_text: String(args.p_reward_text),
type: String(args.p_type),
config: args.p_config,
active: true,
expiry_days: null,
reward_expiry_days: null,
reward_cost_cents: null,
head_start: false,
head_start_percent: 0,
replaced_by: null,
carry_over_stamps: false,
birthday_bonus_enabled: false,
});
return PROGRAM_ID;
},
vendor_active_programs: () =>
programs.map((p) => ({
id: p.id,
name: p.name,
type: p.type,
reward_text: p.reward_text,
})),
customer_join: (args: Record<string, unknown>) => {
const program = programs[0];
if (!program) return { cards: [] };
let card = cards.find((c) => c.phone === args.p_phone);
if (!card) {
card = {
id: CARD_ID,
phone: String(args.p_phone),
program_id: program.id,
stamp_count: 0,
reward_count: 0,
state: {},
cycle_started_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
cards.push(card);
}
return { cards: [programSummary(program, card)] };
},
customer_set_birthday: (args: Record<string, unknown>) => {
birthday = { month: args.p_month, day: args.p_day };
return null;
},
add_stamp: (args: Record<string, unknown>) => {
let card = cards.find((c) => c.phone === args.p_phone);
if (!card) {
card = {
id: CARD_ID,
phone: String(args.p_phone),
program_id: String(args.p_program),
stamp_count: 0,
reward_count: 0,
state: {},
cycle_started_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
cards.push(card);
}
card.stamp_count += 1;
card.updated_at = new Date().toISOString();
return {
id: card.id,
phone: card.phone,
stamp_count: card.stamp_count,
};
},
},
});
listening = await new Promise<boolean>((resolve) => {
server.once("error", () => resolve(false));
server.listen(Number(url.port), url.hostname, () => resolve(true));
});
});

test.afterAll(async () => {
if (listening)
await new Promise<void>((done) => server.close(() => done()));
});

test.beforeEach(() => {
// Intentional, conditional skip: a real stack already owns the port.
test.skip(!listening, "the Supabase stand-in could not bind its port");
});

test("vendor creates a stamp card program", async ({
context,
page,
baseURL,
}) => {
const { name, value } = signedInCookie(SUPABASE_URL);
await context.addCookies([
{ name, value, url: baseURL ?? "http://localhost:3000" },
]);

await page.goto("/setup/new");
await page.getByRole("button", { name: "Next: Basics" }).click();
await page.locator("#name").fill(PROGRAM_NAME);
await page.locator("#reward_text").fill(REWARD_TEXT);
await page.locator("#stamps_required").fill(String(STAMPS_REQUIRED));
await page.getByRole("button", { name: "Next: Rules" }).click();
await page.getByRole("button", { name: "Create card" }).click();

await expect(page).toHaveURL(new RegExp(`/dashboard\\?p=${PROGRAM_ID}`), {
timeout: 60_000,
});
expect(programs).toHaveLength(1);
expect(programs[0]).toMatchObject({
name: PROGRAM_NAME,
type: "stamp",
stamps_required: STAMPS_REQUIRED,
reward_text: REWARD_TEXT,
});
});

test("customer joins from the shop link and saves a birthday", async ({
browser,
baseURL,
}) => {
// A separate context: the customer has no vendor session.
const customer = await browser.newContext({ baseURL });
const page = await customer.newPage();

await page.goto(`/c?v=${USER_ID}`);
await expect(page.getByText(`Join: ${PROGRAM_NAME}`)).toBeVisible();

await page.locator("#phone").fill(PHONE_INPUT);
await page.getByRole("button", { name: "Check my card" }).click();

await expect(page.getByText(REWARD_TEXT).first()).toBeVisible();
expect(cards).toHaveLength(1);
expect(cards[0]).toMatchObject({ phone: PHONE_E164, stamp_count: 0 });

// The shared Select is Radix: open the trigger, then pick the option.
await page.getByRole("combobox", { name: "Birth month" }).click();
await page.getByRole("option", { name: "March" }).click();
await page.getByRole("combobox", { name: "Birth day" }).click();
await page.getByRole("option", { name: "14", exact: true }).click();
await page.getByRole("button", { name: "Save", exact: true }).click();

await expect(page.getByText("Birthday saved.")).toBeVisible();
expect(birthday).toEqual({ month: 3, day: 14 });

await customer.close();
});

test("vendor stamps the enrolled card and the count updates", async ({
context,
page,
baseURL,
}) => {
const { name, value } = signedInCookie(SUPABASE_URL);
await context.addCookies([
{ name, value, url: baseURL ?? "http://localhost:3000" },
]);

await page.goto(`/dashboard/counter?p=${PROGRAM_ID}`, {
waitUntil: "domcontentloaded",
});
await expect(
page.getByRole("button", { name: /Serve a customer/ }),
).toBeVisible();

await page
.getByText(/Existing customer who can.t scan\? Enter their number/)
.click();
await page.locator("#phone").fill(PHONE_INPUT);
await page.getByRole("button", { name: "Look up" }).click();
await expect(page.getByText(`0 / ${STAMPS_REQUIRED} stamps`)).toBeVisible();

await page.getByRole("button", { name: "Add stamp" }).click();
await expect(page.getByText(`1 / ${STAMPS_REQUIRED} stamps`)).toBeVisible();
expect(cards[0]?.stamp_count).toBe(1);
});
});
Loading
Loading