| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import { Context } from '@deepseek-ai/cordis'
- import { describe, expect, it, vi } from 'vitest'
- import { TestRemote } from '@deepseek-ai/dsh-client-test-runtime'
- import { apply, inject } from '../src/client/index.ts'
- import { SettingsSchemaService } from '../src/client/schema.ts'
- import { SettingsScopeBinder } from '../src/client/settings-scope.ts'
- import { apply as hostApply } from '../src/index.ts'
- function bench() {
- const describeCall = vi.fn((scope?: string) => Promise.resolve({
- ok: true,
- value: { writable: true, hasDocument: true, namespaces: [], ...scope === undefined ? { scopes: [] } : { scope, scopes: [scope] } },
- }))
- const ctx = new Context()
- const remote = new TestRemote(ctx, { settings: { describe: describeCall } })
- return { ctx, describeCall, remote, fiber: ctx.plugin({ inject: [...inject], apply }) }
- }
- describe('settings domain base plugin', () => {
- it('keeps the host Loader entry inert', () => {
- expect(hostApply).not.toThrow()
- })
- it('mounts the scope service under settingsScope and reads once eagerly', async () => {
- const { ctx, describeCall, fiber } = bench()
- await fiber.await()
- expect(ctx.get('settingsScope')).toBeInstanceOf(SettingsScopeBinder)
- expect(ctx.get('settingsSchema')).toBeInstanceOf(SettingsSchemaService)
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
- })
- it('refreshes the mirror on document commits and connection resets, once each', async () => {
- const { ctx, describeCall, remote, fiber } = bench()
- await fiber.await()
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
- remote.emit('settings/document-updated', ['ui-test', 0])
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
- ctx.emit('connection/reset')
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(3) })
- })
- it('routes a scoped commit to the scope\'s mirror and a global commit to every mirror', async () => {
- const { ctx, describeCall, remote, fiber } = bench()
- await fiber.await()
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
- // A scoped commit before any scoped bind touches nothing: no mirror exists for it.
- remote.emit('settings/document-updated', ['ui-test', 0, 'preset/research'])
- await Promise.resolve()
- expect(describeCall).toHaveBeenCalledTimes(1)
- ctx.settingsScope.describe('preset/research')
- remote.emit('settings/document-updated', ['ui-test', 1, 'preset/research'])
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
- expect(describeCall.mock.calls[1]).toEqual(['preset/research'])
- // A global commit reaches the scoped instance too: it inherits the global section.
- remote.emit('settings/document-updated', ['ui-test', 2])
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(4) })
- expect(describeCall.mock.calls.slice(2).map(call => call[0])).toEqual([undefined, 'preset/research'])
- ctx.emit('connection/reset')
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(6) })
- await fiber.dispose()
- })
- it('fiber disposal retires the service and its invalidation subscriptions', async () => {
- const { ctx, describeCall, remote, fiber } = bench()
- await fiber.await()
- await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
- await fiber.dispose()
- expect(ctx.get('settingsScope')).toBeUndefined()
- expect(ctx.get('settingsSchema')).toBeUndefined()
- remote.emit('settings/document-updated', ['ui-test', 0])
- ctx.emit('connection/reset')
- await Promise.resolve()
- expect(describeCall).toHaveBeenCalledTimes(1)
- })
- })
|