Files
deepseek-harness/apps/web/tests/client-style-scope.e2e.ts
T
imccyu 897ab671f6 feat(ui-renderer): scope opted-in package styles on existing slot roots (#9033)
* feat(ui-renderer): scope opted-in package styles on existing slot roots

Share the package style policy between slot dispatch and stylesheet installation. Preserve global themes, root identity and component state through style-only toggles, including nested Factory, HOC and list output.

* fix(dev-web): keep launcher validation independent of package sources

* refactor(ui-renderer): identify generated scope CSS as stylesheet content

* fix(client): keep panel indicators visible under style isolation

Position a shared indicator area on the Session panel toggle. Jobs, Subagents, and Forge paint their own marks without crossing package scopes.

* refactor(ui-renderer): fix each package style scope for the page

Stamp the content supplier on existing slot roots. Read its page-owned mode when registering content and writing stylesheets instead of subscribing the render tree to live policy changes.

* fix(ui-renderer): keep scoped styles with their actual package

Resolve Loader aliases against the page module table and stamp contributions from the renderer’s own module source. Let a valid package stylesheet take over a path rejected during scope preflight or by the destination.

* fix(client-modules): declare the row callback receiver-free

* refactor(ui-renderer): scope styles from their module records

* test(ui-renderer): verify module-record style scoping

* chore(ui-renderer): restore scoped slots to the PR base

* fix(ui-renderer): mark slot roots with their supplying module

* refactor(ui-renderer): bind Factory child slots once per supplier

An outlet belongs to whoever called renderSlot for it, wherever it later
renders. A Factory's child-slot methods are now kept per Factory and per
supplier: the Factory itself for its own Component and fallback locals,
the occurrence's caller for a local it selected. One caller mounting its
local in several places shares one method, and no per-mount binding or
extra occurrence context field remains.

* refactor(ui-renderer): scan a Factory's chain children once

Whether a Factory declares a chain child is recorded once per Factory
beside its per-supplier child-slot methods, instead of once per
supplier. The Factory kit assigns `renderSlot` and `renderSlotChain`
by name rather than copying whichever keys the methods object holds.

* test(client-runtime): expect data-plugin on the mounted slot root

The slot outlet names its supplier on the existing root; the test runtime registers without a package, so the two DOM snapshots gain data-plugin="".

* test(web): launch the panel indicator scenario per isolation mode

The Host reads an entry's client isolation when its plugin starts, so
updating that field on a running entry never reached the boot graph and
the scenario waited forever. Each mode now starts its own Host: the
shipped composition, and one whose overlay declares style isolation for
the panel and the two packages drawing on its toggle. Both check the
boot graph first, then run the same interaction and indicator
assertions.
2026-11-25 02:52:49 +08:00

108 lines
5.8 KiB
TypeScript

/** One-way stylesheet isolation across a sidebar HOC, a Factory and its list row. */
import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { chromium, type Page } from 'playwright'
import { expect, it, onTestFinished } from 'vitest'
import { launchWebScaffold, watchConsole } from './scaffold.ts'
import { newEnglishPage } from './support.ts'
const GLOBAL = '@fixture/style-scope-global'
const ADDON = '@fixture/style-scope-addon'
const here = (relative: string) => fileURLToPath(new URL(relative, import.meta.url))
const sidebar = (page: Page) => page.locator('[data-slot="sidebar"]')
const counter = (page: Page) => sidebar(page).locator([
'[data-style-probe="outer"]',
'[data-slot-factory="fixture.styleScope.panel"] > [data-style-probe="middle"]',
'[data-slot="fixture.styleScope.rows"]:not([data-plugin]) > [data-slot-item] > button[data-style-probe="inner"]',
].join(' > '))
async function expectStyles(page: Page, scoped: boolean, margin = '23px') {
// Neither property inherits: every value requires a selector match on the probe itself.
const sample = () => sidebar(page).locator('[data-style-probe]').evaluateAll(probes => probes.map((probe) => {
const root = probe.parentElement!
const { paddingLeft, marginLeft } = getComputedStyle(probe)
return [root.getAttribute('data-plugin'), paddingLeft, marginLeft]
}))
await expect.poll(sample, { timeout: 30_000 }).toEqual([
[GLOBAL, '7px', scoped ? '0px' : margin],
[ADDON, '7px', margin],
[GLOBAL, '7px', scoped ? '0px' : margin],
])
const same = sidebar(page).locator('[data-style-same]')
expect(await same.evaluate(button => [button.parentElement!.dataset.plugin, getComputedStyle(button).scrollMarginRight]))
.toEqual([ADDON, scoped ? '9px' : '0px'])
expect(await page.locator(`style[data-plugin="${ADDON}"][data-path="addon.css"]`).evaluate(sheet => ({
attributes: sheet.getAttributeNames().sort(), scoped: (sheet as HTMLStyleElement).sheet!.cssRules[0] instanceof CSSScopeRule,
}))).toEqual({ attributes: ['data-path', 'data-plugin'], scoped })
}
it('scopes actual Slot suppliers and gives rebuilt modules the current package declaration', async () => {
const fixture = await mkdtemp(join(tmpdir(), 'dsh-style-scope-'))
onTestFinished(() => rm(fixture, { recursive: true, force: true }))
await cp(here('./fixtures/plugins/fixture-style-scope-addon'), fixture, { recursive: true })
const scaffold = await launchWebScaffold({
extraOverlayPath: [here('./pin-browse-picker.overlay.yml'), here('./client-style-scope.overlay.yml')],
extraInstallAnchors: [here('./fixtures/plugins/fixture-style-scope-global/package.json'), join(fixture, 'package.json')],
})
onTestFinished(() => scaffold.close())
const browser = await chromium.launch()
onTestFinished(() => browser.close())
const page = await newEnglishPage(browser)
const fresh = await newEnglishPage(browser)
const consoles = [watchConsole(page), watchConsole(fresh)]
await page.goto(scaffold.authenticatedUrl)
await sidebar(page).locator('[data-style-probe="outer"]').waitFor({ state: 'attached' })
expect(await sidebar(page).getAttribute('data-plugin')).toBe('@deepseek-ai/dsh-client-ui-sidebar')
expect(await counter(page).count()).toBe(0)
let entryId = await scaffold.ctx.loader.create({ name: ADDON, __unstable_enable_client_isolation: 'style' })
await expectStyles(page, true)
const button = counter(page)
const original = await button.evaluateHandle(node => ({ button: node, row: node.parentElement }))
onTestFinished(() => original.dispose())
await button.click()
const replaceHostEntry = async (mode: 'style' | null, pages: Page[]) => {
// Keep the Client package present while the Host composes the replacement entry's options.
const nextId = await scaffold.ctx.loader.create({ name: ADDON, __unstable_enable_client_isolation: mode })
scaffold.ctx.loader.remove(entryId)
entryId = nextId
await expect.poll(() => scaffold.ctx.clientModules.graph().entries.find(row => row.id === ADDON)?.isolation)
.toBe(mode ?? undefined)
const rev = scaffold.ctx.clientModules.graph().rev
for (const open of pages) await open.waitForFunction(revision => Reflect.get(window, '__styleScopeGraph') === revision, rev)
}
await replaceHostEntry(null, [page])
await expectStyles(page, true)
expect(await button.evaluate((node, before) => ({
sameButton: node === before.button, sameRow: node.parentElement === before.row, count: node.textContent,
}), original)).toEqual({ sameButton: true, sameRow: true, count: '1' })
await fresh.goto(scaffold.authenticatedUrl)
await expectStyles(fresh, false)
for (const [open, scoped] of [[page, true], [fresh, false]] as const) {
await open.getByRole('button', { name: 'Load details' }).click()
const sheet = open.locator(`style[data-plugin="${ADDON}"][data-path="details.css"]`)
expect(await sheet.evaluate(node => (node as HTMLStyleElement).sheet!.cssRules[0] instanceof CSSScopeRule)).toBe(scoped)
expect(await sidebar(open).locator('[data-style-probe]').evaluateAll(nodes => nodes.map(node => getComputedStyle(node).outlineOffset)))
.toEqual(scoped ? ['0px', '13px', '0px'] : ['13px', '13px', '13px'])
}
const file = join(fixture, 'client.js')
const source = await readFile(file, 'utf8')
await writeFile(file, source.replace('23px', '31px'))
scaffold.ctx.clientModules.rebuilt(ADDON)
await expectStyles(page, false, '31px')
await expectStyles(fresh, false, '31px')
await replaceHostEntry('style', [page, fresh])
await expectStyles(fresh, false, '31px')
await fresh.reload()
await expectStyles(fresh, true, '31px')
for (const log of consoles) {
expect(log.pageErrors).toEqual([])
expect(log.warnings).toEqual([])
}
})