72 lines
4.2 KiB
TypeScript
72 lines
4.2 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { createServer } from 'vite'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import type { MessageEnvelope } from '../src/shared/types/message.ts'
|
|
import type { useAccountsStore } from '../src/renderer/src/stores/accounts.store.ts'
|
|
import type { useFoldersStore } from '../src/renderer/src/stores/folders.store.ts'
|
|
import type { useMessagesStore } from '../src/renderer/src/stores/messages.store.ts'
|
|
|
|
test('открывает письмо после смены аккаунта и загрузки папки, даже вне первой страницы', async (): Promise<void> => {
|
|
const server = await createServer({
|
|
configFile: false,
|
|
resolve: { alias: { '@shared': fileURLToPath(new URL('../src/shared', import.meta.url)) } },
|
|
server: { middlewareMode: true, hmr: false, ws: false, watch: null }
|
|
})
|
|
const originalWindow: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor(globalThis, 'window')
|
|
const calls: string[] = []
|
|
const envelope: MessageEnvelope = {
|
|
id: '99', folderId: 'Work', subject: 'Важное', date: '2026-11-17',
|
|
from: { name: 'Автор', email: 'sender@example.org' }, to: [], cc: [],
|
|
flags: { seen: false, flagged: false, answered: false, deleted: false },
|
|
hasAttachments: false, snippet: ''
|
|
}
|
|
Object.defineProperty(globalThis, 'window', { configurable: true, value: {
|
|
api: {
|
|
on: (): (() => void) => (): void => {},
|
|
invoke: async (channel: string, payload: { accountId: string; folderId: string; messageId: string }): Promise<unknown> => {
|
|
calls.push(channel)
|
|
if (channel === 'folders:list') return { folders: [
|
|
{ id: 'INBOX', name: 'Входящие', role: 'inbox', parentId: null, unreadCount: 0, totalCount: 0, children: [] },
|
|
{ id: 'Work', name: 'Работа', role: 'none', parentId: null, unreadCount: 1, totalCount: 100, children: [] }
|
|
] }
|
|
if (channel === 'messages:list') return { messages: [], total: 100, unseen: 1 }
|
|
if (channel === 'messages:body') {
|
|
assert.equal(payload.accountId, 'account-b')
|
|
assert.equal(payload.folderId, 'Work')
|
|
assert.equal(payload.messageId, '99')
|
|
return { id: '99', text: 'Текст письма', html: null, references: null, attachments: [], inlineImages: [] }
|
|
}
|
|
if (channel === 'messages:setFlags') return undefined
|
|
throw new Error(`Неожиданный канал ${channel}`)
|
|
}
|
|
}
|
|
} })
|
|
try {
|
|
setActivePinia(createPinia())
|
|
const accountModule = await server.ssrLoadModule('/src/renderer/src/stores/accounts.store.ts') as { useAccountsStore: typeof useAccountsStore }
|
|
const folderModule = await server.ssrLoadModule('/src/renderer/src/stores/folders.store.ts') as { useFoldersStore: typeof useFoldersStore }
|
|
const messageModule = await server.ssrLoadModule('/src/renderer/src/stores/messages.store.ts') as { useMessagesStore: typeof useMessagesStore }
|
|
const accounts = accountModule.useAccountsStore()
|
|
const folders = folderModule.useFoldersStore()
|
|
const messages = messageModule.useMessagesStore()
|
|
accounts.setActive('account-a')
|
|
await folders.ensureFoldersLoaded('account-a')
|
|
folders.setActiveFolder('Work')
|
|
await messages.openMessage('account-b', envelope)
|
|
await new Promise<void>((resolve): void => { setTimeout(resolve, 200) })
|
|
assert.equal(accounts.activeAccountId, 'account-b')
|
|
assert.equal(folders.activeFolderId, 'Work')
|
|
assert.equal(messages.selectedMessageId, '99')
|
|
assert.equal(messages.messageBody?.text, 'Текст письма')
|
|
assert.equal(messages.messages.find((message: MessageEnvelope): boolean => message.id === '99')?.flags.seen, true)
|
|
assert.ok(calls.indexOf('folders:list') < calls.indexOf('messages:list'))
|
|
assert.equal(calls.filter((channel: string): boolean => channel === 'messages:list').length, 1)
|
|
assert.equal(calls.filter((channel: string): boolean => channel === 'messages:body').length, 1)
|
|
} finally {
|
|
await server.close()
|
|
if (originalWindow) Object.defineProperty(globalThis, 'window', originalWindow)
|
|
else Reflect.deleteProperty(globalThis, 'window')
|
|
}
|
|
})
|