|
| 1 | +/* Choosing where a loose bin lands, from the page the loose bin is on. |
| 2 | + * |
| 3 | + * scratchLanding() refuses a bin with "no free U×V space on layer N — clear some cells, |
| 4 | + * or add a layer". Both remedies it names lived inside #s-layout, which focus hides |
| 5 | + * wholesale, so the only way to take either was to discard the bin, fix the drawer, and |
| 6 | + * start again. These cases pin the two controls that answer it, and the one thing about |
| 7 | + * them that is easy to get wrong. |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | +const { test, expect } = require('@playwright/test'); |
| 11 | +const { openBins, dragCells, bins, setField } = require('./helpers'); |
| 12 | + |
| 13 | +const start = async (page) => { |
| 14 | + await page.locator('#scratchBinMap').click(); |
| 15 | + await page.waitForTimeout(250); |
| 16 | +}; |
| 17 | +const why = (page) => page.evaluate(() => document.getElementById('scratchWhy').textContent); |
| 18 | +const layerCount = (page) => page.evaluate(() => layers.length); |
| 19 | +const curLayer = (page) => page.evaluate(() => cur); |
| 20 | +const undoDepths = (page) => page.evaluate(() => ({ |
| 21 | + drawer: undoStack.length, loose: sUndoStack.length })); |
| 22 | + |
| 23 | +test('the loose bin can see and choose the layer it will land on', async ({ page }) => { |
| 24 | + const errors = await openBins(page); |
| 25 | + await page.locator('#addLayer').click(); // two layers in the drawer |
| 26 | + await start(page); |
| 27 | + |
| 28 | + await expect(page.locator('#scratchTarget')).toBeVisible(); |
| 29 | + const opts = await page.locator('#scratchLayer option').allTextContents(); |
| 30 | + expect(opts, 'one option per layer, named the way the tabs name them').toHaveLength(2); |
| 31 | + expect(opts[0]).toContain('Layer 1'); |
| 32 | + expect(opts[1]).toContain('Layer 2'); |
| 33 | + expect(errors).toEqual([]); |
| 34 | +}); |
| 35 | + |
| 36 | +test('choosing another layer re-answers the landing question straight away', |
| 37 | + async ({ page }) => { |
| 38 | + await openBins(page); |
| 39 | + /* Added from the drawer, because #addLayer is hidden inside the mode -- which is the |
| 40 | + whole reason this feature exists. The picker is the way in from here. */ |
| 41 | + await page.locator('#addLayer').click(); |
| 42 | + await start(page); |
| 43 | + await setField(page, 'u', 2); |
| 44 | + await setField(page, 'v', 2); |
| 45 | + |
| 46 | + await page.locator('#scratchLayer').selectOption('0'); |
| 47 | + await page.waitForTimeout(150); |
| 48 | + expect(await curLayer(page)).toBe(0); |
| 49 | + expect(await why(page), 'the reason follows the layer, not the one it was written for') |
| 50 | + .toContain('layer 1'); |
| 51 | + |
| 52 | + await page.locator('#scratchLayer').selectOption('1'); |
| 53 | + await page.waitForTimeout(150); |
| 54 | + expect(await curLayer(page)).toBe(1); |
| 55 | + expect(await why(page)).toContain('layer 2'); |
| 56 | + }); |
| 57 | + |
| 58 | +/* The refusal in full: fill layer 1, then resolve it without leaving the page. */ |
| 59 | +test('adding a layer from here clears a refusal that had no answer on this page', |
| 60 | + async ({ page }) => { |
| 61 | + await openBins(page); |
| 62 | + const g = await page.evaluate(() => { const q = grid(); return { nx: q.nx, ny: q.ny }; }); |
| 63 | + /* Layer 1 covered edge to edge by ONE bin. Full coverage is what makes adding a layer |
| 64 | + the remedy: an upper layer needs level, continuous support (seat().flat and |
| 65 | + .solidBelow), so a half-empty layer 1 refuses layer 2 just as firmly -- adding a |
| 66 | + layer is not a universal answer, and this is the case where it genuinely is one. */ |
| 67 | + await dragCells(page, [0, 0], [g.nx - 1, g.ny - 1]); |
| 68 | + expect((await bins(page)).length).toBe(1); |
| 69 | + |
| 70 | + await start(page); |
| 71 | + await setField(page, 'u', 2); |
| 72 | + await setField(page, 'v', 2); |
| 73 | + await expect(page.locator('#scratchAdd'), |
| 74 | + 'layer 1 is full, so there is nowhere to land').toBeDisabled(); |
| 75 | + expect(await why(page)).toContain('add a layer'); |
| 76 | + |
| 77 | + await page.locator('#scratchAddLayer').click(); |
| 78 | + await page.waitForTimeout(250); |
| 79 | + expect(await layerCount(page)).toBe(2); |
| 80 | + expect(await curLayer(page), 'the new layer becomes the target, or the button did nothing') |
| 81 | + .toBe(1); |
| 82 | + await expect(page.locator('#scratchAdd'), |
| 83 | + 'the full layer below supports the new one, so the bin can land').toBeEnabled(); |
| 84 | + expect(await why(page)).toContain('layer 2'); |
| 85 | + }); |
| 86 | + |
| 87 | +/* The one that is easy to get wrong. snapshot() in scratch mode captures ONLY the loose |
| 88 | + * bin, and pushUndo files it on the loose bin's stack. Adding a layer edits the DRAWER, |
| 89 | + * so routing it through pushUndo would bank an entry on the wrong stack: undoing it |
| 90 | + * restores the bin, leaves the new layer standing, and spends an Undo that appears to do |
| 91 | + * nothing at all. */ |
| 92 | +test('adding a layer banks its undo on the drawer, not on the loose bin', |
| 93 | + async ({ page }) => { |
| 94 | + await openBins(page); |
| 95 | + await start(page); |
| 96 | + const before = await undoDepths(page); |
| 97 | + |
| 98 | + await page.locator('#scratchAddLayer').click(); |
| 99 | + await page.waitForTimeout(200); |
| 100 | + expect(await layerCount(page)).toBe(2); |
| 101 | + |
| 102 | + const after = await undoDepths(page); |
| 103 | + expect(after.loose, 'the loose bin did not change, so its history must not have grown') |
| 104 | + .toBe(before.loose); |
| 105 | + expect(after.drawer, 'the drawer gained a layer, so the drawer is where it is undoable') |
| 106 | + .toBe(before.drawer + 1); |
| 107 | + |
| 108 | + /* And it really undoes, once you are back in the drawer looking at it. */ |
| 109 | + await page.locator('#scratchDrop').click(); |
| 110 | + await page.waitForTimeout(200); |
| 111 | + expect(await layerCount(page)).toBe(2); |
| 112 | + await page.locator('#undoBtn').click(); |
| 113 | + await page.waitForTimeout(200); |
| 114 | + expect(await layerCount(page), 'the drawer stack holds the layer, so Undo takes it back') |
| 115 | + .toBe(1); |
| 116 | + }); |
0 commit comments