Open History to see the last commit
1import { expect, test } from "@playwright/test"
2import { openGame } from "./helpers"
3
4for (const pixelRatio of [1, 1.3, 2]) {
5 test.describe(`Board density ${pixelRatio}`, () => {
6 test.use({ deviceScaleFactor: pixelRatio })
7
8 test("keeps the minimap sharp across responsive sizes", async ({ page }) => {
9 await openGame(page)
10 const minimap = page.getByTestId("world-view").locator("canvas")
11 for (const width of [1600, 500]) {
12 await page.setViewportSize({ width, height: 900 })
13 await expect
14 .poll(() =>
15 minimap.evaluate((el) => {
16 const canvas = el as HTMLCanvasElement
17 const size = canvas.getBoundingClientRect().width
18 const ctx = canvas.getContext("2d")!
19 const expected = Math.round(size * devicePixelRatio)
20 return {
21 widthMatches: canvas.width === expected,
22 heightMatches: canvas.height === expected,
23 scaleMatches: Math.abs(ctx.getTransform().a - expected / size) < 0.0001,
24 smoothing: ctx.imageSmoothingEnabled,
25 quality: ctx.imageSmoothingQuality,
26 }
27 }),
28 )
29 .toEqual({
30 widthMatches: true,
31 heightMatches: true,
32 scaleMatches: true,
33 smoothing: true,
34 quality: "high",
35 })
36 }
37 })
38
39 test("keeps tile resolution and antialiasing through a drawing frame", async ({ page }) => {
40 // Observe real texture dimensions and transforms without changing rendering.
41 await page.addInitScript(() => {
42 const samples: Array<{ pixels: number; period: number }> = []
43 Object.assign(window, { patternSamples: samples })
44 const original = CanvasRenderingContext2D.prototype.createPattern
45 CanvasRenderingContext2D.prototype.createPattern = function (source, repetition) {
46 const pattern = original.call(this, source, repetition)
47 if (pattern && source instanceof OffscreenCanvas) {
48 const setTransform = pattern.setTransform.bind(pattern)
49 pattern.setTransform = (matrix) => {
50 samples.push({ pixels: source.width, period: source.width * (matrix?.a ?? 1) })
51 setTransform(matrix)
52 }
53 }
54 return pattern
55 }
56 })
57 await page.route("**/api/draw", async (route) => {
58 await new Promise((resolve) => setTimeout(resolve, 350))
59 await route.fulfill({
60 status: 409,
61 json: { ok: false, code: "EDGE_TAKEN", quotaRemaining: 8, refillIn: 0 },
62 })
63 })
64 await openGame(page)
65 const canvas = page.getByTestId("game-canvas")
66 const inspect = () =>
67 canvas.evaluate((el) => {
68 const canvas = el as HTMLCanvasElement
69 const rect = canvas.getBoundingClientRect()
70 const ctx = canvas.getContext("2d")!
71 return {
72 width: canvas.width,
73 height: canvas.height,
74 expectedWidth: Math.floor(rect.width * devicePixelRatio),
75 expectedHeight: Math.floor(rect.height * devicePixelRatio),
76 scale: ctx.getTransform().a,
77 smoothing: ctx.imageSmoothingEnabled,
78 quality: ctx.imageSmoothingQuality,
79 }
80 })
81 const before = await inspect()
82 expect(before.width).toBe(before.expectedWidth)
83 expect(before.height).toBe(before.expectedHeight)
84 expect(before.scale).toBeCloseTo(pixelRatio)
85 expect(before.smoothing).toBe(true)
86 expect(before.quality).toBe("high")
87 const samples = await page.evaluate(
88 () =>
89 (window as unknown as { patternSamples: Array<{ pixels: number; period: number }> })
90 .patternSamples,
91 )
92 expect(samples.length).toBeGreaterThanOrEqual(2)
93 for (const sample of samples) {
94 expect(sample.pixels).toBe(Math.ceil(128 * pixelRatio))
95 // Preserve four-cell spacing, allowing native DOMMatrix float rounding.
96 expect(sample.period).toBeCloseTo(80, 4)
97 }
98 const bounds = (await canvas.boundingBox())!
99 const position = { x: bounds.width / 2 + 10, y: bounds.height / 2 + 40 }
100 await page.mouse.move(bounds.x + position.x, bounds.y + position.y)
101 await canvas.click({ position })
102 await expect(page.getByTestId("drawing-pencil")).toHaveAttribute("data-drawing", "true")
103 expect(await inspect()).toEqual(before)
104 await expect(page.getByTestId("drawing-pencil")).toHaveAttribute("data-drawing", "false")
105 expect(await inspect()).toEqual(before)
106 })
107 })
108}