File size: 5,636 Bytes
6b5ce6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Drives the hand editor in headless Chromium: click-select, drag, panel edits, undo/redo, delete.
//   node tests/check_editor.mjs [screenshot.png]
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { chromium } from 'playwright-core';

const WEB = new URL('../web/', import.meta.url).pathname;
const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json' };
const server = http.createServer((req, res) => {
  const f = path.join(WEB, decodeURIComponent(req.url.split('?')[0]).replace(/\/$/, '/index.html'));
  if (!fs.existsSync(f)) { res.writeHead(404); return res.end(); }
  res.writeHead(200, { 'content-type': MIME[path.extname(f)] ?? 'application/octet-stream' });
  fs.createReadStream(f).pipe(res);
}).listen(0);
const root = `${process.env.HOME}/.cache/ms-playwright`;
const exe = process.env.CHROME ?? fs.readdirSync(root).filter((d) => d.startsWith('chromium_headless_shell'))
  .flatMap((d) => fs.readdirSync(`${root}/${d}`).map((s) => `${root}/${d}/${s}/chrome-headless-shell`)).find((p) => fs.existsSync(p));
const browser = await chromium.launch({ executablePath: exe, args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader'] });
const page = await browser.newPage({ viewport: { width: 1280, height: 760 } });
const errors = [];
page.on('pageerror', (e) => errors.push(String(e)));
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });

let fail = 0;
const check = (ok, msg) => { console.log((ok ? 'ok   ' : 'FAIL ') + msg); if (!ok) fail++; };
const sc = () => page.evaluate(() => window.tinyScene.scene);
const screenOf = (id) => page.evaluate((id) => { // screen position of an entity's centre
  const s = window.tinyScene.stage, i = s.built.insts.find((x) => x.ent.id === id);
  const THREE_Box = s.selBox.constructor, b = new THREE_Box().setFromObject(i.obj), p = b.getCenter(b.min.clone()).project(s.camera);
  const r = s.canvas.getBoundingClientRect();
  return { x: r.left + (p.x + 1) / 2 * r.width, y: r.top + (1 - p.y) / 2 * r.height };
}, id);

await page.goto(`http://localhost:${server.address().port}/?q=${encodeURIComponent('a red dog next to a blue car')}&t=1`);
await page.waitForFunction(() => window.tinyScene?.ready, null, { timeout: 60000 });
await page.waitForTimeout(300);
let s = await sc();
check(s.ents.length === 2, 'scene from prompt has 2 objects: ' + s.ents.map((e) => e.type).join(', '));
const car = s.ents.find((e) => e.type === 'car');

// click selects
let p = await screenOf(car.id);
await page.mouse.click(p.x, p.y);
check(await page.evaluate(() => window.tinyScene.stage.selected) === car.id, 'clicking the car selects it');
check(await page.evaluate(() => document.getElementById('panel').dataset.tab) === 'edit', 'selecting opens the Editar tab');

// drag moves
p = await screenOf(car.id);
await page.mouse.move(p.x, p.y);
await page.mouse.down();
for (let k = 1; k <= 10; k++) await page.mouse.move(p.x + k * 18, p.y + k * 4);
await page.mouse.up();
s = await sc();
const moved = s.ents.find((e) => e.id === car.id);
check(Math.hypot(moved.dx ?? 0, moved.dz ?? 0) > 0.5, `dragging moves the car (dx=${moved.dx}, dz=${moved.dz})`);
const camAfter = await page.evaluate(() => window.tinyScene.stage.controls.enabled);
check(camAfter, 'orbit controls are re-enabled after the drag');

// panel edits
await page.click('.sw[title="green"]');
check((await sc()).ents.find((e) => e.id === car.id).color === 'green', 'colour swatch paints the car green');
await page.selectOption('label.field:has(span:text-is("Acción")) select', 'drive');
check((await sc()).ents.find((e) => e.id === car.id).act === 'drive', 'action select sets drive');
await page.selectOption('label.field:has(span:text-is("Cantidad")) select', '3');
check((await sc()).ents.find((e) => e.id === car.id).n === 3, 'count select sets 3');

// model edit with "it" targets the selection
await page.evaluate(() => window.tinyScene.submit('make it yellow'));
check((await sc()).ents.find((e) => e.id === car.id).color === 'yellow', '"make it yellow" applies to the selected car');

// undo / redo
await page.keyboard.press('Control+z');
check((await sc()).ents.find((e) => e.id === car.id).color === 'green', 'Ctrl+Z undoes the colour');
await page.keyboard.press('Control+Shift+z');
check((await sc()).ents.find((e) => e.id === car.id).color === 'yellow', 'Ctrl+Shift+Z redoes it');

// add from the panel, then delete with the keyboard
await page.selectOption('label.field:has(span:text-is("Nuevo objeto")) select', 'tree');
await page.click('button:text-is("Añadir")');
s = await sc();
check(s.ents.some((e) => e.type === 'tree'), 'Añadir adds a tree');
const tree = s.ents.find((e) => e.type === 'tree');
check(await page.evaluate(() => window.tinyScene.stage.selected) === tree.id, 'the new tree is selected');
await page.screenshot({ path: process.argv[2] ?? '/dev/null' });
await page.evaluate(() => document.activeElement?.blur());
await page.keyboard.press('Delete');
check(!(await sc()).ents.some((e) => e.type === 'tree'), 'Delete key removes the selected tree');

// click on empty sky deselects
const sky = await page.evaluate(() => { const r = document.getElementById('canvas').getBoundingClientRect(); return { x: r.left + 30, y: r.top + 80 }; });
await page.mouse.click(sky.x, sky.y);
check(await page.evaluate(() => window.tinyScene.stage.selected) === null, 'clicking empty space deselects');

check(!errors.length, 'no page errors' + (errors.length ? ': ' + errors.join(' | ') : ''));
await browser.close();
server.close();
process.exit(fail ? 1 : 0);