PlotDirector/PlotLine.Tests/Browser/timeline-scene-drops.cjs

96 lines
6.1 KiB
JavaScript

const { chromium } = require('playwright');
const assert = require('node:assert/strict');
(async () => {
const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROMIUM_PATH });
try {
const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });
const failures = [];
page.on('pageerror', e => failures.push(e.message));
let posts = [];
let failSave = false;
await page.route('**/Timeline/Drop*', async route => {
posts.push({ url: route.request().url(), body: route.request().postData() });
await route.fulfill({ status: failSave ? 400 : 200, contentType: 'application/json', body: JSON.stringify(failSave ? { success: false, message: 'This character is already in the scene.' } : { success: true }) });
});
const url = `${process.env.PD_BROWSER_FIXTURE_URL || 'http://127.0.0.1:5238'}/fixture?page=timeline-render`;
await page.goto(url);
await page.locator('[data-drag-palette]').evaluate(el => el.open = true);
const modal = page.locator('[data-scene-assignment-modal]');
const card = page.locator('.timeline-scene-card[data-scene-id="101"]');
const drop = async type => {
const source = page.locator(`.drag-chip[data-drag-type="${type}"]`);
const data = await source.evaluate(el => ({ type: el.dataset.dragType, id: el.dataset.dragId, label: el.dataset.dragLabel }));
await card.evaluate((el, data) => {
const transfer = new DataTransfer();
transfer.setData('application/json', JSON.stringify(data));
el.dispatchEvent(new DragEvent('dragover', { bubbles: true, cancelable: true, dataTransfer: transfer }));
if (!el.classList.contains('drop-target-active')) throw new Error('Scene rejected drag target');
el.dispatchEvent(new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer: transfer }));
}, data);
await modal.waitFor({ state: 'visible' });
await page.waitForTimeout(350);
};
assert.equal(await page.locator('.drag-chip.location .pd-icon').count(), 1);
for (const theme of ['light', 'dark']) {
await page.evaluate(theme => { document.documentElement.dataset.appTheme = `modern-${theme}`; document.documentElement.dataset.theme = theme; document.documentElement.dataset.bsTheme = theme; }, theme);
await page.evaluate(() => window.scrollTo({ top: 120, behavior: "instant" }));
const scrollBefore = await page.evaluate(() => window.scrollY);
await drop('character');
await page.screenshot({ path: `/tmp/task50-character-${theme}.png` });
assert.match(await modal.innerText(), /Traveller.*Arrival/s);
assert.equal(await modal.locator('select').count(), 3);
assert.equal(await page.locator('[data-drag-drop-panel]').isVisible(), false);
assert.equal(await modal.locator('select').first().evaluate(el => el === document.activeElement), true);
await page.keyboard.press('Escape');
await modal.waitFor({ state: 'hidden' });
await page.waitForFunction(() => document.activeElement?.dataset.sceneId === '101');
assert.equal(await page.evaluate(() => window.scrollY), scrollBefore);
}
assert.equal(posts.length, 0, 'Cancel must not post');
await drop('location');
assert.match(await modal.innerText(), /Station.*Arrival/s);
assert.match(await modal.innerText(), /North Pier/);
await modal.locator('form:not(.d-none) button[type=submit]').click();
assert.equal(posts.length, 0, 'Replacement requires explicit consent');
await modal.locator('[name=ConfirmReplacement]').check();
await modal.locator('form:not(.d-none) [data-drag-cancel]').click();
await modal.waitFor({ state: 'hidden' });
assert.equal(posts.length, 0);
await page.setViewportSize({ width: 390, height: 740 });
await drop('character');
const bounds = await modal.locator('.modal-dialog').boundingBox();
assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= 390);
await page.screenshot({ path: '/tmp/task50-character-narrow.png' });
failSave = true;
await modal.locator('form:not(.d-none) button[type=submit]').click();
await modal.locator('[data-scene-assignment-error]:not([hidden])').waitFor();
assert.match(await modal.innerText(), /already in the scene/);
assert.equal(await modal.isVisible(), true);
await modal.locator('form:not(.d-none) [data-drag-cancel]').click();
await modal.waitFor({ state: 'hidden' });
failSave = false;
await page.locator('[data-timeline-search]').evaluate(el => { el.value = 'Arrival'; el.dispatchEvent(new Event('input', { bubbles: true })); });
await drop('location');
await modal.locator('[name=ConfirmReplacement]').check();
await Promise.all([page.waitForEvent('load'), modal.locator('form:not(.d-none) button[type=submit]').click()]);
await page.waitForTimeout(400);
assert.equal(page.url(), url);
assert.equal(await page.locator('[data-timeline-search]').inputValue(), 'Arrival');
assert.equal(await card.evaluate(el => el === document.activeElement), true);
assert.match(posts.at(-1).url, /DropLocation/);
assert.match(posts.at(-1).body, /name="ExpectedLocationID"\r\n\r\n77/);
await page.locator('[data-drag-palette]').evaluate(el => el.open = true);
await drop('character');
await modal.locator('[name=RoleInSceneTypeID]').selectOption('4');
await modal.locator('[name=PresenceTypeID]').selectOption('5');
await modal.locator('select[name=LocationID]').selectOption('78');
await Promise.all([page.waitForEvent('load'), modal.locator('form:not(.d-none) button[type=submit]').click()]);
assert.match(posts.at(-1).url, /DropCharacter/);
for (const [name, value] of [['CharacterID', '3'], ['RoleInSceneTypeID', '4'], ['PresenceTypeID', '5'], ['LocationID', '78']]) {
assert.ok(posts.at(-1).body.includes(`name="${name}"\r\n\r\n${value}`), `${name} must reach the save endpoint`);
}
assert.equal(failures.length, 0, failures.join('\n'));
console.log('PASS timeline character/location modal themes, responsive layout, cancellation, confirmation, errors and state restoration (mock save transport)');
} finally { await browser.close(); }
})().catch(e => { console.error(e); process.exitCode = 1; });