Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class FakeContents {
return {
ok: true,
value: {
identity: { document_id: 'opened-document', name: 'Opened deck' },
meta: { total_pages: 3 },
document_id: 'opened-document',
deck: { id: 'opened-document', title: 'Opened deck', total_pages: 3 },
reused: false,
},
}
Expand Down Expand Up @@ -93,6 +93,8 @@ it('owns exactly one CDP target per Session and presents only the active one', a
const second = await manager.ensureSession('session-b')
expect(first.targetId).toBe('ppt-target-1')
expect(second.targetId).toBe('ppt-target-2')
expect(first.documentCount).toBeNull()
expect(second.documentCount).toBeNull()
expect(views).toHaveLength(2)

manager.setBounds({ x: 12, y: 24, width: 900, height: 600 })
Expand All @@ -117,7 +119,7 @@ it('owns exactly one CDP target per Session and presents only the active one', a
slideCount: 3,
title: 'Opened deck',
})
expect(views[0]!.webContents.executedScripts.at(-1)).toContain('"method":"view_ppt"')
expect(views[0]!.webContents.executedScripts.at(-1)).toContain('"method":"open"')
expect(views[0]!.webContents.executedScripts.at(-1)).toContain('"content_base64":"BAUG"')
await expect(manager.openFile('session-a', path.join(presentationDirectory, 'deck.txt')))
.rejects.toThrow('must end with .pptx')
Expand All @@ -130,6 +132,41 @@ it('owns exactly one CDP target per Session and presents only the active one', a
expect(manager.snapshot().sessions.map((item) => item.sessionId)).toEqual(['session-a'])
})

it('publishes authoritative project counts without treating the native target as a document', async () => {
const views: FakeView[] = []
const snapshots: Array<ReturnType<EmbeddedPowerPointManager['snapshot']>> = []
const manager = new EmbeddedPowerPointManager(
() => {
const view = new FakeView(views.length + 1)
views.push(view)
return view as unknown as WebContentsView
},
async () => {},
(snapshot) => snapshots.push(snapshot),
)
manager.attachHost({
isDestroyed: () => false,
contentView: { addChildView: () => {}, removeChildView: () => {} },
} as unknown as BrowserWindow)

const session = await manager.ensureSession('session-a')
expect(session.documentCount).toBeNull()
manager.reportState(session.webContentsId, { documentCount: 0 })
expect(manager.sessionInfo('session-a')?.documentCount).toBe(0)
manager.reportState(session.webContentsId, { documentCount: 2 })
expect(manager.snapshot().sessions[0]?.documentCount).toBe(2)
const publishCount = snapshots.length
manager.reportState(session.webContentsId, { documentCount: 2 })
expect(snapshots).toHaveLength(publishCount)

views[0]!.webContents.emit('did-start-loading')
expect(manager.sessionInfo('session-a')?.documentCount).toBeNull()
expect(() => manager.reportState(session.webContentsId, { documentCount: -1 }))
.toThrow('Invalid PowerPoint runtime document count')
expect(() => manager.reportState(999, { documentCount: 1 })).toThrow('does not own')
manager.closeAll()
})

