mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-26 02:04:37 +08:00
fix(sync): delete imported session files completely
This commit is contained in:
1 parent
848a44310c
commit
82a6be4fcb
7 files changed
+124
-13
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import test from 'node:test'
|
||||
import Fastify from 'fastify'
|
||||
import { DataSourceManager } from '@openchatlab/sync'
|
||||
import { registerAutomationRoutes } from './routes'
|
||||
|
||||
function makeTempDir(): string {
|
||||
const baseDir = fs.existsSync('/private/tmp') ? '/private/tmp' : os.tmpdir()
|
||||
return fs.mkdtempSync(path.join(baseDir, 'chatlab-sync-routes-'))
|
||||
}
|
||||
|
||||
test('DELETE import session deletes imported local session when deleteData=true', async () => {
|
||||
const settingsDir = makeTempDir()
|
||||
const dsManager = new DataSourceManager(settingsDir)
|
||||
const ds = dsManager.add({
|
||||
baseUrl: 'http://example.com',
|
||||
token: 'token',
|
||||
intervalMinutes: 60,
|
||||
})
|
||||
const [session] = dsManager.addSessions(ds.id, [{ name: 'First chat', remoteSessionId: 'remote-1' }])
|
||||
dsManager.updateSession(ds.id, session.id, { targetSessionId: 'local-1' })
|
||||
|
||||
const deletedSessionIds: string[] = []
|
||||
const app = Fastify()
|
||||
registerAutomationRoutes(app, {
|
||||
dsManager,
|
||||
pullEngine: {
|
||||
triggerPull: async () => ({ success: true, newMessageCount: 0 }),
|
||||
triggerPullAll: async () => ({ success: true, newMessageCount: 0 }),
|
||||
getProgress: () => [],
|
||||
} as any,
|
||||
dbManager: {
|
||||
deleteSessionDatabaseFiles: (sessionId: string) => {
|
||||
deletedSessionIds.push(sessionId)
|
||||
},
|
||||
} as any,
|
||||
serverInfo: { port: 5200, host: '127.0.0.1', token: 'api-token' },
|
||||
})
|
||||
await app.ready()
|
||||
|
||||
const resp = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/_web/automation/data-sources/${ds.id}/sessions/${session.id}?deleteData=true`,
|
||||
})
|
||||
|
||||
await app.close()
|
||||
|
||||
assert.equal(resp.statusCode, 200)
|
||||
assert.deepEqual(deletedSessionIds, ['local-1'])
|
||||
assert.equal(dsManager.get(ds.id)?.sessions.length, 0)
|
||||
})
|
||||
@@ -91,10 +91,7 @@ export function registerAutomationRoutes(server: FastifyInstance, ctx: SyncRoute
|
||||
if (!removed) return reply.code(404).send({ error: 'Session not found' })
|
||||
reloadTimer(request.params.id)
|
||||
if (request.query.deleteData === 'true' && removed.targetSessionId) {
|
||||
dbManager.close(removed.targetSessionId)
|
||||
const dbPath = dbManager.getDbPath(removed.targetSessionId)
|
||||
const fs = await import('node:fs')
|
||||
if (fs.existsSync(dbPath)) fs.unlinkSync(dbPath)
|
||||
dbManager.deleteSessionDatabaseFiles(removed.targetSessionId)
|
||||
}
|
||||
return { success: true }
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user