|
| 1 | +import { test, expect } from '../fixtures/test'; |
| 2 | +import { seedUser, seedClip } from '../helpers/seed'; |
| 3 | +import * as schema from '../../src/lib/server/db/schema'; |
| 4 | +import { eq } from 'drizzle-orm'; |
| 5 | +import { v4 as uuid } from 'uuid'; |
| 6 | +import type { E2eDb } from '../fixtures/db'; |
| 7 | + |
| 8 | +/** |
| 9 | + * The reciprocity feed gate: once a user has banked the cap AND has clips waiting in their |
| 10 | + * queue, a dedicated full-screen QueueGateSlide takes over instead of the next reel. It is NOT |
| 11 | + * a bottom sheet — the only way off is the SKIP (soft nudge) or POST button. |
| 12 | + */ |
| 13 | +test.describe('reciprocity: feed gate slide', () => { |
| 14 | + function setCredits(db: E2eDb, userId: string, n: number) { |
| 15 | + (db as any) |
| 16 | + .update(schema.users) |
| 17 | + .set({ postCredits: n }) |
| 18 | + .where(eq(schema.users.id, userId)) |
| 19 | + .run(); |
| 20 | + } |
| 21 | + |
| 22 | + function seedQueued(db: E2eDb, groupId: string, userId: string) { |
| 23 | + const clip = seedClip(db, { groupId, addedBy: userId, status: 'queued' }); |
| 24 | + (db as any) |
| 25 | + .insert(schema.clipQueue) |
| 26 | + .values({ id: uuid(), clipId: clip.id, userId, groupId, createdAt: new Date() }) |
| 27 | + .run(); |
| 28 | + return clip; |
| 29 | + } |
| 30 | + |
| 31 | + // Cap is 5 by default; sit the user at the cap with clips waiting → the gate should engage. |
| 32 | + async function primeGate(db: E2eDb, loggedIn: { userId: string; groupId: string }) { |
| 33 | + setCredits(db, loggedIn.userId, 5); |
| 34 | + const other = seedUser(db, { groupId: loggedIn.groupId }); |
| 35 | + seedClip(db, { groupId: loggedIn.groupId, addedBy: other.id, status: 'ready' }); |
| 36 | + seedQueued(db, loggedIn.groupId, loggedIn.userId); |
| 37 | + seedQueued(db, loggedIn.groupId, loggedIn.userId); |
| 38 | + } |
| 39 | + |
| 40 | + test('engages at the cap and SKIP dismisses it (soft nudge)', async ({ page, loggedIn, db }) => { |
| 41 | + await primeGate(db, loggedIn); |
| 42 | + await page.goto('/'); |
| 43 | + |
| 44 | + const gate = page.locator('.gate[role="dialog"]'); |
| 45 | + await expect(gate).toBeVisible({ timeout: 8000 }); |
| 46 | + await expect(gate).toContainText('Post one to keep scrolling'); |
| 47 | + await expect(gate.locator('.queue-item')).toHaveCount(2); |
| 48 | + |
| 49 | + // It must NOT be the drag-to-dismiss bottom sheet. |
| 50 | + await expect(page.locator('.base-sheet')).toHaveCount(0); |
| 51 | + |
| 52 | + // Retry the dismiss — a tap landing during the entrance animation can be dropped under |
| 53 | + // heavy parallel CPU load. Skipping is idempotent, so a repeat tap is harmless. |
| 54 | + const skipBtn = gate.getByRole('button', { name: 'Skip for now' }); |
| 55 | + await expect(async () => { |
| 56 | + if (await gate.isVisible()) await skipBtn.click({ timeout: 2000 }).catch(() => {}); |
| 57 | + await expect(gate).toBeHidden({ timeout: 2000 }); |
| 58 | + }).toPass({ timeout: 12000 }); |
| 59 | + }); |
| 60 | + |
| 61 | + test('POST publishes a queued clip and dismisses the gate', async ({ |
| 62 | + page, |
| 63 | + request, |
| 64 | + loggedIn, |
| 65 | + db |
| 66 | + }) => { |
| 67 | + await primeGate(db, loggedIn); |
| 68 | + await page.goto('/'); |
| 69 | + |
| 70 | + const gate = page.locator('.gate[role="dialog"]'); |
| 71 | + await expect(gate).toBeVisible({ timeout: 8000 }); |
| 72 | + |
| 73 | + await gate.locator('.select-box').first().click(); |
| 74 | + const postBtn = gate.locator('.post-btn'); |
| 75 | + await expect(postBtn).toBeEnabled(); |
| 76 | + await expect(postBtn).toContainText('Post 1'); |
| 77 | + |
| 78 | + // Wait for the post round-trip so we don't race the response. |
| 79 | + await Promise.all([ |
| 80 | + page.waitForResponse( |
| 81 | + (r) => r.url().includes('/api/queue/post') && r.request().method() === 'POST' |
| 82 | + ), |
| 83 | + postBtn.click() |
| 84 | + ]); |
| 85 | + |
| 86 | + // Gate dismisses and stays gone (the credit spent is earned back by watching the next reel, |
| 87 | + // but the post grace keeps the gate from instantly re-opening). One clip left the queue. |
| 88 | + await expect(gate).toBeHidden({ timeout: 8000 }); |
| 89 | + const count = await (await request.get('/api/queue/count')).json(); |
| 90 | + expect(count.count).toBe(1); |
| 91 | + }); |
| 92 | + |
| 93 | + test('never engages when the queue is empty', async ({ page, loggedIn, db }) => { |
| 94 | + setCredits(db, loggedIn.userId, 5); // at the cap, but nothing queued |
| 95 | + const other = seedUser(db, { groupId: loggedIn.groupId }); |
| 96 | + seedClip(db, { groupId: loggedIn.groupId, addedBy: other.id, status: 'ready' }); |
| 97 | + await page.goto('/'); |
| 98 | + |
| 99 | + // Give the feed a moment to settle, then assert the gate stayed away. |
| 100 | + await page.waitForTimeout(1500); |
| 101 | + await expect(page.locator('.gate[role="dialog"]')).toHaveCount(0); |
| 102 | + }); |
| 103 | +}); |
0 commit comments