|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Claude account-pool card inset contract. |
| 5 | + * |
| 6 | + * Source-text assertions, not rendered measurements: happy-dom performs no layout, so a |
| 7 | + * getBoundingClientRect() here returns zeros and would prove nothing. Rendered proof was |
| 8 | + * captured in a real browser during the fix — the title sat 17px from the card's left |
| 9 | + * edge while the warning box and the threshold input sat at 1px. This file's job is to |
| 10 | + * keep the CSS shape that produced that gap from coming back silently. |
| 11 | + * |
| 12 | + * The bug: `.card` carries no padding of its own, so each child supplies the 16px inset. |
| 13 | + * `.card-row`, `.card-sub` and `.setting-row` all do, which is why the title row, the help |
| 14 | + * line and the rotation rows were indented. The experimental warning box and the threshold |
| 15 | + * field are none of those three, so they ran flush against the card border. |
| 16 | + */ |
| 17 | + |
| 18 | +const cssUrl = new URL("../src/styles.css", import.meta.url); |
| 19 | +const tsxUrl = new URL( |
| 20 | + "../src/components/provider-workspace/AnthropicAccountPoolSettings.tsx", |
| 21 | + import.meta.url, |
| 22 | +); |
| 23 | + |
| 24 | +/** Strip comments so no assertion can pass on prose that quotes a value. */ |
| 25 | +function withoutComments(css: string): string { |
| 26 | + return css.replace(/\/\*[\s\S]*?\*\//g, ""); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Body of a top-level rule. Anchored with no leading whitespace on purpose: `.setting-row` |
| 31 | + * also appears indented inside an `@media` block that only sets `flex-wrap`, and matching |
| 32 | + * that one instead would read the inset off the wrong rule. |
| 33 | + */ |
| 34 | +function ruleBody(css: string, selector: string): string { |
| 35 | + const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 36 | + const match = css.match(new RegExp(`(^|\\n)${escaped}\\s*\\{([^}]*)\\}`)); |
| 37 | + if (!match) throw new Error(`rule not found: ${selector}`); |
| 38 | + return match[2]; |
| 39 | +} |
| 40 | + |
| 41 | +/** Horizontal component of a `margin`/`padding` shorthand, in px. */ |
| 42 | +function horizontal(body: string, property: "margin" | "padding"): number { |
| 43 | + const match = body.match(new RegExp(`(?:^|;)\\s*${property}:\\s*([^;]+)`)); |
| 44 | + if (!match) throw new Error(`${property} not found in: ${body.trim()}`); |
| 45 | + const parts = match[1].trim().split(/\s+/); |
| 46 | + // 1 value: all sides. 2-3 values: second is the horizontal one. 4: right, then left. |
| 47 | + const side = parts.length === 1 ? parts[0] : parts[1]; |
| 48 | + const px = side.match(/^([\d.]+)(?:px)?$/); |
| 49 | + if (!px) throw new Error(`${property} value is not a px length: ${side}`); |
| 50 | + return Number(px[1]); |
| 51 | +} |
| 52 | + |
| 53 | +/** The inset every padded row in this card already uses. */ |
| 54 | +async function rowInset(): Promise<number> { |
| 55 | + const css = withoutComments(await Bun.file(cssUrl).text()); |
| 56 | + const cardRow = horizontal(ruleBody(css, ".card-row"), "padding"); |
| 57 | + const settingRow = horizontal(ruleBody(css, ".setting-row"), "padding"); |
| 58 | + const cardSub = horizontal(ruleBody(css, ".card-sub"), "padding"); |
| 59 | + // The three disagree only if the card's own baseline moved, which this contract cannot |
| 60 | + // silently follow — a mismatch means the target inset has to be re-decided, not guessed. |
| 61 | + expect(settingRow).toBe(cardRow); |
| 62 | + expect(cardSub).toBe(cardRow); |
| 63 | + return cardRow; |
| 64 | +} |
| 65 | + |
| 66 | +test("the warning box is inset by the same amount as the card's padded rows", async () => { |
| 67 | + const css = withoutComments(await Bun.file(cssUrl).text()); |
| 68 | + const notice = ruleBody(css, ".anthropic-pool-card__notice"); |
| 69 | + |
| 70 | + // Margin, not padding: the box draws a border, so padding alone insets the text and |
| 71 | + // leaves the border itself sitting on the card edge — the original bug, one layer in. |
| 72 | + expect(horizontal(notice, "margin")).toBe(await rowInset()); |
| 73 | +}); |
| 74 | + |
| 75 | +test("the threshold field is inset by the same amount as the card's padded rows", async () => { |
| 76 | + const css = withoutComments(await Bun.file(cssUrl).text()); |
| 77 | + const field = ruleBody(css, ".anthropic-pool-card__field"); |
| 78 | + |
| 79 | + // The `.input` inside is full-width, so the field carries the inset for the whole group; |
| 80 | + // without it the input's own border reaches the card edge. |
| 81 | + expect(horizontal(field, "padding")).toBe(await rowInset()); |
| 82 | +}); |
| 83 | + |
| 84 | +test("the help line inside the threshold field is not indented twice", async () => { |
| 85 | + const css = withoutComments(await Bun.file(cssUrl).text()); |
| 86 | + |
| 87 | + // `.card-sub` brings its own 16px, which lands on top of the field's inset and pushes the |
| 88 | + // help text out of line with the label directly above it. |
| 89 | + const group = css.match( |
| 90 | + /(^|\n)([^{}]*\.anthropic-pool-card__field \.card-sub\s*)\{([^}]*)\}/, |
| 91 | + ); |
| 92 | + expect(group).not.toBeNull(); |
| 93 | + expect(horizontal(group![3], "padding")).toBe(0); |
| 94 | +}); |
| 95 | + |
| 96 | +test("the component styles the warning and the field by class, not inline", async () => { |
| 97 | + const tsx = await Bun.file(tsxUrl).text(); |
| 98 | + |
| 99 | + expect(tsx).toContain('className="card-sub anthropic-pool-card__notice"'); |
| 100 | + expect(tsx).toContain('className="field anthropic-pool-card__field"'); |
| 101 | + |
| 102 | + // The inline box styles are what bypassed the inset: an inline `padding` also outranks |
| 103 | + // any stylesheet rule, so reintroducing one silently disables the fix above. |
| 104 | + expect(tsx).not.toContain('padding: "10px 16px"'); |
| 105 | + expect(tsx).not.toContain("borderRadius"); |
| 106 | + expect(tsx).not.toMatch(/style=\{\{[^}]*marginTop: 10/); |
| 107 | + expect(tsx).not.toMatch(/style=\{\{[^}]*display: "block"/); |
| 108 | +}); |
0 commit comments