|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Headless regression tests for #OPTION charset lower + ASCII/CHR$ on canvas WASM. |
| 3 | +
|
| 4 | +Catches wrong gfx_put_byte mapping (space as !, CHR$(65) wrong vs "A", IDE without -petscii). |
| 5 | +Requires: make basic-wasm-canvas, pip install -r tests/requirements-wasm.txt, playwright install chromium. |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import http.server |
| 10 | +import socketserver |
| 11 | +import sys |
| 12 | +import threading |
| 13 | +import time |
| 14 | +from functools import partial |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parents[1] |
| 18 | +WEB = ROOT / "web" |
| 19 | + |
| 20 | + |
| 21 | +def _serve_web() -> tuple[socketserver.TCPServer, int]: |
| 22 | + Handler = partial(http.server.SimpleHTTPRequestHandler, directory=str(WEB)) |
| 23 | + socketserver.TCPServer.allow_reuse_address = True |
| 24 | + httpd = socketserver.TCPServer(("127.0.0.1", 0), Handler) |
| 25 | + port = httpd.server_address[1] |
| 26 | + |
| 27 | + def run() -> None: |
| 28 | + httpd.serve_forever() |
| 29 | + |
| 30 | + threading.Thread(target=run, daemon=True).start() |
| 31 | + return httpd, port |
| 32 | + |
| 33 | + |
| 34 | +def _click_run(page) -> None: |
| 35 | + page.evaluate("document.getElementById('run').click()") |
| 36 | + |
| 37 | + |
| 38 | +def _canvas_pixel_rgba(page, x: int, y: int) -> tuple[int, int, int, int]: |
| 39 | + return page.evaluate( |
| 40 | + """([x, y]) => { |
| 41 | + const c = document.getElementById('screen'); |
| 42 | + const ctx = c.getContext('2d'); |
| 43 | + const d = ctx.getImageData(x, y, 1, 1).data; |
| 44 | + return [d[0], d[1], d[2], d[3]]; |
| 45 | + }""", |
| 46 | + [x, y], |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _set_petscii(page, on: bool) -> None: |
| 51 | + page.evaluate(f"() => {{ document.getElementById('optPetscii').checked = {str(on).lower()}; }}") |
| 52 | + |
| 53 | + |
| 54 | +def _run_charset_suite(page, *, petscii_checked: bool) -> None: |
| 55 | + """Assert mixed-case string, space, and CHR$(65) match \"A\" on same charset.""" |
| 56 | + label = "petscii on" if petscii_checked else "petscii off" |
| 57 | + _set_petscii(page, petscii_checked) |
| 58 | + page.wait_for_function("() => !document.getElementById('run').disabled", timeout=60000) |
| 59 | + |
| 60 | + # Line 0: mixed case + spaces (user-reported pattern) |
| 61 | + page.fill( |
| 62 | + "#program", |
| 63 | + '#OPTION charset lower\n' |
| 64 | + '10 COLOR 1\n' |
| 65 | + '20 BACKGROUND 6\n' |
| 66 | + '30 PRINT "hEY hEY"\n' |
| 67 | + '40 PRINT CHR$(32)\n' |
| 68 | + '50 PRINT CHR$(65)\n' |
| 69 | + '60 PRINT "A"\n' |
| 70 | + "70 END\n", |
| 71 | + ) |
| 72 | + _click_run(page) |
| 73 | + page.wait_for_function( |
| 74 | + "() => (window.Module && Module.wasmGfxRunDone === 1)", |
| 75 | + timeout=120000, |
| 76 | + ) |
| 77 | + log = page.text_content("#log") or "" |
| 78 | + if log.strip(): |
| 79 | + raise RuntimeError(f"{label}: error log: {log!r}") |
| 80 | + |
| 81 | + # Row 0 "hEY hEY" — space between words at column 3 (0-based), center x = 3*8+4 = 28 |
| 82 | + px_h = _canvas_pixel_rgba(page, 4, 4) |
| 83 | + px_space_word = _canvas_pixel_rgba(page, 28, 4) |
| 84 | + if list(px_h[:3]) == list(px_space_word[:3]): |
| 85 | + raise RuntimeError( |
| 86 | + f"{label}: first 'h' vs space in 'hEY hEY' should differ, got h={px_h!r} sp={px_space_word!r}" |
| 87 | + ) |
| 88 | + |
| 89 | + # CHR$(32) line: only a space — interior of cell should match word-space (both true space) |
| 90 | + px_chr32 = _canvas_pixel_rgba(page, 4, 12) |
| 91 | + if list(px_chr32[:3]) != list(px_space_word[:3]): |
| 92 | + raise RuntimeError( |
| 93 | + f"{label}: CHR$(32) center should match literal space pixel, " |
| 94 | + f"chr32={px_chr32!r} lit_sp={px_space_word!r}" |
| 95 | + ) |
| 96 | + |
| 97 | + # CHR$(65) vs "A" on next lines (y=20 and y=28 for rows 2 and 3 at cell center y=4+8*r) |
| 98 | + px_chr65 = _canvas_pixel_rgba(page, 4, 20) |
| 99 | + px_quote_a = _canvas_pixel_rgba(page, 4, 28) |
| 100 | + if list(px_chr65[:3]) != list(px_quote_a[:3]): |
| 101 | + raise RuntimeError( |
| 102 | + f"{label}: CHR$(65) vs PRINT \"A\" should match at (4,20) vs (4,28), " |
| 103 | + f"chr65={px_chr65!r} qA={px_quote_a!r}" |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +def main() -> int: |
| 108 | + if not (WEB / "basic-canvas.js").is_file() or not (WEB / "basic-canvas.wasm").is_file(): |
| 109 | + print("error: run make basic-wasm-canvas first", file=sys.stderr) |
| 110 | + return 1 |
| 111 | + try: |
| 112 | + from playwright.sync_api import sync_playwright |
| 113 | + except ImportError: |
| 114 | + print( |
| 115 | + "error: pip install -r tests/requirements-wasm.txt && playwright install chromium", |
| 116 | + file=sys.stderr, |
| 117 | + ) |
| 118 | + return 1 |
| 119 | + |
| 120 | + httpd, port = _serve_web() |
| 121 | + url = f"http://127.0.0.1:{port}/canvas.html" |
| 122 | + try: |
| 123 | + with sync_playwright() as p: |
| 124 | + browser = p.chromium.launch(headless=True) |
| 125 | + page = browser.new_page(viewport={"width": 1100, "height": 900}) |
| 126 | + page.goto(url, wait_until="networkidle", timeout=120000) |
| 127 | + page.wait_for_function("() => !document.getElementById('run').disabled", timeout=120000) |
| 128 | + |
| 129 | + _run_charset_suite(page, petscii_checked=True) |
| 130 | + _run_charset_suite(page, petscii_checked=False) |
| 131 | + _set_petscii(page, True) |
| 132 | + |
| 133 | + browser.close() |
| 134 | + finally: |
| 135 | + httpd.shutdown() |
| 136 | + httpd.server_close() |
| 137 | + |
| 138 | + print("wasm_canvas_charset_test: OK") |
| 139 | + return 0 |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + raise SystemExit(main()) |
0 commit comments