|
| 1 | +/** |
| 2 | + * Smoke test – Full voting flow |
| 3 | + * |
| 4 | + * Exercises the entire happy-path: |
| 5 | + * 1. Votekeeper signs in (mock auth) |
| 6 | + * 2. Seeds votekeeper if first time |
| 7 | + * 3. Creates an event |
| 8 | + * 4. Adds 3 voting options |
| 9 | + * 5. Opens voting |
| 10 | + * 6. Voter joins on a mobile viewport, enters name, allocates votes |
| 11 | + * 7. Votekeeper closes voting |
| 12 | + * 8. Votekeeper reveals results one-by-one |
| 13 | + * 9. Votekeeper completes the event |
| 14 | + * 10. Public results page loads |
| 15 | + * |
| 16 | + * Prerequisites: the local dev environment must be running |
| 17 | + * (Azurite + Azure Functions + Vite dev server). |
| 18 | + */ |
| 19 | +import { test, expect, type Page, type BrowserContext } from '@playwright/test'; |
| 20 | + |
| 21 | +// ── helpers ────────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +/** Sign in via the mock auth page and land on the dashboard. */ |
| 24 | +async function votekeeperSignIn(page: Page) { |
| 25 | + await page.goto('/.auth/login/aad?post_login_redirect_uri=/dashboard'); |
| 26 | + await page.getByRole('button', { name: /sign in/i }).click(); |
| 27 | + // Dashboard loads, fires /api/me, then shows content or redirects. |
| 28 | + // Wait for either "Create Event" link or "Become First Votekeeper" button. |
| 29 | + await expect( |
| 30 | + page.getByRole('link', { name: /new event/i }).first() |
| 31 | + .or(page.getByRole('button', { name: /become first votekeeper/i })) |
| 32 | + ).toBeVisible({ timeout: 20_000 }); |
| 33 | +} |
| 34 | + |
| 35 | +/** If the "Become First Votekeeper" button is visible, click it and wait. */ |
| 36 | +async function seedIfNeeded(page: Page) { |
| 37 | + const seedBtn = page.getByRole('button', { name: /become first votekeeper/i }); |
| 38 | + if (await seedBtn.isVisible()) { |
| 39 | + await seedBtn.click(); |
| 40 | + // After seeding the page refreshes and shows the dashboard |
| 41 | + await expect(page.getByRole('link', { name: /new event/i }).first()).toBeVisible({ timeout: 15_000 }); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// ── test ───────────────────────────────────────────────────────────────── |
| 46 | + |
| 47 | +test('create event → vote → reveal → complete', async ({ browser }) => { |
| 48 | + // ── 1. Votekeeper: sign in ────────────────────────────────────────── |
| 49 | + const vkContext = await browser.newContext(); |
| 50 | + const vk = await vkContext.newPage(); |
| 51 | + |
| 52 | + await votekeeperSignIn(vk); |
| 53 | + await seedIfNeeded(vk); |
| 54 | + |
| 55 | + // ── 2. Create event ───────────────────────────────────────────────── |
| 56 | + await vk.getByRole('link', { name: /new event/i }).first().click(); |
| 57 | + await vk.waitForURL('**/create'); |
| 58 | + |
| 59 | + await vk.getByLabel(/event name/i).fill('Smoke Test Event'); |
| 60 | + await vk.getByRole('button', { name: /create event/i }).click(); |
| 61 | + |
| 62 | + // Land on the manage page – URL contains /manage/<4-letter ID> |
| 63 | + await vk.waitForURL(/\/manage\/[A-Z]{4}$/); |
| 64 | + const eventId = vk.url().match(/\/manage\/([A-Z]{4})$/)?.[1]; |
| 65 | + expect(eventId).toBeTruthy(); |
| 66 | + |
| 67 | + // ── 3. Add 3 voting options ───────────────────────────────────────── |
| 68 | + const options = ['Option Alpha', 'Option Beta', 'Option Gamma']; |
| 69 | + for (const title of options) { |
| 70 | + await vk.getByPlaceholder('Option title').fill(title); |
| 71 | + await vk.getByRole('button', { name: 'Add' }).click(); |
| 72 | + // Wait for the option to appear in the list |
| 73 | + await expect(vk.getByText(title)).toBeVisible(); |
| 74 | + } |
| 75 | + |
| 76 | + // Verify all 3 options are listed |
| 77 | + await expect(vk.getByText('Voting Options (3)')).toBeVisible(); |
| 78 | + |
| 79 | + // ── 4. Open voting ────────────────────────────────────────────────── |
| 80 | + await vk.getByRole('button', { name: /open voting/i }).click(); |
| 81 | + |
| 82 | + // Status should change |
| 83 | + await expect(vk.getByText(/open/i).first()).toBeVisible(); |
| 84 | + |
| 85 | + // ── 5. Voter: join, enter name, cast votes ────────────────────────── |
| 86 | + const voterContext: BrowserContext = await browser.newContext({ |
| 87 | + viewport: { width: 375, height: 812 }, // iPhone-like viewport |
| 88 | + }); |
| 89 | + const voter = await voterContext.newPage(); |
| 90 | + |
| 91 | + await voter.goto(`/join/${eventId}`); |
| 92 | + await expect(voter.getByText('Smoke Test Event')).toBeVisible(); |
| 93 | + |
| 94 | + // Enter voter name |
| 95 | + await voter.getByPlaceholder(/enter your name/i).fill('Test Voter'); |
| 96 | + |
| 97 | + // Voting option buttons should appear |
| 98 | + await expect(voter.getByText('Option Alpha')).toBeVisible(); |
| 99 | + |
| 100 | + // Allocate votes: 2 on Alpha, 1 on Gamma (3 total, the default) |
| 101 | + // Use heading-based locator to find each option's + button |
| 102 | + const optionPlus = (name: string) => |
| 103 | + voter.getByRole('heading', { name }).locator('..').locator('..').getByRole('button', { name: '+' }); |
| 104 | + |
| 105 | + await optionPlus('Option Alpha').click(); |
| 106 | + await optionPlus('Option Alpha').click(); |
| 107 | + await optionPlus('Option Gamma').click(); |
| 108 | + |
| 109 | + // Verify all votes allocated |
| 110 | + await expect(voter.getByText('All votes allocated!')).toBeVisible(); |
| 111 | + |
| 112 | + // Wait for auto-save (debounce 1.5s + network) |
| 113 | + await expect(voter.getByText('Saving...')).toBeVisible({ timeout: 5_000 }); |
| 114 | + await expect(voter.getByText('✓ Saved')).toBeVisible({ timeout: 10_000 }); |
| 115 | + |
| 116 | + // ── 6. Votekeeper: close voting ───────────────────────────────────── |
| 117 | + // Accept the confirm dialog |
| 118 | + vk.on('dialog', (d) => d.accept()); |
| 119 | + await vk.getByRole('button', { name: /close voting/i }).click(); |
| 120 | + |
| 121 | + // Status should become 'closed' |
| 122 | + await expect(vk.getByText(/closed/i).first()).toBeVisible({ timeout: 10_000 }); |
| 123 | + |
| 124 | + // ── 7. Reveal results one-by-one ──────────────────────────────────── |
| 125 | + // Click "Reveal Results" to start the reveal |
| 126 | + await vk.getByRole('button', { name: /reveal results/i }).click(); |
| 127 | + |
| 128 | + // Now in the reveal view – dark background |
| 129 | + await expect(vk.locator('.bg-gray-900')).toBeVisible({ timeout: 10_000 }); |
| 130 | + |
| 131 | + // Reveal each option (3 options → 2 more "Reveal Next" clicks) |
| 132 | + for (let i = 0; i < 2; i++) { |
| 133 | + const revealNext = vk.getByRole('button', { name: /reveal next/i }); |
| 134 | + await expect(revealNext).toBeVisible({ timeout: 10_000 }); |
| 135 | + await revealNext.click(); |
| 136 | + // Brief wait for animation/network |
| 137 | + await vk.waitForTimeout(500); |
| 138 | + } |
| 139 | + |
| 140 | + // All revealed – "Complete Event" should appear |
| 141 | + await expect(vk.getByRole('button', { name: /complete event/i })).toBeVisible({ timeout: 10_000 }); |
| 142 | + await vk.getByRole('button', { name: /complete event/i }).click(); |
| 143 | + |
| 144 | + // ── 8. Verify final results ───────────────────────────────────────── |
| 145 | + // Wait for "Final Results" label first, then check content |
| 146 | + await expect(vk.getByText('Final Results')).toBeVisible({ timeout: 10_000 }); |
| 147 | + await expect(vk.getByRole('heading', { name: 'Option Alpha' }).first()).toBeVisible({ timeout: 10_000 }); |
| 148 | + |
| 149 | + // PDF link should be available |
| 150 | + await expect(vk.getByText('PDF')).toBeVisible(); |
| 151 | + |
| 152 | + // ── 9. Public results page ────────────────────────────────────────── |
| 153 | + const publicPage = await vkContext.newPage(); |
| 154 | + await publicPage.goto(`/results/${eventId}`); |
| 155 | + await expect(publicPage.getByText('Smoke Test Event')).toBeVisible({ timeout: 10_000 }); |
| 156 | + await expect(publicPage.getByRole('heading', { name: 'Option Alpha' }).first()).toBeVisible(); |
| 157 | + |
| 158 | + // ── Cleanup ───────────────────────────────────────────────────────── |
| 159 | + await voterContext.close(); |
| 160 | + await vkContext.close(); |
| 161 | +}); |
0 commit comments