| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | import { installLocalCursorFallback } from "./remote-cursor.mjs"; |
| 4 | |
| 5 | function cursorStub() { |
| 6 | return { |
| 7 | _target: { style: { cursor: "none" } }, |
| 8 | changes: [], |
| 9 | change(...args) { |
| 10 | this.changes.push(args); |
| 11 | this._target.style.cursor = "remote"; |
| 12 | }, |
| 13 | clear() { |
| 14 | this._target.style.cursor = "none"; |
| 15 | }, |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | const visibleShape = [0, 0, 0, 255]; |
| 20 | |
| 21 | test("macOS browser fallback remains visible before a remote cursor arrives", () => { |
| 22 | const cursor = cursorStub(); |
| 23 | cursor._target = null; |
| 24 | installLocalCursorFallback(cursor, true); |
| 25 | cursor._target = { style: { cursor: "none" } }; |
| 26 | cursor.clear(); |
| 27 | assert.equal(cursor._target.style.cursor, "default"); |
| 28 | assert.deepEqual(cursor.changes, []); |
| 29 | }); |
| 30 | |
| 31 | test("macOS browser fallback replaces an empty or transparent remote cursor", () => { |
| 32 | const cursor = cursorStub(); |
| 33 | installLocalCursorFallback(cursor, true); |
| 34 | for (const rgba of [[], [0, 0, 0, 0], [255, 255, 255, 0, 0, 0, 0, 0]]) { |
| 35 | cursor.change(rgba, 0, 0, rgba.length / 4, 1); |
| 36 | assert.equal(cursor._target.style.cursor, "default"); |
| 37 | } |
| 38 | assert.deepEqual(cursor.changes, []); |
| 39 | }); |
| 40 | |
| 41 | test("a real remote cursor shape is preserved without fallback pixels", () => { |
| 42 | const cursor = cursorStub(); |
| 43 | installLocalCursorFallback(cursor, true); |
| 44 | cursor.change(visibleShape, 2, 3, 1, 1); |
| 45 | assert.equal(cursor._target.style.cursor, "remote"); |
| 46 | assert.deepEqual(cursor.changes, [[visibleShape, 2, 3, 1, 1]]); |
| 47 | }); |
| 48 | |
| 49 | test("non-target displays retain noVNC cursor behavior", () => { |
| 50 | const cursor = cursorStub(); |
| 51 | const change = cursor.change; |
| 52 | const clear = cursor.clear; |
| 53 | installLocalCursorFallback(cursor, false); |
| 54 | assert.equal(cursor.change, change); |
| 55 | assert.equal(cursor.clear, clear); |
| 56 | cursor.clear(); |
| 57 | assert.equal(cursor._target.style.cursor, "none"); |
| 58 | cursor.change([], 0, 0, 0, 0); |
| 59 | assert.equal(cursor._target.style.cursor, "remote"); |
| 60 | }); |