mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-26 02:04:37 +08:00
fix(migration): make Electron path test cross-platform
Resolve Electron userData fixtures for macOS, Windows, and Linux so the migration regression test exercises the path selected by the current runner.
This commit is contained in:
1 parent
7d156040f8
commit
0227a8cc6b
2 files changed
+39
-13
No files matched your search
@@ -7,23 +7,20 @@ import { mock, test } from 'node:test'
|
||||
test('Electron migration moves persistent system data but leaves legacy temp data untouched', async (t) => {
|
||||
const root = fs.mkdtempSync(path.join(process.env.CHATLAB_TEST_TMPDIR ?? os.tmpdir(), 'chatlab-electron-migration-'))
|
||||
const originalHome = process.env.HOME
|
||||
const electronUserData = path.join(root, 'Library', 'Application Support', 'ChatLab')
|
||||
const electronDataDir = path.join(electronUserData, 'data')
|
||||
const originalAppData = process.env.APPDATA
|
||||
const appDataDir = path.join(root, 'AppData', 'Roaming')
|
||||
const systemDir = path.join(root, 'system')
|
||||
const configWrites: Array<{ section: string; key: string; value: unknown }> = []
|
||||
t.after(() => {
|
||||
if (originalHome === undefined) delete process.env.HOME
|
||||
else process.env.HOME = originalHome
|
||||
if (originalAppData === undefined) delete process.env.APPDATA
|
||||
else process.env.APPDATA = originalAppData
|
||||
fs.rmSync(root, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
process.env.HOME = root
|
||||
fs.mkdirSync(path.join(electronDataDir, 'databases'), { recursive: true })
|
||||
fs.mkdirSync(path.join(electronDataDir, 'ai'), { recursive: true })
|
||||
fs.mkdirSync(path.join(electronDataDir, 'temp'), { recursive: true })
|
||||
fs.writeFileSync(path.join(electronDataDir, 'databases', 'session.db'), 'sqlite')
|
||||
fs.writeFileSync(path.join(electronDataDir, 'ai', 'settings.json'), '{}')
|
||||
fs.writeFileSync(path.join(electronDataDir, 'temp', 'stale.tmp'), 'temporary')
|
||||
process.env.APPDATA = appDataDir
|
||||
|
||||
await mock.module('@openchatlab/config', {
|
||||
namedExports: {
|
||||
@@ -33,7 +30,30 @@ test('Electron migration moves persistent system data but leaves legacy temp dat
|
||||
},
|
||||
})
|
||||
|
||||
const { migrateFromElectronIfNeeded } = await import('./electron-data-migration.js')
|
||||
const { getElectronUserDataDir, migrateFromElectronIfNeeded } = await import('./electron-data-migration.js')
|
||||
assert.equal(
|
||||
getElectronUserDataDir({ platform: 'darwin', homeDir: root }),
|
||||
path.join(root, 'Library', 'Application Support', 'ChatLab')
|
||||
)
|
||||
assert.equal(
|
||||
getElectronUserDataDir({ platform: 'win32', homeDir: root, appDataDir }),
|
||||
path.join(appDataDir, 'ChatLab')
|
||||
)
|
||||
assert.equal(getElectronUserDataDir({ platform: 'linux', homeDir: root }), path.join(root, '.config', 'ChatLab'))
|
||||
|
||||
const electronUserData = getElectronUserDataDir({
|
||||
platform: process.platform,
|
||||
homeDir: root,
|
||||
appDataDir,
|
||||
})
|
||||
const electronDataDir = path.join(electronUserData, 'data')
|
||||
fs.mkdirSync(path.join(electronDataDir, 'databases'), { recursive: true })
|
||||
fs.mkdirSync(path.join(electronDataDir, 'ai'), { recursive: true })
|
||||
fs.mkdirSync(path.join(electronDataDir, 'temp'), { recursive: true })
|
||||
fs.writeFileSync(path.join(electronDataDir, 'databases', 'session.db'), 'sqlite')
|
||||
fs.writeFileSync(path.join(electronDataDir, 'ai', 'settings.json'), '{}')
|
||||
fs.writeFileSync(path.join(electronDataDir, 'temp', 'stale.tmp'), 'temporary')
|
||||
|
||||
const result = migrateFromElectronIfNeeded(systemDir)
|
||||
|
||||
assert.equal(result.migrated, true)
|
||||
|
||||
@@ -30,13 +30,19 @@ interface StorageConfig {
|
||||
* - Windows: %APPDATA%/ChatLab/
|
||||
* - Linux: ~/.config/ChatLab/
|
||||
*/
|
||||
function getElectronUserDataDir(): string {
|
||||
const home = os.homedir()
|
||||
switch (process.platform) {
|
||||
export function getElectronUserDataDir(
|
||||
options: {
|
||||
platform?: NodeJS.Platform
|
||||
homeDir?: string
|
||||
appDataDir?: string
|
||||
} = {}
|
||||
): string {
|
||||
const home = options.homeDir ?? os.homedir()
|
||||
switch (options.platform ?? process.platform) {
|
||||
case 'darwin':
|
||||
return path.join(home, 'Library', 'Application Support', 'ChatLab')
|
||||
case 'win32': {
|
||||
const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming')
|
||||
const appData = options.appDataDir ?? process.env.APPDATA ?? path.join(home, 'AppData', 'Roaming')
|
||||
return path.join(appData, 'ChatLab')
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user