diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1fcf56..43a6227 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,3 +66,36 @@ jobs: name: dist-${{ github.sha }} path: dist/ retention-days: 14 + + # Browser tests against the mock-data app (`vite --mode mock`): no Power + # Platform access and no secrets, so it runs as its own parallel job. + e2e: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup Node + uses: actions/setup-node@v6 + with: + node-version: 22 + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Install Playwright browser + run: npx playwright install --with-deps chromium + + - name: End-to-end tests + run: npm run test:e2e + + - name: Upload Playwright report + if: ${{ !cancelled() }} + uses: actions/upload-artifact@v7 + with: + name: playwright-report-${{ github.sha }} + path: playwright-report/ + retention-days: 14 diff --git a/.gitignore b/.gitignore index fe494e2..faf4620 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,11 @@ dist-ssr /[0-9][0-9]-*.png /mock-*.png +# Playwright Test output (npm run test:e2e) +/test-results/ +/playwright-report/ +/blob-report/ + # Sensitive / environment-specific material — never committed (see # specs/003-github-actions-cicd/contracts/secrets-and-environments.contract.md) power.config.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..4b16d6d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "vitest.explorer", + "ms-playwright.playwright" + ] +} diff --git a/README.md b/README.md index 56bbf5b..846c787 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ All four workflows live in [`.github/workflows/`](.github/workflows/). Credentia ``` Pull Request └── ci.yml ──► install → lint → type-check → build → test → secret scan → workflow lint + └─► e2e (parallel job): Playwright against the mock-data app All must pass. Target: < 10 minutes with npm caching. Merge to main @@ -171,6 +172,14 @@ Tests: npm run test ``` +End-to-end tests (Playwright, against the mock-data app — no Power Platform needed): + +```bash +npx playwright install chromium # once per machine +npm run test:e2e # headless +npm run test:e2e:ui # interactive runner +``` + CI build (matches the pipeline exactly — no deploy side-effects): ```bash diff --git a/e2e/analytics.spec.ts b/e2e/analytics.spec.ts new file mode 100644 index 0000000..2e2110d --- /dev/null +++ b/e2e/analytics.spec.ts @@ -0,0 +1,44 @@ +import fs from 'node:fs/promises' +import type { Page } from '@playwright/test' +import { test, expect } from './fixtures' + +async function exportCsv(page: Page): Promise { + const downloadPromise = page.waitForEvent('download') + await page.getByRole('button', { name: 'Export CSV' }).click() + const download = await downloadPromise + expect(download.suggestedFilename()).toMatch(/\.csv$/) + // saveAs(), not path(): the VS Code extension drives a remotely connected + // browser, where the download's own path is not readable. + const savedPath = test.info().outputPath(download.suggestedFilename()) + await download.saveAs(savedPath) + return fs.readFile(savedPath, 'utf-8') +} + +test.beforeEach(async ({ page }) => { + await page.goto('/analytics') + await expect(page.getByRole('heading', { name: 'Medication Breakdown' })).toBeVisible() +}) + +test('the breakdown lists each medication with its adherence', async ({ page }) => { + const breakdown = page.getByRole('region', { name: 'Per-medication breakdown' }) + + await expect(breakdown.getByRole('row', { name: /^Metformin \d+ \d+ \d+%$/ })).toBeVisible() + await expect(breakdown.getByRole('row', { name: /^Insulin Glargine \d+ \d+ \d+%$/ })).toBeVisible() +}) + +test('Export CSV downloads the intake logs', async ({ page }) => { + const csv = await exportCsv(page) + + expect(csv).toContain('Metformin') + expect(csv).toContain('Insulin Glargine') +}) + +test('the medication filter narrows the export', async ({ page }) => { + await page.getByRole('combobox', { name: 'Medication filter' }).click() + await page.getByRole('option', { name: 'Metformin' }).click() + + const csv = await exportCsv(page) + + expect(csv).toContain('Metformin') + expect(csv).not.toContain('Insulin Glargine') +}) diff --git a/e2e/calendar.spec.ts b/e2e/calendar.spec.ts new file mode 100644 index 0000000..7be848d --- /dev/null +++ b/e2e/calendar.spec.ts @@ -0,0 +1,26 @@ +import { test, expect } from './fixtures' + +test.beforeEach(async ({ page }) => { + await page.goto('/calendar') + await expect(page.getByRole('grid', { name: 'March 2026' })).toBeVisible() +}) + +test('selecting a past day lists that day\'s logs', async ({ page }) => { + const dayDetail = page.getByRole('region', { name: 'Day detail' }) + await expect(dayDetail).toContainText('No logs for this day.') + + await page.getByRole('gridcell', { name: 'Tuesday, March 10th, 2026' }).click() + + await expect(dayDetail.getByRole('heading', { name: 'March 10, 2026' })).toBeVisible() + await expect(dayDetail).toContainText('Metformin') + await expect(dayDetail).toContainText('Insulin Glargine') +}) + +test('month navigation moves back and returns to today', async ({ page }) => { + await page.getByRole('button', { name: 'Previous month' }).click() + await expect(page.getByRole('grid', { name: 'February 2026' })).toBeVisible() + + await page.getByRole('button', { name: 'Today' }).click() + await expect(page.getByRole('grid', { name: 'March 2026' })).toBeVisible() + await expect(page.getByRole('gridcell', { name: /^Today, Wednesday, March 11th, 2026/ })).toBeVisible() +}) diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts new file mode 100644 index 0000000..31c3cf3 --- /dev/null +++ b/e2e/fixtures.ts @@ -0,0 +1,36 @@ +import { test as base, expect, type Locator, type Page } from '@playwright/test' + +// The seed data in src/mocks/mock-data.ts is built relative to "now", so the +// browser clock is pinned to keep the same doses due and overdue on every run. +// A Wednesday at noon: Metformin (06:00) is overdue, the evening doses are not. +const NOW = new Date('2026-03-11T12:00:00Z') + +// The mock store lives in browser memory and resets on every full page load. +// Inside a test, move between pages with the nav links, not page.goto(). +export const test = base.extend<{ pinnedClock: void }>({ + pinnedClock: [ + async ({ page }, run) => { + await page.clock.setFixedTime(NOW) + await run() + }, + { auto: true }, + ], +}) + +export { expect } + +/** The "Active" or "Inactive" group on the Medications page. */ +export function medicationSection(page: Page, name: 'Active' | 'Inactive'): Locator { + return page + .locator('section') + .filter({ has: page.getByRole('heading', { name, exact: true }) }) +} + +/** One medication's card: the innermost element holding both its schedule and its actions. */ +export function medicationCard(page: Page, name: string): Locator { + return page + .locator('div') + .filter({ has: page.getByRole('region', { name: `Schedule for ${name}`, exact: true }) }) + .filter({ has: page.getByRole('button', { name: 'Delete' }) }) + .last() +} diff --git a/e2e/log-intake.spec.ts b/e2e/log-intake.spec.ts new file mode 100644 index 0000000..d128bc6 --- /dev/null +++ b/e2e/log-intake.spec.ts @@ -0,0 +1,46 @@ +import { test, expect } from './fixtures' + +test('logging an overdue dose clears it from the dashboard', async ({ page }) => { + await page.goto('/') + + const overdue = page.getByRole('region', { name: 'Overdue medications' }) + const metformin = page.getByRole('listitem').filter({ hasText: 'Metformin' }) + await expect(overdue).toContainText('Metformin') + + await metformin.getByRole('button', { name: 'Log' }).click() + + const dialog = page.getByRole('dialog', { name: 'Log Intake' }) + await expect(dialog.getByRole('combobox', { name: 'Medication' })).toContainText('Metformin') + await expect(dialog.getByRole('button', { name: 'Taken' })).toHaveAttribute('aria-pressed', 'true') + await dialog.getByRole('button', { name: 'Log Intake' }).click() + + await expect(page.getByText('Intake logged')).toBeVisible() + await expect(metformin).toContainText('Taken') + await expect(metformin.getByRole('button', { name: 'Edit Metformin log' })).toBeVisible() + await expect(overdue).toBeHidden() +}) + +test('an injection logged on the dashboard shows up on the calendar', async ({ page }) => { + await page.goto('/') + + await page + .getByRole('listitem') + .filter({ hasText: 'Insulin Glargine' }) + .getByRole('button', { name: 'Log' }) + .click() + + // An injection cannot be saved until a site is picked on the body map. + const dialog = page.getByRole('dialog', { name: 'Log Intake' }) + const save = dialog.getByRole('button', { name: 'Log Intake' }) + await expect(save).toBeDisabled() + await dialog.getByRole('button', { name: /^Abdominal Left/ }).click() + await save.click() + await expect(page.getByText('Intake logged')).toBeVisible() + + await page.getByRole('link', { name: 'Calendar' }).click() + + const dayDetail = page.getByRole('region', { name: 'Day detail' }) + await expect(dayDetail.getByRole('heading', { name: 'March 11, 2026' })).toBeVisible() + await expect(dayDetail).toContainText('Insulin Glargine') + await expect(dayDetail).toContainText('Abdominal Left') +}) diff --git a/e2e/medications.spec.ts b/e2e/medications.spec.ts new file mode 100644 index 0000000..9578181 --- /dev/null +++ b/e2e/medications.spec.ts @@ -0,0 +1,58 @@ +import { test, expect, medicationCard, medicationSection } from './fixtures' + +test.beforeEach(async ({ page }) => { + await page.goto('/medications') + await expect(medicationSection(page, 'Active')).toContainText('Metformin') +}) + +test('adding a medication puts it in the Active list', async ({ page }) => { + await page.getByRole('button', { name: 'Add Medication' }).click() + + const form = page.getByRole('dialog', { name: 'Add Medication' }) + await form.getByLabel('Name').fill('Aspirin') + await form.getByLabel('Dosage').fill('100 mg') + await form.getByRole('combobox', { name: 'Frequency' }).click() + await page.getByRole('option', { name: 'Daily' }).click() + await form.getByRole('combobox', { name: 'Method' }).click() + await page.getByRole('option', { name: 'Pill' }).click() + await form.getByLabel('Instructions').fill('Take with food.') + await form.getByRole('button', { name: 'Save' }).click() + + await expect(form).toBeHidden() + await expect(page.getByText('Aspirin saved')).toBeVisible() + + const card = medicationCard(page, 'Aspirin') + await expect(medicationSection(page, 'Active')).toContainText('Aspirin') + await expect(card).toContainText('100 mg') + await expect(card).toContainText('Take with food.') +}) + +test('a weekly medication asks for its scheduled day', async ({ page }) => { + await page.getByRole('button', { name: 'Add Medication' }).click() + + const form = page.getByRole('dialog', { name: 'Add Medication' }) + await expect(form.getByRole('combobox', { name: 'Scheduled Day' })).toBeHidden() + + await form.getByRole('combobox', { name: 'Frequency' }).click() + await page.getByRole('option', { name: 'Weekly', exact: true }).click() + + await expect(form.getByRole('combobox', { name: 'Scheduled Day' })).toBeVisible() +}) + +test('switching a medication off moves it to Inactive', async ({ page }) => { + await medicationCard(page, 'Ibuprofen').getByRole('switch', { name: 'Active' }).click() + + await expect(medicationSection(page, 'Inactive')).toContainText('Ibuprofen') + await expect(medicationSection(page, 'Active')).not.toContainText('Ibuprofen') +}) + +test('deleting a medication removes it after confirmation', async ({ page }) => { + await medicationCard(page, 'Ibuprofen').getByRole('button', { name: 'Delete' }).click() + + const confirm = page.getByRole('dialog', { name: 'Delete medication?' }) + await expect(confirm).toContainText('Ibuprofen') + await confirm.getByRole('button', { name: 'Delete' }).click() + + await expect(page.getByText('Ibuprofen deleted')).toBeVisible() + await expect(page.getByRole('main')).not.toContainText('Ibuprofen') +}) diff --git a/e2e/navigation.spec.ts b/e2e/navigation.spec.ts new file mode 100644 index 0000000..0b6b6c9 --- /dev/null +++ b/e2e/navigation.spec.ts @@ -0,0 +1,19 @@ +import { test, expect } from './fixtures' + +test('the sidebar reaches every page', async ({ page }) => { + await page.goto('/') + await expect(page.getByRole('heading', { level: 1, name: 'Good afternoon, Alex' })).toBeVisible() + + for (const name of ['Medications', 'Calendar', 'Analytics']) { + await page.getByRole('link', { name }).click() + await expect(page.getByRole('heading', { level: 1, name })).toBeVisible() + } + + await page.getByRole('link', { name: 'Home' }).click() + await expect(page.getByRole('heading', { name: "Today's Schedule" })).toBeVisible() +}) + +test('a page opens directly from its URL', async ({ page }) => { + await page.goto('/calendar') + await expect(page.getByRole('heading', { level: 1, name: 'Calendar' })).toBeVisible() +}) diff --git a/package-lock.json b/package-lock.json index c6fbc05..27654bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,6 +43,7 @@ "@microsoft/power-apps": "^1.1.3", "@microsoft/power-apps-cli": "1.2.0", "@microsoft/power-apps-vite": "^1.0.2", + "@playwright/test": "^1.63.0", "@testing-library/jest-dom": "^6.9.1", "@testing-library/react": "^16.3.2", "@testing-library/user-event": "^14.6.1", @@ -496,6 +497,7 @@ }, "node_modules/@clack/prompts/node_modules/is-unicode-supported": { "version": "1.3.0", + "extraneous": true, "inBundle": true, "license": "MIT", "engines": { @@ -1721,6 +1723,22 @@ "dev": true, "license": "MIT" }, + "node_modules/@playwright/test": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.63.0.tgz", + "integrity": "sha512-oxMK4vllB9RK5NQ2l1pq1IfOf2AvnEuj/vYGDj0H2nMtmtZpKtCwt/l00GEO6xjGfpBNAvjovvYdCm50dRQkpQ==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.63.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/@radix-ui/number": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@radix-ui/number/-/number-1.1.1.tgz", @@ -7074,6 +7092,35 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/playwright": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.63.0.tgz", + "integrity": "sha512-+7ziBLidS4NaNCdt57SUDT+wYmmd5fmiQejUic/kb+YsYSCPyOOE9sebzMjNmQrsnNpDJqd4WHvV/8lfKfUDUg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.63.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.63.0.tgz", + "integrity": "sha512-rYCsBF/M5HjUch52bbtVONEFjv6Xu8sm8h72dNlR5bzIE1fvC/bxgspzkjSfU+MweEMmPM8KJebG6nnyxo5mCg==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/postcss": { "version": "8.5.15", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.15.tgz", diff --git a/package.json b/package.json index beed9cd..3ac1bff 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "lint": "eslint .", "preview": "vite preview", "test": "vitest run", - "test:watch": "vitest" + "test:watch": "vitest", + "test:e2e": "playwright test", + "test:e2e:ui": "playwright test --ui" }, "dependencies": { "@radix-ui/react-checkbox": "^1.3.3", @@ -50,6 +52,7 @@ "@microsoft/power-apps": "^1.1.3", "@microsoft/power-apps-cli": "1.2.0", "@microsoft/power-apps-vite": "^1.0.2", + "@playwright/test": "^1.63.0", "@testing-library/jest-dom": "^6.9.1", "@testing-library/react": "^16.3.2", "@testing-library/user-event": "^14.6.1", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..2f7932d --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,39 @@ +import fs from 'node:fs' +import path from 'node:path' +import { defineConfig, devices } from '@playwright/test' + +// Dedicated port, so a run never attaches to a live-data dev server on 3000. +const PORT = 3100 + +// vite.config.ts serves HTTPS only when the gitignored local dev certs exist, +// so the URL has to follow the same rule (HTTPS on a dev machine, HTTP in CI). +const hasLocalCerts = ['localhost-key.pem', 'localhost.pem'].every((file) => + fs.existsSync(path.resolve(import.meta.dirname, file)) +) +const baseURL = `${hasLocalCerts ? 'https' : 'http'}://localhost:${PORT}` + +export default defineConfig({ + testDir: './e2e', + fullyParallel: true, + forbidOnly: !!process.env.CI, + retries: process.env.CI ? 2 : 0, + reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : 'list', + use: { + baseURL, + ignoreHTTPSErrors: true, + // Pinned so dates and times render the same on every machine. + locale: 'en-US', + timezoneId: 'UTC', + trace: 'retain-on-failure', + }, + projects: [ + { name: 'chromium', use: { ...devices['Desktop Chrome'] } }, + ], + // Mock mode: in-memory seed data, no Power Apps runtime or Dataverse needed. + webServer: { + command: `npm run dev:mock -- --port ${PORT} --strictPort`, + url: baseURL, + ignoreHTTPSErrors: true, + reuseExistingServer: !process.env.CI, + }, +}) diff --git a/tsconfig.node.json b/tsconfig.node.json index 8a67f62..0836927 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -22,5 +22,5 @@ "noFallthroughCasesInSwitch": true, "noUncheckedSideEffectImports": true }, - "include": ["vite.config.ts"] + "include": ["vite.config.ts", "playwright.config.ts", "e2e"] } diff --git a/vite.config.ts b/vite.config.ts index 449d574..e143e66 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'vitest/config' +import { configDefaults, defineConfig } from 'vitest/config' import fs from 'fs' import path from 'path' import react from '@vitejs/plugin-react' @@ -61,6 +61,8 @@ export default defineConfig(({ mode }) => { environment: 'happy-dom', globals: true, setupFiles: ['./tests/setup.ts'], + // e2e/ holds Playwright tests (`npm run test:e2e`), not Vitest ones. + exclude: [...configDefaults.exclude, 'e2e/**'], passWithNoTests: true, clearMocks: true, },