it('rejects unknown close senders and preserves a replacement target during deferred close', async () => {
const views: FakeView[] = []
const manager = new EmbeddedPowerPointManager(
Expand All @@ -147,6 +184,8 @@ it('rejects unknown close senders and preserves a replacement target during defe
const first = await manager.ensureSession('session-a')
const other = await manager.ensureSession('session-b')
expect(manager.sessionForContents(first.webContentsId)).toBe('session-a')
expect(manager.ownedSessionForContents(first.webContentsId)).toBe('session-a')
expect(manager.ownedSessionForContents(99)).toBeNull()
expect(() => manager.sessionForContents(99)).toThrow('does not own')
manager.closeSession('session-a')
const replacement = await manager.ensureSession('session-a')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ const { IPC } = await import('../../shared/ipc-channels')
const { registerExcelHostHandlers } = await import('../handlers/excel-host')

describe('Excel host close requests', () => {
it('binds renderer state reports to the sending Excel target', async () => {
const calls: unknown[][] = []
const excel = {
reportState: (sender: number, state: unknown) => { calls.push([sender, state]) },
} as unknown as ExcelHost
testIpcHandlers.clear()
registerExcelHostHandlers(excel, {} as EmbeddedBrowserManager, () => undefined)

await testIpcHandlers.get(IPC.excelHost.reportState)?.(
{ sender: { id: 17 } } as IpcMainInvokeEvent,
{ documentCount: 0, dirty: false },
)

expect(calls).toEqual([[17, { documentCount: 0, dirty: false }]])
})

it('resolves the owning Session from the sender and retracts its panel before deferred destruction', async () => {
const events: unknown[][] = []
const closed: number[] = []
Expand Down
36 changes: 33 additions & 3 deletions desktop/apps/electron/src/main/__tests__/excel-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ function setup(openExternal?: (url: string) => void) {
}

describe('ExcelHost Session target ownership', () => {
it('publishes the renderer-owned workbook inventory only for its Session target', async () => {
const snapshots: Array<ReturnType<ExcelHost['snapshot']>> = []
const views: FakeView[] = []
const host = fakeHost()
const manager = new ExcelHost(
() => {
const view = new FakeView(new FakeWebContents(views.length + 1, `excel-target-${views.length + 1}`))
views.push(view)
return view as unknown as WebContentsView
},
'/dist/excel-host-preload.cjs',
'http://localhost:5173',
'/dist/renderer/excel.html',
(snapshot) => snapshots.push(snapshot),
)
manager.attachHost(host.window)
const session = await manager.ensureSession('session-a', {
sessionId: 'session-a', locale: 'en-US', theme: 'light',
}, false)

expect(session.documentCount).toBeNull()
manager.reportState(session.webContentsId, { documentCount: 0, dirty: false })
expect(manager.snapshot().sessions[0]?.documentCount).toBe(0)
manager.reportState(session.webContentsId, { documentCount: 2, dirty: true })
expect(manager.snapshot().sessions[0]).toMatchObject({ documentCount: 2, dirty: true })
expect(snapshots.at(-1)?.sessions[0]).toMatchObject({ documentCount: 2, dirty: true })
expect(() => manager.reportState(session.webContentsId, { documentCount: -1, dirty: false })).toThrow('Invalid Excel runtime')
expect(() => manager.reportState(-1, { documentCount: 0, dirty: false })).toThrow('does not own')
})

it('keeps serialized recovery scoped to the owning Session renderer', async () => {
const { manager } = setup()
const first = await manager.ensureSession('session-a', { sessionId: 'session-a', locale: 'en-US', theme: 'light' })
Expand Down Expand Up @@ -227,7 +257,7 @@ describe('ExcelHost Session target ownership', () => {
const config = { sessionId: 'session-a', locale: 'en-US' as const, theme: 'light' as const }
const first = await manager.ensureSession('session-a', config)
await manager.ensureSession('session-b', { ...config, sessionId: 'session-b' })
manager.setDirty(first.webContentsId, true)
manager.reportState(first.webContentsId, { documentCount: 1, dirty: true })
manager.setRecoveryState(first.webContentsId, '{"version":1}')
manager.activateSession('session-b')
manager.setVisible(true)
Expand All @@ -251,7 +281,7 @@ describe('ExcelHost Session target ownership', () => {
})
const checkpoint = JSON.stringify({ version: 1, tabs: [{ tabId: 'tab-a' }] })
manager.setRecoveryState(first.webContentsId, checkpoint)
manager.setDirty(first.webContentsId, true)
manager.reportState(first.webContentsId, { documentCount: 1, dirty: true })

views[0]?.webContents.emit('render-process-gone', {}, { reason: 'crashed' })
const recovered = await manager.ensureSession('session-a', {
Expand All @@ -270,7 +300,7 @@ describe('ExcelHost Session target ownership', () => {
const { manager, views, host } = setup()
for (const sessionId of ['session-a', 'session-b']) {
const session = await manager.ensureSession(sessionId, { sessionId, locale: 'en-US', theme: 'light' })
manager.setDirty(session.webContentsId, true)
manager.reportState(session.webContentsId, { documentCount: 1, dirty: true })
}
manager.shutdown()
expect(manager.snapshot().sessions).toEqual([])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { beforeEach, describe, expect, it, mock } from 'bun:test'
import type { IpcMainInvokeEvent } from 'electron'
import type { MountReferenceProvider } from '../handlers/mount-references'
import { electronModuleMock, loggerModuleMock, testIpcHandlers } from './electron-module-mock'

mock.module('electron', () => electronModuleMock)
mock.module('../logger', () => loggerModuleMock)

const { IPC } = await import('../../shared/ipc-channels')
const { registerMountReferenceHandlers } = await import('../handlers/mount-references')

const compatibleProvider = (usage: { assetCount: number; elementCount: number; projectCount: number }): MountReferenceProvider => ({
usage: async () => usage,
validateReplacement: async () => ({ compatible: true }),
remove: async () => usage,
refresh: async () => undefined,
})

describe('mount reference IPC boundary', () => {
beforeEach(() => testIpcHandlers.clear())

it('aggregates reference usage without exposing editor-specific APIs to Files', async () => {
registerMountReferenceHandlers([
compatibleProvider({ assetCount: 2, elementCount: 3, projectCount: 1 }),
compatibleProvider({ assetCount: 1, elementCount: 4, projectCount: 1 }),
])
const usage = await testIpcHandlers.get(IPC.mountReferences.usage)?.({} as IpcMainInvokeEvent, 'session-a', 'mount-a')
expect(usage).toEqual({ assetCount: 3, elementCount: 7, projectCount: 2 })
})

it('rejects relocation when any registered project type finds an incompatible reference', async () => {
const rejecting: MountReferenceProvider = {
...compatibleProvider({ assetCount: 0, elementCount: 0, projectCount: 0 }),
validateReplacement: async () => ({ compatible: false, reason: 'content-mismatch', assetName: 'chart.png' }),
}
registerMountReferenceHandlers([
compatibleProvider({ assetCount: 0, elementCount: 0, projectCount: 0 }),
rejecting,
])
const result = await testIpcHandlers.get(IPC.mountReferences.rebind)?.({} as IpcMainInvokeEvent, 'session-a', 'mount-a', '/tmp/chart.png')
expect(result).toEqual({
ok: false,
validation: { compatible: false, reason: 'content-mismatch', assetName: 'chart.png' },
})
})
})
6 changes: 2 additions & 4 deletions desktop/apps/electron/src/main/__tests__/office-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ describe('Office source files and durable recovery', () => {
expect(await readFile(path, 'utf8')).toBe('external')
})
}
it('restores independent Session drafts through a fresh store instance', async () => {
it('restores independent Excel Session drafts through a fresh store instance', async () => {
const directory = await root()
const first = createOfficeRecoveryStore(directory)
await Promise.all([first.write('excel', 'session-a', '{"draft":1}'), first.write('excel', 'session-b', '{"draft":2}'), first.write('presentation', 'session-a', '{"draft":3}')])
await Promise.all([first.write('excel', 'session-a', '{"draft":1}'), first.write('excel', 'session-b', '{"draft":2}')])
const next = createOfficeRecoveryStore(directory)
expect(await next.read('excel', 'session-a')).toBe('{"draft":1}')
expect(await next.read('excel', 'session-b')).toBe('{"draft":2}')
expect(await next.read('presentation', 'session-a')).toBe('{"draft":3}')
expect(await next.read('word', 'session-a')).toBeNull()
})
})
Loading
Loading