import assert from 'node:assert/strict'; import test from 'node:test'; import { adoptStyles } from '../src/styles.ts'; import { createCorePluginApi, type PluginApiSite } from '../src/pluginApi.ts'; /** A constructable sheet as far as the helper uses one. */ class FakeSheet { static refuse = false; css = ''; replaceSync(css: string) { if (FakeSheet.refuse) throw new Error('refused'); this.css = css; } } type FakeStyle = { tag: string; textContent: string; parent: FakeParent | null; sheet?: unknown; remove(): void }; type FakeParent = { children: FakeStyle[]; appendChild(node: FakeStyle): FakeStyle }; function parent(): FakeParent { const node: FakeParent = { children: [], appendChild(child) { child.parent = node; node.children.push(child); return child; }, }; return node; } function fakeDocument(options: { constructable?: boolean; adopted?: boolean } = {}) { const win: Record = {}; if (options.constructable !== false) win.CSSStyleSheet = FakeSheet; const head = parent(); const doc: Record = { nodeType: 9, defaultView: win, head, documentElement: parent(), createElement(tag: string): FakeStyle { const node: FakeStyle = { tag, textContent: '', parent: null, remove() { if (node.parent) node.parent.children = node.parent.children.filter(c => c !== node); node.parent = null; } }; return node; }, }; if (options.adopted !== false) doc.adoptedStyleSheets = []; win.document = doc; return { win, doc, head }; } function fakeShadowRoot(doc: Record, adopted = true) { const root = parent() as FakeParent & Record; root.nodeType = 11; root.ownerDocument = doc; if (adopted) root.adoptedStyleSheets = []; return root; } test('a shadow root adopts a constructed sheet: no