File size: 9,547 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
function assert(ok, message) { if (!ok) throw new Error(message); }
function entry() {
const found = fs.readdirSync(process.cwd()).find(f => f.toLowerCase().endsWith('.svg'));
assert(found, 'missing SVG output');
return path.join(process.cwd(), found);
}
async function runCase(id, fn) {
try { await fn(); console.log(`${id} PASS`); }
catch (e) { console.error(`${id} FAIL: ${e.message}`); process.exitCode = 1; }
}
(async () => {
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox', '--disable-dev-shm-usage'] });
const page = await browser.newPage({ viewport: { width: 1000, height: 720 } });
await page.goto(pathToFileURL(entry()).href, { waitUntil: 'domcontentloaded' });
// ENHANCED_CASE_1: a large aquarium scene includes multiple plants and bottom decoration.
// Paint names are deliberately not selectors: equivalent SVG palettes remain valid.
await runCase('ENHANCED_CASE_1', async () => {
const s = await page.evaluate(() => {
const svg = document.querySelector('svg');
const box = svg?.getBoundingClientRect();
const nodes = [...document.querySelectorAll('svg rect,svg path,svg polygon,svg circle,svg ellipse')];
const plants = nodes.filter(el => {
const r = el.getBoundingClientRect();
const paint = `${el.getAttribute('fill') || ''} ${getComputedStyle(el).fill}`.toLowerCase();
return r.y + r.height / 2 > (box?.y || 0) + (box?.height || 0) * 0.6 &&
(/green/.test(paint) || [...paint.matchAll(/rgba?\((\d+),\s*(\d+),\s*(\d+)/g)].some(m => +m[2] > +m[1] && +m[2] > +m[3]));
});
const bottomDecor = nodes.filter(el => {
const r = el.getBoundingClientRect();
return r.width >= 10 && r.height >= 10 && r.y + r.height / 2 > (box?.y || 0) + (box?.height || 0) * 0.7;
});
return { w: box?.width || 0, h: box?.height || 0, plants: plants.length, bottomDecor: bottomDecor.length };
});
assert(s.w >= 400 && s.h >= 240, `aquarium is not large enough (${s.w}x${s.h})`);
assert(s.plants >= 2, 'expected multiple visible plant elements near the tank bottom');
assert(s.bottomDecor >= 3, 'expected visible bottom scenery in addition to the tank');
});
// Check the actual marked fish and fins, regardless of painted shape type.
// Body/eye recognizability is visual evidence, not an SVG tag requirement.
await runCase('ENHANCED_CASE_2', async () => {
const fish = await page.evaluate(() => {
const measure = element => {
if (!element) return 0;
for (let node = element; node; node = node.parentElement) {
const style = getComputedStyle(node);
if (style.display === 'none' || style.visibility === 'hidden' || +style.opacity === 0) return 0;
}
const {width, height} = element.getBoundingClientRect();
return width * height;
};
return [0, 1].map(i => ({
body: measure(document.querySelector(`[data-fish="${i}"]`)),
fin: measure(document.querySelector(`[data-tail="${i}"]`)),
}));
});
assert(fish.every(f => f.body > 0 && f.fin > 0), 'both marked fish and fins must have visible geometry');
assert(Math.abs(fish[0].body - fish[1].body) > 20, 'the two rendered fish should have different sizes');
});
// ENHANCED_CASE_3: both fish have ongoing movement, independent of exact SVG selector structure.
await runCase('ENHANCED_CASE_3', async () => {
const result = await page.evaluate(() => { const a=window.aquariumTest.snapshot(); window.aquariumTest.step(120); const b=window.aquariumTest.snapshot(); return {a,b}; });
assert(result.a.fish[0].tail !== result.b.fish[0].tail && result.a.fish[1].tail !== result.b.fish[1].tail, 'both tails must animate');
assert((result.b.fish[0].tail-result.a.fish[0].tail) !== (result.b.fish[1].tail-result.a.fish[1].tail), 'tail fins need independent animation state');
});
await runCase('ENHANCED_CASE_4', async () => {
const result = await page.evaluate(() => { window.aquariumTest.feed(470,60); const a=window.aquariumTest.snapshot(); window.aquariumTest.step(200); const b=window.aquariumTest.snapshot(); return {a,b}; });
assert(result.a.food.length >= 3, 'feeding must create several pellets at the selected location');
assert(result.b.food[0].y > result.a.food[0].y, 'pellets must sink over time');
assert(result.b.fish.some((f,i) => Math.hypot(f.x-470,f.y-60) < Math.hypot(result.a.fish[i].x-470,result.a.fish[i].y-60)), 'fish must move closer to the food');
});
await runCase('ENHANCED_CASE_5', async () => {
const consumption = await page.evaluate(() => {
const svg = document.documentElement;
const screen = svg.getScreenCTM();
const pad = Math.max(Math.hypot(screen.a, screen.b), Math.hypot(screen.c, screen.d)) * 12;
const fish = [...document.querySelectorAll('[data-fish]')];
const frame = () => ({
food: window.aquariumTest.snapshot().food.map(({x, y}) => new DOMPoint(x, y).matrixTransform(screen)),
fish: fish.map(node => node.getBoundingClientRect()),
});
const reaches = (point, box) => point.x >= box.left - pad && point.x <= box.right + pad &&
point.y >= box.top - pad && point.y <= box.bottom + pad;
let before = frame();
for (let elapsed = 200; elapsed < 10000 && before.food.length; elapsed += 40) {
window.aquariumTest.step(40);
const after = frame();
const consumed = before.food.length - after.food.length;
const reachable = before.food.filter(pellet =>
[...before.fish, ...after.fish].some(box => reaches(pellet, box)));
if (consumed > reachable.length) {
return {remaining: after.food.length, removedAtDistance: true};
}
before = after;
}
return {remaining: before.food.length, removedAtDistance: false};
});
assert(!consumption.removedAtDistance, 'pellets must not disappear while all fish are away from them');
assert(consumption.remaining === 0, 'food must be eaten within ten seconds of feeding');
});
await runCase('ENHANCED_CASE_6', async () => {
const before = await page.evaluate(() => window.aquariumTest.snapshot().food.length);
const tank = await page.locator('svg').boundingBox();
const target = await page.locator('svg').evaluate(svg => {
const b = svg.getBoundingClientRect(), p = svg.createSVGPoint();
p.x = b.x + b.width * .75; p.y = b.y + b.height * .3;
const q = p.matrixTransform(svg.getScreenCTM().inverse());
return { x: q.x, y: q.y };
});
await page.mouse.click(tank.x + tank.width * .75, tank.y + tank.height * .3);
const after = await page.evaluate(() => window.aquariumTest.snapshot());
assert(after.food.length >= before + 3, 'clicking the tank must feed through the same state machine');
assert(after.food.some(p => Math.abs(p.x - target.x) < 20 && Math.abs(p.y - target.y) < 20), 'pellets should appear near the clicked point');
});
await runCase('VISIBLE_STATE_AND_COPY', async () => {
const visible = await page.evaluate(() => {
const elements = [...document.querySelectorAll('[data-fish], [data-tail], [data-food-layer] > *')];
const good = element => {
for (let node=element; node; node=node.parentElement) {
const style=getComputedStyle(node);
if(style.display==='none'||style.visibility==='hidden'||+style.opacity===0) return false;
}
return true;
};
const snapshot=aquariumTest.snapshot();
const origins=[...document.querySelectorAll('[data-fish]')].map(element => {
const matrix=element.getCTM();
return {x:matrix.e,y:matrix.f};
});
snapshot.fish[0].x=-99999;snapshot.food.length=0;
return {allVisible:elements.every(good),origins,state:aquariumTest.snapshot()};
});
assert(visible.allVisible && visible.origins.length === 2, 'visible fish/tail/food geometry required');
visible.origins.forEach((point,index) => assert(Math.abs(point.x-visible.state.fish[index].x)<1 && Math.abs(point.y-visible.state.fish[index].y)<1,'rendered fish position must match snapshot'));
assert(visible.state.food.length>=3 && visible.state.fish[0].x!==-99999,'snapshot must be an isolated copy');
fs.mkdirSync('/logs/verifier/visual-audit',{recursive:true});
await page.screenshot({path:'/logs/verifier/visual-audit/fed-aquarium.png'});
await page.evaluate(() => {for(let i=0;i<120;i++)aquariumTest.step(100);});
const movement=await page.evaluate(() => {const a=aquariumTest.snapshot();aquariumTest.step(300);return {a,b:aquariumTest.snapshot()};});
assert(movement.b.food.length===0,'second meal must be eaten');
for(let i=0;i<2;i++) assert(Math.abs(movement.a.fish[i].x-movement.b.fish[i].x)>0.1 && Math.abs(movement.a.fish[i].y-movement.b.fish[i].y)>0.1,'both fish must swim horizontally and vertically');
await page.screenshot({path:'/logs/verifier/visual-audit/free-swimming.png'});
await page.reload();
const first=await page.locator('[data-fish="0"]').getAttribute('transform');
await page.waitForFunction(first => document.querySelector('[data-fish="0"]').getAttribute('transform')!==first,first,{timeout:5000});
});
await browser.close();
if (process.exitCode) process.exit(process.exitCode);
})().catch(e => { console.error(e.stack || e); process.exit(1); });
|