|
| 1 | +import type { Locator } from "@playwright/test" |
1 | 2 | import { expect, story } from "../../storybook/playwright/story" |
2 | 3 |
|
| 4 | +// The digits drawn inside the count's clipped width, left to right. Each slot shows whichever visible glyph lines up |
| 5 | +// with it, so a slot mid-roll reads "_", and the spare leading zero outside the width is skipped. |
| 6 | +const shown = (count: Locator) => |
| 7 | + count.locator('[data-slot="animated-number-value"]').evaluate((value) => { |
| 8 | + const clip = value.getBoundingClientRect() |
| 9 | + |
| 10 | + return Array.from(value.querySelectorAll('[data-slot="animated-number-digit"]'), (slot) => ({ |
| 11 | + slot, |
| 12 | + rect: slot.getBoundingClientRect(), |
| 13 | + })) |
| 14 | + .filter((item) => item.rect.left >= clip.left - 0.5 && item.rect.right <= clip.right + 0.5) |
| 15 | + .toSorted((a, b) => a.rect.left - b.rect.left) |
| 16 | + .map( |
| 17 | + (item) => |
| 18 | + Array.from(item.slot.querySelectorAll("[data-digit]")) |
| 19 | + .find( |
| 20 | + (glyph) => |
| 21 | + getComputedStyle(glyph).visibility === "visible" && |
| 22 | + Math.abs(glyph.getBoundingClientRect().top - item.rect.top) < 0.5, |
| 23 | + ) |
| 24 | + ?.getAttribute("data-digit") ?? "_", |
| 25 | + ) |
| 26 | + .join("") |
| 27 | + }) |
| 28 | + |
3 | 29 | for (const reasoningDefaultOpen of [false, true]) { |
4 | 30 | story( |
5 | 31 | `keeps ordered thoughts and tool-only counts with reasoning ${reasoningDefaultOpen ? "expanded" : "collapsed"}`, |
@@ -109,6 +135,94 @@ story("summarizes subagents as Agent while retaining their card titles", async ( |
109 | 135 | await expect(group.locator('[data-component="task-tool-title"]')).toHaveText(["General", "Explore"]) |
110 | 136 | }) |
111 | 137 |
|
| 138 | +for (const direction of ["ltr", "rtl"]) { |
| 139 | + story(`reads and shows a two-digit count once, in order, in ${direction}`, async ({ mount, page }) => { |
| 140 | + const root = await mount("current-tool-group--sticky-header", { globals: { direction } }) |
| 141 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 142 | + await expect(count).toHaveText("37") |
| 143 | + expect(await count.ariaSnapshot()).toBe('- text: "37"') |
| 144 | + // The clipped width follows the web font's metrics. |
| 145 | + await page.evaluate(() => document.fonts.ready.then(() => undefined)) |
| 146 | + expect(await shown(count)).toBe("37") |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +story("does not animate the count on mount or when its font changes", async ({ mount }) => { |
| 151 | + const root = await mount("current-tool-group--sticky-header") |
| 152 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 153 | + await expect(count).toHaveText("37") |
| 154 | + expect(await count.evaluate((element) => element.getAnimations({ subtree: true }).length)).toBe(0) |
| 155 | + |
| 156 | + // Like a late web font swap or a zoom change: the digit width changes without the count changing. |
| 157 | + await count.evaluate((element) => { |
| 158 | + element.style.fontSize = "20px" |
| 159 | + }) |
| 160 | + expect(await count.evaluate((element) => element.getAnimations({ subtree: true }).length)).toBe(0) |
| 161 | +}) |
| 162 | + |
| 163 | +story("settles count changes made while detached, without rolling", async ({ mount }) => { |
| 164 | + const root = await mount("current-tool-group--count-rollover") |
| 165 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 166 | + await expect(count).toHaveText("9") |
| 167 | + |
| 168 | + // A background session keeps adding tools while its cached timeline is detached. |
| 169 | + await root.getByRole("button", { name: "Detach" }).click() |
| 170 | + |
| 171 | + for (let added = 0; added < 12; added++) await root.getByRole("button", { name: "Add read" }).click() |
| 172 | + |
| 173 | + await root.getByRole("button", { name: "Attach" }).click() |
| 174 | + |
| 175 | + await expect(count).toHaveText("21") |
| 176 | + expect(await count.evaluate((element) => element.getAnimations({ subtree: true }).length)).toBe(0) |
| 177 | + await expect(count.locator('[data-animating="true"]')).toHaveCount(0) |
| 178 | + expect(await shown(count)).toBe("21") |
| 179 | +}) |
| 180 | + |
| 181 | +story("keeps a digit rolling when the count changes again mid-roll", async ({ mount, page }) => { |
| 182 | + const root = await mount("current-tool-group--count-rollover") |
| 183 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 184 | + // A slow roll keeps the second change inside the first roll on a loaded machine. |
| 185 | + await page.addStyleTag({ content: ":root { --tool-motion-odometer-ms: 5000ms }" }) |
| 186 | + await expect(count).toHaveText("9") |
| 187 | + |
| 188 | + await root.getByRole("button", { name: "Add read" }).click() |
| 189 | + await root.getByRole("button", { name: "Add read" }).click() |
| 190 | + await expect(count).toHaveText("11") |
| 191 | + // 9 -> 10 starts the tens digit rolling; 10 -> 11 leaves it unchanged, so it finishes its roll instead of jumping. |
| 192 | + await expect(count.locator('[data-animating="true"]')).toHaveCount(2) |
| 193 | +}) |
| 194 | + |
| 195 | +story("rolls the leading digit in and out when the count gains or loses a digit", async ({ mount }) => { |
| 196 | + const root = await mount("current-tool-group--count-rollover") |
| 197 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 198 | + const rolling = count.locator('[data-animating="true"]') |
| 199 | + await expect(count).toHaveText("9") |
| 200 | + |
| 201 | + await root.getByRole("button", { name: "Add read" }).click() |
| 202 | + // The tens digit rolls in from zero beside the ones digit instead of appearing. |
| 203 | + await expect(rolling).toHaveCount(2) |
| 204 | + expect(await count.ariaSnapshot()).toBe('- text: "10"') |
| 205 | + await expect(rolling).toHaveCount(0) |
| 206 | + |
| 207 | + await root.getByRole("button", { name: "Remove read" }).click() |
| 208 | + // The tens digit rolls back to zero before it leaves instead of vanishing. |
| 209 | + await expect(rolling).toHaveCount(2) |
| 210 | + expect(await count.ariaSnapshot()).toBe('- text: "9"') |
| 211 | + await expect(rolling).toHaveCount(0) |
| 212 | + await expect(count).toHaveText("9") |
| 213 | +}) |
| 214 | + |
| 215 | +story("rolls the count and exposes only its current value while rolling", async ({ mount }) => { |
| 216 | + const root = await mount("current-tool-group--mixed-reasoning") |
| 217 | + const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]') |
| 218 | + await expect(count).toHaveText("4") |
| 219 | + await root.getByRole("button", { name: "Append follow-up read" }).click() |
| 220 | + await expect(count.locator('[data-animating="true"]')).toHaveCount(1) |
| 221 | + // One read mid-roll: a retrying assertion would pass once the strip hides again. |
| 222 | + expect(await count.ariaSnapshot()).toBe('- text: "5"') |
| 223 | + await expect(count).toHaveText("5") |
| 224 | +}) |
| 225 | + |
112 | 226 | for (const width of [840, 390]) { |
113 | 227 | story(`keeps grouped cards inside their trigger bounds at ${width}px`, async ({ mount, page }) => { |
114 | 228 | await page.setViewportSize({ width, height: 600 }) |
|
0 commit comments