New specs: edit-mode (ES1 save round-trip + ES2/ES3 prefill 404/500 states), delete-flow (DEL1-3 happy/cancel/failed), anon-view (AN1 no owner controls, AN2 draft hidden from anon), photo-editor (live add/delete/reorder). Existing: P3-P8 now attach a photo to satisfy the create photo-gate; V3 picker cap 4->6. Full post suite 38/38, stable across parallel (3-worker) runs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mpdu3Dt1iVoozHwAMyjrbn
170 lines
6.6 KiB
JavaScript
170 lines
6.6 KiB
JavaScript
// @ts-check
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const { execSync } = require('child_process');
|
|
|
|
/**
|
|
* Resolve the Grav user directory.
|
|
*
|
|
* Resolution order:
|
|
* 1. GRAV_USER_DIR env var (set in .env or shell)
|
|
* 2. Sibling `user/` directory — authoritative for this repo's layout, where
|
|
* docker-compose always bind-mounts `./user` relative to the checkout. This
|
|
* is correct for BOTH the main checkout and a git worktree (each worktree
|
|
* serves its own `./user`), so it must be preferred over docker inspect.
|
|
* 3. `docker inspect intotheeast_grav` — last-resort fallback for running the
|
|
* specs detached from the served checkout. NOTE: from a worktree this points
|
|
* at the MAIN checkout's container (a different `user/`), so it must never
|
|
* win over the sibling dir above, or disk assertions look in the wrong tree.
|
|
*/
|
|
function resolveUserDir() {
|
|
if (process.env.GRAV_USER_DIR) {
|
|
return process.env.GRAV_USER_DIR;
|
|
}
|
|
const sibling = path.join(__dirname, '../../user');
|
|
if (fs.existsSync(path.join(sibling, 'config/site.yaml'))) {
|
|
return sibling;
|
|
}
|
|
try {
|
|
const raw = execSync(
|
|
"docker inspect intotheeast_grav --format '{{range .Mounts}}{{if eq .Destination \"/var/www/html/user\"}}{{.Source}}{{end}}{{end}}'",
|
|
{ encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }
|
|
).trim();
|
|
if (raw) return raw;
|
|
} catch (_) {
|
|
// docker not available or container not running
|
|
}
|
|
return sibling;
|
|
}
|
|
|
|
/**
|
|
* Resolve the active trip slug from site.yaml `active_trip`.
|
|
*
|
|
* The post form no longer hardcodes `pageconfig.parent` — the write target is
|
|
* injected server-side from `site.active_trip` (see the cache-on-save plugin).
|
|
* `active_trip` is a full route ("/trips/italy-2026-demo") or a bare slug; both
|
|
* reduce to the trip slug here.
|
|
*/
|
|
function resolveActiveTripSlug(userDir) {
|
|
const sitePath = path.join(userDir, 'config/site.yaml');
|
|
if (!fs.existsSync(sitePath)) return null;
|
|
const content = fs.readFileSync(sitePath, 'utf-8');
|
|
const m = content.match(/^active_trip:\s*['"]?(\S+?)['"]?\s*$/m);
|
|
if (!m) return null;
|
|
return m[1]
|
|
.replace(/^\/?trips\//, '') // strip a leading /trips/
|
|
.replace(/^\//, '')
|
|
.replace(/\/.*$/, ''); // keep only the slug segment
|
|
}
|
|
|
|
/**
|
|
* Resolve the active dailies directory on disk from the active trip slug.
|
|
*/
|
|
function resolveDailiesDir(userDir) {
|
|
const tripSlug = resolveActiveTripSlug(userDir);
|
|
if (!tripSlug) return null;
|
|
|
|
const tripsBase = path.join(userDir, 'pages/01.trips');
|
|
if (!fs.existsSync(tripsBase)) return null;
|
|
|
|
const tripFolder = fs.readdirSync(tripsBase).find(f => f === tripSlug || f.endsWith('.' + tripSlug) || f.includes(tripSlug));
|
|
if (!tripFolder) return null;
|
|
|
|
const dailiesBase = path.join(tripsBase, tripFolder);
|
|
const dailiesFolder = fs.readdirSync(dailiesBase).find(f => f === 'dailies' || f === '01.dailies' || f.endsWith('.dailies'));
|
|
if (!dailiesFolder) return null;
|
|
|
|
return path.join(dailiesBase, dailiesFolder);
|
|
}
|
|
|
|
const USER_DIR = resolveUserDir();
|
|
const TRACKER_DIR = resolveDailiesDir(USER_DIR) || path.join(USER_DIR, 'pages/01.trips/italy-2026-demo/01.dailies');
|
|
|
|
/**
|
|
* The Grav route to the active trip page, derived from site.yaml `active_trip`.
|
|
* Posted entries surface in this page's journal feed.
|
|
* Falls back to '/trips/italy-2026-demo'.
|
|
*/
|
|
function resolveActiveTripUrl() {
|
|
const slug = resolveActiveTripSlug(USER_DIR);
|
|
return slug ? '/trips/' + slug : '/trips/italy-2026-demo';
|
|
}
|
|
|
|
const ACTIVE_TRIP_URL = resolveActiveTripUrl();
|
|
|
|
/**
|
|
* Type content into the EasyMDE editor. The underlying <textarea> is hidden by
|
|
* EasyMDE, so we set the value through the instance the bundle exposes on
|
|
* window.postFormEditor (which also syncs the textarea for submission).
|
|
*/
|
|
async function fillEditor(page, text) {
|
|
await page.waitForFunction(() => window.postFormEditor != null, { timeout: 10_000 });
|
|
await page.evaluate((t) => window.postFormEditor.value(t), text);
|
|
}
|
|
|
|
/**
|
|
* Wait for photos to finish uploading. post-form.js converts HEIC->JPEG and
|
|
* hands files to FilePond via pond.addFile(); FilePond then uploads each, and a
|
|
* finished item reaches data-filepond-item-state="processing-complete".
|
|
*/
|
|
async function waitForPhotoUpload(page, count = 1) {
|
|
await page.waitForFunction(
|
|
(n) => {
|
|
const items = document.querySelectorAll('.filepond--item[data-filepond-item-state]');
|
|
return [...items].filter(el => el.getAttribute('data-filepond-item-state') === 'processing-complete').length >= n;
|
|
},
|
|
count,
|
|
{ timeout: 40_000 }
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Submit the post form with minimal required fields and return the unique title marker.
|
|
* Caller is responsible for cleanup via cleanupEntry().
|
|
*/
|
|
async function postEntry(page, { titleTag, content = 'Automated test. Safe to delete.', city = '', country = '' } = {}) {
|
|
const title = `UI Test ${titleTag} ${Date.now()}`;
|
|
await page.goto('/post');
|
|
await page.fill('input[name="data[title]"]', title);
|
|
await fillEditor(page, content);
|
|
if (city) await page.fill('input[name="data[location_city]"]', city);
|
|
if (country) await page.fill('input[name="data[location_country]"]', country);
|
|
await page.locator('.btn-post').evaluate(el => el.click());
|
|
await page.waitForSelector('.form-messages, .notices', { timeout: 15_000 });
|
|
return titleTag;
|
|
}
|
|
|
|
/**
|
|
* Find a tracker entry folder by a unique slug fragment, then delete it.
|
|
*/
|
|
function cleanupEntry(slugFragment) {
|
|
if (!slugFragment) return;
|
|
if (!fs.existsSync(TRACKER_DIR)) return;
|
|
const entries = fs.readdirSync(TRACKER_DIR);
|
|
const match = entries.find(e => e.includes(slugFragment));
|
|
if (match) {
|
|
fs.rmSync(path.join(TRACKER_DIR, match), { recursive: true });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Find the first entry folder matching a slug fragment and return its full path.
|
|
*/
|
|
function findEntry(slugFragment) {
|
|
if (!fs.existsSync(TRACKER_DIR)) return null;
|
|
const entries = fs.readdirSync(TRACKER_DIR);
|
|
const match = entries.find(e => e.includes(slugFragment));
|
|
return match ? path.join(TRACKER_DIR, match) : null;
|
|
}
|
|
|
|
/**
|
|
* Read the entry .md file (entry.md or entry.en.md) from an entry folder.
|
|
*/
|
|
function readEntryMd(entryDir) {
|
|
const name = ['entry.md', 'entry.en.md'].find(f => fs.existsSync(path.join(entryDir, f)));
|
|
if (!name) return null;
|
|
return fs.readFileSync(path.join(entryDir, name), 'utf-8');
|
|
}
|
|
|
|
module.exports = { fillEditor, waitForPhotoUpload, postEntry, cleanupEntry, findEntry, readEntryMd, TRACKER_DIR, ACTIVE_TRIP_URL };
|