refactor(electron): simplify archive import runtime

This commit is contained in:
digua committed 2026-07-23 17:37:50 +08:00
1 parent a8e59174af
commit e02ca569e3
3 files changed
+3 -73

No files matched your search

@@ -1,58 +0,0 @@
import assert from 'node:assert/strict'
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { describe, it } from 'node:test'
import { ArchiveImportSourceManager } from '@openchatlab/node-runtime/import/archive/source-manager'
import { writeZipFixture } from '../../../../packages/node-runtime/src/import/archive/test-utils'
import { importPreparedChatWithSource } from './archive-source-runtime'
function createTakeout(zipPath: string): void {
writeZipFixture(zipPath, [
{
name: 'Takeout/Google Chat/Users/User sample/user_info.json',
content: JSON.stringify({ user: { email: 'owner@example.com', name: 'Owner' } }),
},
{
name: 'Takeout/Google Chat/Groups/DM sample/group_info.json',
content: JSON.stringify({
members: [
{ email: 'owner@example.com', name: 'Owner' },
{ email: 'other@example.com', name: 'Other User' },
],
}),
},
{
name: 'Takeout/Google Chat/Groups/DM sample/messages.json',
content: JSON.stringify({ messages: [] }),
},
])
}
describe('desktop archive import runtime', () => {
it('passes an internal manifest to the importer and preserves the local ZIP', async () => {
const dir = mkdtempSync(join(tmpdir(), 'chatlab-desktop-import-source-'))
try {
const zipPath = join(dir, 'takeout.zip')
createTakeout(zipPath)
const manager = new ArchiveImportSourceManager({ tempRoot: join(dir, 'temp') })
const source = await manager.prepareLocalArchive(zipPath)
const result = await importPreparedChatWithSource(
manager,
source.sourceId,
'Groups/DM sample',
async (manifestPath) => {
assert.equal(JSON.parse(readFileSync(manifestPath, 'utf8')).chatId, 'Groups/DM sample')
return { success: true, sessionId: 'session-1' }
}
)
assert.deepEqual(result, { success: true, sessionId: 'session-1' })
assert.equal(existsSync(zipPath), true)
await manager.close()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})
@@ -9,15 +9,6 @@ export function getArchiveImportSourceManager(): ArchiveImportSourceManager {
return sourceManager
}
export async function importPreparedChatWithSource<T>(
manager: ArchiveImportSourceManager,
sourceId: string,
chatId: string,
importer: (manifestPath: string) => Promise<T>
): Promise<T> {
return manager.withMaterializedChat(sourceId, chatId, importer)
}
export async function cleanupArchiveImportSources(): Promise<void> {
if (!sourceManager) return
const manager = sourceManager
+3 -6
View File
@@ -19,7 +19,7 @@ import {
import type { IpcContext } from './types'
import { CURRENT_SCHEMA_VERSION, getPendingMigrationInfos } from '../database/migrations'
import { t } from '../i18n'
import { getArchiveImportSourceManager, importPreparedChatWithSource } from '../import/archive-source-runtime'
import { getArchiveImportSourceManager } from '../import/archive-source-runtime'
export function registerChatHandlers(ctx: IpcContext): void {
const { win } = ctx
@@ -148,11 +148,8 @@ export function registerChatHandlers(ctx: IpcContext): void {
ipcMain.handle('chat:importPreparedChat', async (_, sourceId: string, chatId: string) => {
try {
const result = await importPreparedChatWithSource(
getArchiveImportSourceManager(),
sourceId,
chatId,
(manifestPath) => worker.autoImport(manifestPath, forwardImportProgress, { formatId: 'google-chat-takeout' })
const result = await getArchiveImportSourceManager().withMaterializedChat(sourceId, chatId, (manifestPath) =>
worker.autoImport(manifestPath, forwardImportProgress, { formatId: 'google-chat-takeout' })
)
return finishAutoImport(result)
} catch (error) {