|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + studioSetStyle, |
| 5 | + studioSetText, |
| 6 | + type ContentToolDeps, |
| 7 | + type StudioSetStyleResult, |
| 8 | + type StudioSetTextResult, |
| 9 | +} from "./contentTools"; |
| 10 | +import { expectFailure, expectOk, previewElement, selectionFor } from "../webmcpTestUtils"; |
| 11 | + |
| 12 | +function contentDeps(overrides: Partial<ContentToolDeps> = {}): ContentToolDeps { |
| 13 | + const element = previewElement('<h1 id="headline">Ship it</h1>', "headline"); |
| 14 | + return { |
| 15 | + getCurrentSelection: () => selectionFor(element), |
| 16 | + getWriteBlockedReason: () => null, |
| 17 | + setText: async () => ({ ok: true }), |
| 18 | + setStyle: async () => ({ ok: true }), |
| 19 | + ...overrides, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("studioSetText", () => { |
| 24 | + it("writes the text and reports what it now is", async () => { |
| 25 | + const setText = vi.fn(async () => ({ ok: true }) as const); |
| 26 | + |
| 27 | + const result = await studioSetText(contentDeps({ setText }), { text: "Ship it faster" }); |
| 28 | + |
| 29 | + const ok = expectOk<StudioSetTextResult>(result); |
| 30 | + expect(ok.text).toBe("Ship it faster"); |
| 31 | + expect(ok.changed).toBe(true); |
| 32 | + expect(setText).toHaveBeenCalledWith("Ship it faster", undefined); |
| 33 | + }); |
| 34 | + |
| 35 | + it("reports changed:false when the text already said that", async () => { |
| 36 | + const result = await studioSetText(contentDeps(), { text: "Ship it" }); |
| 37 | + |
| 38 | + expect(expectOk<StudioSetTextResult>(result).changed).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it("refuses to write while a conflict is waiting for the user", async () => { |
| 42 | + // The paused-save and conflict states are banners with no lock behind them. |
| 43 | + // Nothing else stops a programmatic write landing on top of a decision the |
| 44 | + // user has been asked to make. |
| 45 | + const setText = vi.fn(); |
| 46 | + |
| 47 | + const result = expectFailure( |
| 48 | + await studioSetText( |
| 49 | + contentDeps({ |
| 50 | + getWriteBlockedReason: () => "an external change to this file is waiting to be resolved", |
| 51 | + setText, |
| 52 | + }), |
| 53 | + { text: "Ship it faster" }, |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + expect(result.kind).toBe("blocked"); |
| 58 | + expect(result.reason).toMatch(/external change/); |
| 59 | + expect(setText).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("does not report success when the commit declined", async () => { |
| 63 | + // The whole reason the handlers now return an outcome: they resolve on |
| 64 | + // failure, so awaiting them proves nothing. |
| 65 | + const result = expectFailure( |
| 66 | + await studioSetText( |
| 67 | + contentDeps({ setText: async () => ({ ok: false, reason: "persist-failed" }) }), |
| 68 | + { text: "Ship it faster" }, |
| 69 | + ), |
| 70 | + ); |
| 71 | + |
| 72 | + expect(result.kind).toBe("failed"); |
| 73 | + expect(result.reason).toMatch(/persist-failed/); |
| 74 | + }); |
| 75 | + |
| 76 | + it("turns a decline reason into a hint naming what to do instead", async () => { |
| 77 | + const result = expectFailure( |
| 78 | + await studioSetText( |
| 79 | + contentDeps({ setText: async () => ({ ok: false, reason: "not-text-editable" }) }), |
| 80 | + { text: "x" }, |
| 81 | + ), |
| 82 | + ); |
| 83 | + |
| 84 | + expect(result.kind).toBe("blocked"); |
| 85 | + expect(result.hint).toMatch(/studio_inspect/); |
| 86 | + }); |
| 87 | + |
| 88 | + it("rejects a non-string text without dispatching", async () => { |
| 89 | + const setText = vi.fn(); |
| 90 | + |
| 91 | + const result = expectFailure(await studioSetText(contentDeps({ setText }), { text: 42 })); |
| 92 | + |
| 93 | + expect(result.kind).toBe("invalid"); |
| 94 | + expect(setText).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("fails when nothing is selected", async () => { |
| 98 | + const setText = vi.fn(); |
| 99 | + |
| 100 | + const result = expectFailure( |
| 101 | + await studioSetText(contentDeps({ getCurrentSelection: () => null, setText }), { text: "x" }), |
| 102 | + ); |
| 103 | + |
| 104 | + expect(result.kind).toBe("invalid"); |
| 105 | + expect(result.hint).toMatch(/studio_select/); |
| 106 | + expect(setText).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("studioSetStyle", () => { |
| 111 | + it("applies every property and reports them", async () => { |
| 112 | + const setStyle = vi.fn(async () => ({ ok: true }) as const); |
| 113 | + |
| 114 | + const result = await studioSetStyle(contentDeps({ setStyle }), { |
| 115 | + styles: { color: "red", "font-size": "48px" }, |
| 116 | + }); |
| 117 | + |
| 118 | + const ok = expectOk<StudioSetStyleResult>(result); |
| 119 | + expect(ok.applied).toEqual({ color: "red", "font-size": "48px" }); |
| 120 | + expect(ok.rejected).toEqual({}); |
| 121 | + expect(setStyle).toHaveBeenCalledTimes(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it("commits sequentially, never concurrently", async () => { |
| 125 | + // Two commits racing through Studio's client-side read-modify-write can |
| 126 | + // record undo entries that both claim the same starting content. |
| 127 | + let inFlight = 0; |
| 128 | + let maxInFlight = 0; |
| 129 | + const setStyle = vi.fn(async () => { |
| 130 | + inFlight += 1; |
| 131 | + maxInFlight = Math.max(maxInFlight, inFlight); |
| 132 | + await Promise.resolve(); |
| 133 | + inFlight -= 1; |
| 134 | + return { ok: true } as const; |
| 135 | + }); |
| 136 | + |
| 137 | + await studioSetStyle(contentDeps({ setStyle }), { |
| 138 | + styles: { color: "red", "font-size": "48px", opacity: "0.5" }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(maxInFlight).toBe(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it("reports a partial success as partial, not whole", async () => { |
| 145 | + const setStyle = vi.fn(async (property: string) => |
| 146 | + property === "left" |
| 147 | + ? ({ ok: false, reason: "geometry-property" } as const) |
| 148 | + : ({ ok: true } as const), |
| 149 | + ); |
| 150 | + |
| 151 | + const result = await studioSetStyle(contentDeps({ setStyle }), { |
| 152 | + styles: { color: "red", left: "10px" }, |
| 153 | + }); |
| 154 | + |
| 155 | + const ok = expectOk<StudioSetStyleResult>(result); |
| 156 | + expect(ok.applied).toEqual({ color: "red" }); |
| 157 | + expect(ok.rejected).toEqual({ left: "geometry-property" }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("fails when every property was refused", async () => { |
| 161 | + const result = expectFailure( |
| 162 | + await studioSetStyle( |
| 163 | + contentDeps({ setStyle: async () => ({ ok: false, reason: "styles-not-editable" }) }), |
| 164 | + { styles: { color: "red" } }, |
| 165 | + ), |
| 166 | + ); |
| 167 | + |
| 168 | + expect(result.kind).toBe("blocked"); |
| 169 | + expect(result.reason).toMatch(/styles-not-editable/); |
| 170 | + }); |
| 171 | + |
| 172 | + it("rejects an empty styles object rather than committing nothing", async () => { |
| 173 | + const setStyle = vi.fn(); |
| 174 | + |
| 175 | + const result = expectFailure(await studioSetStyle(contentDeps({ setStyle }), { styles: {} })); |
| 176 | + |
| 177 | + expect(result.kind).toBe("invalid"); |
| 178 | + expect(setStyle).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it("rejects a non-object styles value", async () => { |
| 182 | + for (const styles of ["color: red", 42, null, ["color"]]) { |
| 183 | + const result = expectFailure(await studioSetStyle(contentDeps(), { styles })); |
| 184 | + expect(result.kind).toBe("invalid"); |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + it("refuses to write while a conflict is waiting for the user", async () => { |
| 189 | + const setStyle = vi.fn(); |
| 190 | + |
| 191 | + const result = expectFailure( |
| 192 | + await studioSetStyle( |
| 193 | + contentDeps({ getWriteBlockedReason: () => "Auto-save is paused", setStyle }), |
| 194 | + { styles: { color: "red" } }, |
| 195 | + ), |
| 196 | + ); |
| 197 | + |
| 198 | + expect(result.kind).toBe("blocked"); |
| 199 | + expect(setStyle).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | +}); |
0 commit comments