|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +import { |
| 5 | + X01_PLAYER_CARD_SELECTOR, |
| 6 | + X01_PLAYER_DISPLAY_ROOT_SELECTOR, |
| 7 | + X01_PLAYER_NAME_SELECTOR, |
| 8 | + X01_PLAYER_SCORE_SELECTOR, |
| 9 | + createX01PlayerSurfaceObserveOptions, |
| 10 | + findX01PlayerSurface, |
| 11 | + getX01PlayerSurfaceSnapshot, |
| 12 | +} from "../../src/features/shared/x01-player-surface-adapter.js"; |
| 13 | +import { FakeDocument } from "./fake-dom.js"; |
| 14 | + |
| 15 | +function removeDefaultPlayerNodes(documentRef) { |
| 16 | + documentRef.activePlayerRow.remove(); |
| 17 | + documentRef.winnerNode.remove(); |
| 18 | +} |
| 19 | + |
| 20 | +function appendPlayerDisplayRoot(documentRef) { |
| 21 | + removeDefaultPlayerNodes(documentRef); |
| 22 | + const root = documentRef.createElement("div"); |
| 23 | + root.id = "ad-ext-player-display"; |
| 24 | + documentRef.main.appendChild(root); |
| 25 | + return root; |
| 26 | +} |
| 27 | + |
| 28 | +function appendPlayerCard(documentRef, root, options = {}) { |
| 29 | + const wrapper = options.nested ? documentRef.createElement("div") : null; |
| 30 | + const card = documentRef.createElement("div"); |
| 31 | + card.id = options.id || ""; |
| 32 | + card.classList.add("ad-ext-player"); |
| 33 | + |
| 34 | + if (options.active === true) { |
| 35 | + card.classList.add("ad-ext-player-active"); |
| 36 | + } else if (options.inactive === true) { |
| 37 | + card.classList.add("ad-ext-player-inactive"); |
| 38 | + } |
| 39 | + |
| 40 | + if (typeof options.scoreText !== "undefined") { |
| 41 | + const scoreNode = documentRef.createElement("p"); |
| 42 | + scoreNode.classList.add("ad-ext-player-score"); |
| 43 | + scoreNode.textContent = options.scoreText; |
| 44 | + card.appendChild(scoreNode); |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof options.nameText !== "undefined") { |
| 48 | + const nameNode = documentRef.createElement("span"); |
| 49 | + nameNode.classList.add("ad-ext-player-name"); |
| 50 | + nameNode.textContent = options.nameText; |
| 51 | + card.appendChild(nameNode); |
| 52 | + } |
| 53 | + |
| 54 | + if (wrapper) { |
| 55 | + wrapper.appendChild(card); |
| 56 | + root.appendChild(wrapper); |
| 57 | + } else { |
| 58 | + root.appendChild(card); |
| 59 | + } |
| 60 | + |
| 61 | + return card; |
| 62 | +} |
| 63 | + |
| 64 | +test("x01 player surface adapter exports the external baseline selectors", () => { |
| 65 | + assert.equal(X01_PLAYER_DISPLAY_ROOT_SELECTOR, "#ad-ext-player-display"); |
| 66 | + assert.equal(X01_PLAYER_CARD_SELECTOR, ".ad-ext-player, [id^=\"ad-ext-player-\"]"); |
| 67 | + assert.equal(X01_PLAYER_SCORE_SELECTOR, ".ad-ext-player-score"); |
| 68 | + assert.equal(X01_PLAYER_NAME_SELECTOR, ".ad-ext-player-name"); |
| 69 | +}); |
| 70 | + |
| 71 | +test("x01 player surface adapter returns an empty snapshot for invalid document input", () => { |
| 72 | + assert.deepEqual(getX01PlayerSurfaceSnapshot(null), { |
| 73 | + playerDisplayRoot: null, |
| 74 | + playerCards: [], |
| 75 | + players: [], |
| 76 | + source: "none", |
| 77 | + }); |
| 78 | + assert.deepEqual(findX01PlayerSurface({}), { |
| 79 | + playerDisplayRoot: null, |
| 80 | + playerCards: [], |
| 81 | + players: [], |
| 82 | + source: "none", |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test("x01 player surface adapter returns source none when player display root is absent", () => { |
| 87 | + const documentRef = new FakeDocument(); |
| 88 | + |
| 89 | + const snapshot = getX01PlayerSurfaceSnapshot(documentRef); |
| 90 | + |
| 91 | + assert.equal(snapshot.playerDisplayRoot, null); |
| 92 | + assert.deepEqual(snapshot.playerCards, []); |
| 93 | + assert.deepEqual(snapshot.players, []); |
| 94 | + assert.equal(snapshot.source, "none"); |
| 95 | +}); |
| 96 | + |
| 97 | +test("x01 player surface adapter reads two players with names and scores", () => { |
| 98 | + const documentRef = new FakeDocument(); |
| 99 | + const root = appendPlayerDisplayRoot(documentRef); |
| 100 | + const firstCard = appendPlayerCard(documentRef, root, { |
| 101 | + id: "ad-ext-player-0", |
| 102 | + nameText: "TORNADO TOM", |
| 103 | + scoreText: "423", |
| 104 | + active: true, |
| 105 | + }); |
| 106 | + const secondCard = appendPlayerCard(documentRef, root, { |
| 107 | + id: "ad-ext-player-1", |
| 108 | + nameText: "TEST2", |
| 109 | + scoreText: "501", |
| 110 | + inactive: true, |
| 111 | + }); |
| 112 | + |
| 113 | + const snapshot = getX01PlayerSurfaceSnapshot(documentRef); |
| 114 | + |
| 115 | + assert.equal(snapshot.playerDisplayRoot, root); |
| 116 | + assert.deepEqual(snapshot.playerCards, [firstCard, secondCard]); |
| 117 | + assert.equal(snapshot.source, "tools-for-autodarts"); |
| 118 | + assert.deepEqual( |
| 119 | + snapshot.players.map((player) => ({ |
| 120 | + index: player.index, |
| 121 | + id: player.id, |
| 122 | + nameText: player.nameText, |
| 123 | + scoreText: player.scoreText, |
| 124 | + isActive: player.isActive, |
| 125 | + })), |
| 126 | + [ |
| 127 | + { |
| 128 | + index: 0, |
| 129 | + id: "ad-ext-player-0", |
| 130 | + nameText: "TORNADO TOM", |
| 131 | + scoreText: "423", |
| 132 | + isActive: true, |
| 133 | + }, |
| 134 | + { |
| 135 | + index: 1, |
| 136 | + id: "ad-ext-player-1", |
| 137 | + nameText: "TEST2", |
| 138 | + scoreText: "501", |
| 139 | + isActive: false, |
| 140 | + }, |
| 141 | + ] |
| 142 | + ); |
| 143 | +}); |
| 144 | + |
| 145 | +test("x01 player surface adapter handles missing score and name nodes defensively", () => { |
| 146 | + const documentRef = new FakeDocument(); |
| 147 | + const root = appendPlayerDisplayRoot(documentRef); |
| 148 | + appendPlayerCard(documentRef, root, { |
| 149 | + id: "ad-ext-player-0", |
| 150 | + }); |
| 151 | + |
| 152 | + const snapshot = getX01PlayerSurfaceSnapshot(documentRef); |
| 153 | + |
| 154 | + assert.equal(snapshot.players.length, 1); |
| 155 | + assert.equal(snapshot.players[0].nameText, ""); |
| 156 | + assert.equal(snapshot.players[0].scoreText, ""); |
| 157 | + assert.equal(snapshot.players[0].isActive, false); |
| 158 | +}); |
| 159 | + |
| 160 | +test("x01 player surface adapter collects nested player cards safely", () => { |
| 161 | + const documentRef = new FakeDocument(); |
| 162 | + const root = appendPlayerDisplayRoot(documentRef); |
| 163 | + const nestedCard = appendPlayerCard(documentRef, root, { |
| 164 | + id: "ad-ext-player-0", |
| 165 | + nameText: "Nested", |
| 166 | + scoreText: "170", |
| 167 | + nested: true, |
| 168 | + }); |
| 169 | + |
| 170 | + const snapshot = getX01PlayerSurfaceSnapshot(documentRef); |
| 171 | + |
| 172 | + assert.deepEqual(snapshot.playerCards, [nestedCard]); |
| 173 | + assert.equal(snapshot.players[0].nameText, "Nested"); |
| 174 | + assert.equal(snapshot.players[0].scoreText, "170"); |
| 175 | +}); |
| 176 | + |
| 177 | +test("x01 player surface observe options stay scoped to class changes", () => { |
| 178 | + assert.deepEqual(createX01PlayerSurfaceObserveOptions(), { |
| 179 | + childList: true, |
| 180 | + subtree: true, |
| 181 | + characterData: true, |
| 182 | + attributes: true, |
| 183 | + attributeFilter: ["class"], |
| 184 | + }); |
| 185 | +}); |
0 commit comments