import { test, expect, type Page, type Locator } from '@playwright/test' import { captureGuide, captureHero, beat, typeInto, settle, VIEWPORT, type GuideScript } from './guide' import { seededTrip, gpxFixture, ensureTrack } from './fixtures' import { openTrip, modal, dialog, confirmDialog, portalDialog } from './trip-shared' import { tripPlacesContext, tripPlacesGuides } from '../../src/help/contexts/tripPlaces' import type { HelpGuide } from '../../src/help/types' /** * Actions for the places column of a trip, keyed by the ids in * `src/help/contexts/tripPlaces.ts`. They run on the seeded "Autumn in Japan" * with one walk imported up front, so the Tracks filter exists; what a guide * creates it takes away again in `cleanup`, so the next one starts the same. */ const guide = (id: string): HelpGuide => { const g = tripPlacesGuides.find(x => x.id === id) if (!g) throw new Error(`no registered guide "${id}"`) return g } const SPARE = { name: 'Tsukiji Outer Market', lat: 35.6654, lng: 139.7707, address: '4 Chome Tsukiji, Chuo City, Tokyo' } /** What create-place looks up. See the note in its second step for why this one. */ const SEARCHED = { query: 'Osaka Castle', match: /^Osaka Castle/ } /** A row of the places column by the place's name (the rows are options of a list). */ const row = (page: Page, name: string) => page.getByRole('option', { name: new RegExp(`^${name}`) }).first() /** The context menu is a fixed popover of plain buttons; the item texts tell it apart. */ const menu = (page: Page) => page.locator('.trek-popover-enter').filter({ has: page.getByRole('button', { name: 'Delete' }) }).last() /** The add button: "Add Place/Activity" with no day open, "New place" with one. */ const addButton = (page: Page) => page.getByRole('button', { name: /^(Add Place\/Activity|New place)$/ }) /** The filter row's controls. */ const filterSelect = (page: Page) => page.getByTestId('places-filter').getByRole('button').first() const searchBox = (page: Page) => page.getByPlaceholder('Search places...').last() /** FileImportModal: its own portal, recognised by the hidden file input and its heading. */ const importFileDialog = (page: Page) => portalDialog(page, page.locator('input[type="file"][accept*=".gpx"]')) /** ListImportModal: its own portal, recognised by the link box. */ const listImportDialog = (page: Page) => portalDialog(page, page.getByPlaceholder(/goo\.gl|naver/)) /** The drop box of the file dialog: the dashed button that opens the file picker. */ const dropBox = (page: Page) => importFileDialog(page).getByRole('button').first() /** The column itself: the header block around the search box. */ const column = (page: Page) => searchBox(page).locator('xpath=ancestor::div[contains(@class,"scroll") or @data-places-sidebar][1]') async function createSpare(page: Page): Promise { const { tripId } = seededTrip() const created = await page.request.post(`/api/trips/${tripId}/places`, { data: SPARE }) if (!created.ok()) throw new Error(`could not create the spare place: ${created.status()} ${await created.text()}`) } async function deleteByName(page: Page, ...names: string[]): Promise { const { tripId } = seededTrip() const res = await page.request.get(`/api/trips/${tripId}/places`) const body = (await res.json()) as { places?: { id: number; name: string }[] } | { id: number; name: string }[] const places = Array.isArray(body) ? body : (body.places ?? []) for (const place of places.filter(x => names.includes(x.name))) { await page.request.delete(`/api/trips/${tripId}/places/${place.id}`) } } const closeModal = async (page: Page): Promise => { if (await modal(page).isVisible().catch(() => false)) { await page.keyboard.press('Escape') await expect(modal(page)).toHaveCount(0) } } const only = (target: (p: Page) => Locator) => ({ target }) const SCRIPTS: Record = { 'create-place': { guide: guide('create-place'), start: p => openTrip(p), steps: [ { // A running trip always has a day open, so the button reads New place. target: addButton, act: async p => { await addButton(p).click() await expect(modal(p).getByPlaceholder('Search places...')).toBeVisible() await settle(p) }, }, { prepare: async p => { const box = modal(p).getByPlaceholder('Search places...') // Osaka Castle on purpose: its entry in the index carries a picture, // opening hours and a description, so Place details shows what the // text says it shows. Most results have none of that and the column // would be an empty state in the picture. await typeInto(p, box, SEARCHED.query) const suggestion = modal(p).locator('.shadow-dropdown button').filter({ hasText: SEARCHED.match }).first() await expect(suggestion).toBeVisible({ timeout: 20_000 }) await suggestion.click() await expect(modal(p).getByPlaceholder('e.g. Eiffel Tower')).toHaveValue(SEARCHED.match, { timeout: 20_000 }) // The details column answers after the form; wait for it or the // picture catches the spinner. await expect(modal(p).getByText('Pick a picture', { exact: false }).first()).toBeVisible({ timeout: 30_000 }) await settle(p) }, target: p => modal(p).getByPlaceholder('Search places...').locator('xpath=../..'), }, // The details column is an