// @vitest-environment jsdom import { mkdtempSync, rmSync } from 'node:fs' import { createRequire } from 'node:module' import { tmpdir } from 'node:os' import { join } from 'node:path' import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import { loadMemoryClientArtifact, MemoryCompositionRunner } from 'dsh-brain-mnemon/testing' import * as runtime from 'dsh-brain-mnemon-source-runtime' import runtimeLocales from 'dsh-brain-mnemon-source-runtime/presentation/locales.json' with { type: 'json' } import * as focus from '../lib/external-strategy.js' const runtimeEn = (key: keyof typeof runtimeLocales.en): string => runtimeLocales.en[key] const require = createRequire(import.meta.url) const dependencies = { react: await vi.importActual('react'), 'react/jsx-runtime': await vi.importActual('react/jsx-runtime'), 'react-dom': await vi.importActual('react-dom'), '@deepseek-ai/dsh-client-ui-primitives': await vi.importActual('@deepseek-ai/dsh-client-ui-primitives'), } const core = loadMemoryClientArtifact(require.resolve('dsh-brain-mnemon/client'), dependencies) const sourceDependencies = { ...dependencies, 'dsh-brain-mnemon/client': core } const runtimeClient = loadMemoryClientArtifact(require.resolve('dsh-brain-mnemon-source-runtime/client'), sourceDependencies) const documentsClient = loadMemoryClientArtifact(require.resolve('dsh-brain-mnemon-source-documents/client'), sourceDependencies) const spacesClient = loadMemoryClientArtifact(require.resolve('dsh-brain-mnemon-source-memory-spaces/client'), sourceDependencies) afterEach(cleanup) describe('compiled Source clients with compiled Core and no source aliases', () => { it('loads Source copy and styles even when the shared kit has stale business presentation', async () => { const directory = mkdtempSync(join(tmpdir(), 'source-presentation-')) const runner = new MemoryCompositionRunner() const sharedCopy = () => 'stale shared copy' const oldKit = { ...core, useT: () => sharedCopy, memoryPageStyles: Object.fromEntries(Object.entries(core.memoryPageStyles).filter(([key]) => !key.startsWith('runtime'))), memorySidebarStyles: Object.fromEntries(Object.entries(core.memorySidebarStyles).filter(([key]) => !key.startsWith('runtime'))), } const owned = loadMemoryClientArtifact(require.resolve('dsh-brain-mnemon-source-runtime/client'), { ...sourceDependencies, 'dsh-brain-mnemon/client': oldKit, }) try { await runner.mount(focus, { instanceId: 'focus', config: { sourceKeys: ['source:work'], mode: 'eager' } }) await runner.mount(runtime, { instanceId: 'work', config: { dataDir: directory } }) const management = await runner.managementClient('source:work') const view = render() expect(await screen.findByRole('heading', { name: runtimeEn('runtime.title') })).not.toBeNull() expect(view.container.querySelector('[class*="runtimeSummaryCard"]')).not.toBeNull() const style = document.querySelector('style[data-plugin-css="dsh-brain-mnemon-source-runtime/presentation/page.module.css"]') expect(style?.textContent).toContain('runtimeSummaryCard') } finally { cleanup(); await runner.dispose(); rmSync(directory, { recursive: true, force: true }) } }) it('registers all Source-owned pages and disposes them through the shared public UI contract', () => { const pages = new Map() const slots = { inject: (_name: string, factory: () => () => void) => factory(), register: (options: { id: string }, component: unknown) => { pages.set(options.id, component); return () => pages.delete(options.id) }, } const releases = [ runtimeClient.installRuntimeMemoryUI({ slots } as never), documentsClient.installDocumentsMemoryUI({ slots } as never), spacesClient.installMemorySpacesUI({ slots } as never), ] expect(pages.get('runtime/entries')).toEqual(expect.any(Function)) expect([...pages.keys()].some(key => key.startsWith('documents/'))).toBe(true) expect([...pages.keys()].some(key => key.startsWith('memory-spaces/'))).toBe(true) for (const release of releases.reverse()) release() expect(pages.size).toBe(0) }) it('clicks a compiled Runtime page backed by a packed Source and keeps instances isolated', async () => { const directory = mkdtempSync(join(tmpdir(), 'compiled-runtime-client-')) const runner = new MemoryCompositionRunner() try { await runner.mount(focus, { instanceId: 'focus', config: { sourceKeys: ['source:work', 'source:personal'], mode: 'eager' } }) for (const id of ['work', 'personal']) await runner.mount(runtime, { instanceId: id, config: { dataDir: join(directory, id) } }) const work = await runner.managementClient('source:work') const personal = await runner.managementClient('source:personal') const pageClient = core.createMemorySourcePageClient(work) expect(await pageClient.read('snapshot', null, { coalesce: true })).toMatchObject({ entries: [] }) const base = { sourceTypeId: 'runtime', sourceInstances: [], locale: 'en', writable: true } const Page = runtimeClient.RuntimeSourcePage const view = render() fireEvent.click(await screen.findByRole('button', { name: runtimeEn('runtime.addButton') })) const textarea = await screen.findByRole('textbox', { name: runtimeEn('runtime.content') }) fireEvent.change(textarea, { target: { value: 'compiled UI sentinel' } }) fireEvent.click(screen.getByRole('button', { name: runtimeEn('runtime.addAction') })) expect(await screen.findByText('compiled UI sentinel')).not.toBeNull() expect((await work.read('snapshot')).value).toMatchObject({ entries: [expect.objectContaining({ content: 'compiled UI sentinel' })] }) view.rerender() await waitFor(() => expect(screen.queryByText('compiled UI sentinel')).toBeNull()) expect((await personal.read('snapshot')).value).toMatchObject({ entries: [] }) } finally { cleanup(); await runner.dispose(); rmSync(directory, { recursive: true, force: true }) } }) })