claude-instance-manager 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -3
- package/cli.js +286 -0
- package/index.html +512 -0
- package/instances.js +102 -0
- package/main.js +232 -0
- package/package.json +15 -4
- package/preload.js +7 -0
- package/test/cli-terminology.test.js +28 -0
- package/test/instances.test.js +228 -0
- package/test/renderer.test.js +336 -0
- package/test/shortcut.test.js +127 -0
- package/test/usage.test.js +106 -0
- package/test/wipe.test.js +173 -0
- package/usage-cache.js +109 -0
- package/usage.js +46 -0
- package/wipe.js +128 -0
package/main.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
|
|
2
|
+
const { spawn, execSync, execFile } = require('child_process');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const { listInstances, getRunningInstances } = require('./instances');
|
|
6
|
+
const { readUsageHistory } = require('./usage');
|
|
7
|
+
|
|
8
|
+
// Keep existing launcher data and sessions when changing the displayed name.
|
|
9
|
+
app.setName('claude-desktop-manager');
|
|
10
|
+
const userData = app.getPath('userData');
|
|
11
|
+
const sessionData = app.getPath('sessionData');
|
|
12
|
+
app.setName('Claude Instance Manager');
|
|
13
|
+
app.setPath('userData', userData);
|
|
14
|
+
app.setPath('sessionData', sessionData);
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
function getClaudePath() {
|
|
18
|
+
const localAppData = process.env.LOCALAPPDATA || '';
|
|
19
|
+
|
|
20
|
+
// 1. Check legacy installer path
|
|
21
|
+
const legacyPath = path.join(localAppData, 'AnthropicClaude', 'claude.exe');
|
|
22
|
+
if (fs.existsSync(legacyPath)) return legacyPath;
|
|
23
|
+
|
|
24
|
+
// 2. Check standard Windows AppX / MSIX package installation paths
|
|
25
|
+
const packagesDir = path.join(localAppData, 'Packages');
|
|
26
|
+
if (fs.existsSync(packagesDir)) {
|
|
27
|
+
try {
|
|
28
|
+
const packageFolders = fs.readdirSync(packagesDir);
|
|
29
|
+
const claudePkg = packageFolders.find(f => f.startsWith('Claude_') || f.includes('Anthropic'));
|
|
30
|
+
if (claudePkg) {
|
|
31
|
+
// Search inside the package directory for the executable
|
|
32
|
+
const pkgAppPath = path.join(packagesDir, claudePkg, 'app', 'claude.exe');
|
|
33
|
+
if (fs.existsSync(pkgAppPath)) return pkgAppPath;
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {
|
|
36
|
+
// Ignore directory read errors
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 3. Fallback: Query Windows Registry / PowerShell for installed AppX package location
|
|
41
|
+
try {
|
|
42
|
+
const psCommand = 'powershell -Command "(Get-AppxPackage *Claude*).InstallLocation"';
|
|
43
|
+
const installLoc = execSync(psCommand, { encoding: 'utf8' }).trim();
|
|
44
|
+
if (installLoc) {
|
|
45
|
+
const exeInPkg = path.join(installLoc, 'Claude.exe');
|
|
46
|
+
if (fs.existsSync(exeInPkg)) return exeInPkg;
|
|
47
|
+
|
|
48
|
+
const exeInAppFolder = path.join(installLoc, 'app', 'claude.exe');
|
|
49
|
+
if (fs.existsSync(exeInAppFolder)) return exeInAppFolder;
|
|
50
|
+
}
|
|
51
|
+
} catch (e) {
|
|
52
|
+
// Powershell lookup failed
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Fallback default
|
|
56
|
+
return legacyPath;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let cachedClaudePath;
|
|
60
|
+
function findClaudePath() {
|
|
61
|
+
if (!cachedClaudePath || !fs.existsSync(cachedClaudePath)) cachedClaudePath = getClaudePath();
|
|
62
|
+
return cachedClaudePath;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let defaultUsageDirectory;
|
|
66
|
+
async function getDefaultUsageDirectory(claudePath) {
|
|
67
|
+
const legacyPath = path.join(process.env.LOCALAPPDATA || '', 'AnthropicClaude', 'claude.exe');
|
|
68
|
+
if (path.normalize(claudePath).toLowerCase() === path.normalize(legacyPath).toLowerCase()) {
|
|
69
|
+
return path.join(process.env.APPDATA, 'Claude');
|
|
70
|
+
}
|
|
71
|
+
if (!defaultUsageDirectory) {
|
|
72
|
+
defaultUsageDirectory = new Promise((resolve, reject) => {
|
|
73
|
+
execFile('powershell.exe', [
|
|
74
|
+
'-NoProfile', '-NonInteractive', '-Command',
|
|
75
|
+
"(Get-AppxPackage -Name 'AnthropicPBC.Claude').PackageFamilyName"
|
|
76
|
+
], { encoding: 'utf8', timeout: 10000, windowsHide: true }, (error, stdout) => {
|
|
77
|
+
if (error) reject(error);
|
|
78
|
+
else resolve(stdout);
|
|
79
|
+
});
|
|
80
|
+
}).then(stdout => {
|
|
81
|
+
const families = stdout.trim().split(/\r?\n/).filter(Boolean);
|
|
82
|
+
if (families.length !== 1 || !/^[a-z0-9._-]+$/i.test(families[0])) {
|
|
83
|
+
throw new Error('Could not identify the Claude package data directory.');
|
|
84
|
+
}
|
|
85
|
+
return path.join(process.env.LOCALAPPDATA, 'Claude-Data', families[0]);
|
|
86
|
+
}).catch(error => {
|
|
87
|
+
defaultUsageDirectory = undefined;
|
|
88
|
+
throw error;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return defaultUsageDirectory;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function getUsageState(instances, claudePath, installed) {
|
|
95
|
+
async function read(directory) {
|
|
96
|
+
try {
|
|
97
|
+
return { history: await readUsageHistory(directory), error: false };
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('Could not read local usage history:', error);
|
|
100
|
+
return { history: null, error: true };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let defaultUsage = { history: null, error: false };
|
|
105
|
+
if (installed) {
|
|
106
|
+
try {
|
|
107
|
+
defaultUsage = await read(await getDefaultUsageDirectory(claudePath));
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error('Could not locate default usage history:', error);
|
|
110
|
+
defaultUsage = { history: null, error: true };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const managed = await Promise.all(instances.map(async instance => ({
|
|
114
|
+
id: instance.id.toLowerCase(),
|
|
115
|
+
...await read(path.join(process.env.APPDATA, `Claude-Profile-${instance.id}`))
|
|
116
|
+
})));
|
|
117
|
+
return { default: defaultUsage, managed };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function getInstanceDirectory(instanceId) {
|
|
121
|
+
if (typeof instanceId !== 'string' || !instanceId.trim() || /[<>:"/\\|?*\x00-\x1f]/.test(instanceId) || /[. ]$/.test(instanceId)) {
|
|
122
|
+
throw new Error('Enter a valid Windows instance name.');
|
|
123
|
+
}
|
|
124
|
+
return path.join(process.env.APPDATA, `Claude-Profile-${instanceId}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function launchClaudeInstance(instanceId) {
|
|
128
|
+
const claudePath = findClaudePath();
|
|
129
|
+
|
|
130
|
+
if (!fs.existsSync(claudePath)) {
|
|
131
|
+
dialog.showErrorBox('Unable to launch Claude', `Could not locate Claude executable at: ${claudePath}`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
const args = [];
|
|
137
|
+
if (instanceId !== null) {
|
|
138
|
+
const instanceDir = getInstanceDirectory(instanceId);
|
|
139
|
+
fs.mkdirSync(instanceDir, { recursive: true });
|
|
140
|
+
args.push(`--user-data-dir=${instanceDir}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const child = spawn(claudePath, args, {
|
|
144
|
+
detached: true,
|
|
145
|
+
stdio: 'ignore'
|
|
146
|
+
});
|
|
147
|
+
child.on('error', (error) => {
|
|
148
|
+
dialog.showErrorBox('Unable to launch Claude', error.message);
|
|
149
|
+
});
|
|
150
|
+
child.unref();
|
|
151
|
+
} catch (error) {
|
|
152
|
+
dialog.showErrorBox('Unable to launch Claude', error.message);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function createWindow() {
|
|
157
|
+
const window = new BrowserWindow({
|
|
158
|
+
width: 1000,
|
|
159
|
+
height: 760,
|
|
160
|
+
minWidth: 620,
|
|
161
|
+
minHeight: 550,
|
|
162
|
+
backgroundColor: '#f7f5f0',
|
|
163
|
+
autoHideMenuBar: true,
|
|
164
|
+
webPreferences: {
|
|
165
|
+
preload: path.join(__dirname, 'preload.js')
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
window.loadFile(path.join(__dirname, 'index.html')).catch((error) => {
|
|
169
|
+
dialog.showErrorBox('Unable to load launcher', error.message);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
ipcMain.on('launch-claude', (_event, instanceId) => {
|
|
174
|
+
launchClaudeInstance(instanceId);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
ipcMain.handle('delete-instance', async (event, instanceId) => {
|
|
178
|
+
try {
|
|
179
|
+
const instanceDir = getInstanceDirectory(instanceId);
|
|
180
|
+
if (!fs.lstatSync(instanceDir).isDirectory()) {
|
|
181
|
+
throw new Error('The instance data directory does not exist or is not a directory.');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const result = await dialog.showMessageBox(BrowserWindow.fromWebContents(event.sender), {
|
|
185
|
+
type: 'warning',
|
|
186
|
+
title: 'Delete instance',
|
|
187
|
+
message: `Permanently delete "${instanceId.replace(/_/g, ' ')}"?`,
|
|
188
|
+
detail: `Close Claude for this instance first. Its data in ${instanceDir} will be permanently deleted and cannot be recovered.`,
|
|
189
|
+
buttons: ['Cancel', 'Delete instance'],
|
|
190
|
+
defaultId: 0,
|
|
191
|
+
cancelId: 0,
|
|
192
|
+
noLink: true
|
|
193
|
+
});
|
|
194
|
+
if (result.response !== 1) return 'cancelled';
|
|
195
|
+
|
|
196
|
+
if (!fs.lstatSync(instanceDir).isDirectory()) {
|
|
197
|
+
throw new Error('The instance data directory no longer exists or is not a directory.');
|
|
198
|
+
}
|
|
199
|
+
await fs.promises.rm(instanceDir, { recursive: true });
|
|
200
|
+
return 'deleted';
|
|
201
|
+
} catch (error) {
|
|
202
|
+
dialog.showErrorBox('Unable to delete instance', error.message);
|
|
203
|
+
return 'failed';
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
ipcMain.handle('get-instance-state', async () => {
|
|
208
|
+
const appData = process.env.APPDATA;
|
|
209
|
+
const instances = listInstances(appData);
|
|
210
|
+
const claudePath = findClaudePath();
|
|
211
|
+
const installed = fs.existsSync(claudePath);
|
|
212
|
+
const usage = await getUsageState(instances, claudePath, installed);
|
|
213
|
+
if (!installed) return { instances, installed, running: null, usage };
|
|
214
|
+
try {
|
|
215
|
+
const running = await getRunningInstances(claudePath, appData);
|
|
216
|
+
return { instances, installed, running, usage };
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error('Could not check Claude running status:', error);
|
|
219
|
+
return { instances, installed, running: null, usage };
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
app.whenReady().then(() => {
|
|
224
|
+
createWindow();
|
|
225
|
+
app.on('activate', () => {
|
|
226
|
+
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
app.on('window-all-closed', () => {
|
|
231
|
+
if (process.platform !== 'darwin') app.quit();
|
|
232
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-instance-manager",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Manage and launch multiple native Claude Desktop instances",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"cim": "cli.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "electron .",
|
|
11
|
+
"test": "node --test"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"electron": "^45.6.1",
|
|
15
|
+
"yargs": "^17.7.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/preload.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const { contextBridge, ipcRenderer } = require('electron');
|
|
2
|
+
|
|
3
|
+
contextBridge.exposeInMainWorld('claudeManager', {
|
|
4
|
+
launch: (name) => ipcRenderer.send('launch-claude', name),
|
|
5
|
+
getInstanceState: () => ipcRenderer.invoke('get-instance-state'),
|
|
6
|
+
deleteInstance: (id) => ipcRenderer.invoke('delete-instance', id)
|
|
7
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { execFileSync } = require('child_process');
|
|
7
|
+
|
|
8
|
+
test('CLI describes existing and missing instances without changing their data paths', () => {
|
|
9
|
+
const appData = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-cli-test-'));
|
|
10
|
+
const instanceDir = path.join(appData, 'Claude-Profile-Work');
|
|
11
|
+
fs.mkdirSync(instanceDir);
|
|
12
|
+
const cli = path.join(__dirname, '..', 'cli.js');
|
|
13
|
+
const run = (...args) => execFileSync(process.execPath, [cli, ...args], {
|
|
14
|
+
encoding: 'utf8',
|
|
15
|
+
env: { ...process.env, APPDATA: appData }
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
assert.match(run('list'), /Existing Claude Instances:/);
|
|
20
|
+
assert.match(run('list'), /Work/);
|
|
21
|
+
assert.match(run('delete', 'Missing'), /Instance "Missing" not found/);
|
|
22
|
+
assert.match(run('--help'), /Claude instances/);
|
|
23
|
+
assert.match(run('--help'), /cim \[command\] \[options\]/);
|
|
24
|
+
assert.ok(fs.existsSync(instanceDir));
|
|
25
|
+
} finally {
|
|
26
|
+
fs.rmSync(appData, { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const vm = require('vm');
|
|
7
|
+
const { EventEmitter } = require('events');
|
|
8
|
+
const { listInstances, runningInstances } = require('../instances');
|
|
9
|
+
|
|
10
|
+
test('discovers instance folders without changing their directory identities', () => {
|
|
11
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-instance-manager-test-'));
|
|
12
|
+
try {
|
|
13
|
+
fs.mkdirSync(path.join(dir, 'Claude-Profile-Work__Team'));
|
|
14
|
+
fs.writeFileSync(path.join(dir, 'Claude-Profile-NotADirectory'), '');
|
|
15
|
+
fs.mkdirSync(path.join(dir, 'Unrelated'));
|
|
16
|
+
assert.deepEqual(listInstances(dir), [{ id: 'Work__Team', name: 'Work Team' }]);
|
|
17
|
+
} finally {
|
|
18
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('identifies default and managed main processes, including quoted paths', () => {
|
|
23
|
+
const exe = 'C:\\Program Files\\Claude\\claude.exe';
|
|
24
|
+
const appData = 'C:\\Users\\Test User\\AppData\\Roaming';
|
|
25
|
+
const processes = [
|
|
26
|
+
{ ExecutablePath: exe, CommandLine: `"${exe}"` },
|
|
27
|
+
{ ExecutablePath: exe, CommandLine: `"${exe}" "--user-data-dir=${appData}\\Claude-Profile-Work__Team"` },
|
|
28
|
+
{ ExecutablePath: exe, CommandLine: `"${exe}" --user-data-dir="${appData}\\Claude-Profile-Personal"` },
|
|
29
|
+
{ ExecutablePath: exe, CommandLine: `"${exe}" --type=renderer` },
|
|
30
|
+
{ ExecutablePath: 'C:\\Other\\claude.exe', CommandLine: 'other' }
|
|
31
|
+
];
|
|
32
|
+
assert.deepEqual(runningInstances(processes, exe, appData), {
|
|
33
|
+
defaultRunning: true,
|
|
34
|
+
instances: ['work__team', 'personal']
|
|
35
|
+
});
|
|
36
|
+
assert.deepEqual(runningInstances([processes[3]], exe, appData), {
|
|
37
|
+
defaultRunning: false,
|
|
38
|
+
instances: []
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('reports unavailable process details rather than claiming instances are stopped', () => {
|
|
43
|
+
assert.throws(() => runningInstances([{ ExecutablePath: null, CommandLine: null }], 'C:\\claude.exe', 'C:\\data'), /unavailable/);
|
|
44
|
+
assert.throws(() => runningInstances([{ ExecutablePath: 'C:\\claude.exe', CommandLine: 'claude.exe --user-data-dir' }], 'C:\\claude.exe', 'C:\\data'), /could not be determined/);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('wipe status checks all Claude executable paths and rejects relative data directories', () => {
|
|
48
|
+
const root = 'C:\\Users\\Test\\AppData\\Roaming';
|
|
49
|
+
const processes = [{
|
|
50
|
+
ExecutablePath: 'C:\\Other\\Claude.exe',
|
|
51
|
+
CommandLine: `Claude.exe --user-data-dir="${root}\\Claude-Profile-Work"`
|
|
52
|
+
}];
|
|
53
|
+
assert.deepEqual(runningInstances(processes, null, root, { allExecutables: true }).instances, ['work']);
|
|
54
|
+
processes[0].CommandLine = `Claude.exe --user-data-dir="${root}\\Claude-Profile-Work\\nested"`;
|
|
55
|
+
assert.deepEqual(runningInstances(processes, null, root, { allExecutables: true }).instances, ['work']);
|
|
56
|
+
assert.deepEqual(runningInstances(processes, null, root, {
|
|
57
|
+
allExecutables: true,
|
|
58
|
+
resolveInstancePath: () => 'C:\\outside'
|
|
59
|
+
}).instances, ['work']);
|
|
60
|
+
assert.throws(() => runningInstances([{
|
|
61
|
+
ExecutablePath: 'C:\\Other\\Claude.exe',
|
|
62
|
+
CommandLine: 'Claude.exe --user-data-dir=relative'
|
|
63
|
+
}], null, root, { allExecutables: true }), /relative data directory/);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('launches the existing default without a custom data directory', () => {
|
|
67
|
+
const handlers = {};
|
|
68
|
+
const launches = [];
|
|
69
|
+
const errors = [];
|
|
70
|
+
const appNames = [];
|
|
71
|
+
const appPaths = { userData: 'legacy-user-data', sessionData: 'legacy-session-data' };
|
|
72
|
+
const electron = {
|
|
73
|
+
app: {
|
|
74
|
+
setName: name => appNames.push(name),
|
|
75
|
+
getPath: name => appPaths[name],
|
|
76
|
+
setPath: (name, value) => { appPaths[name] = value; },
|
|
77
|
+
whenReady: () => ({ then: () => {} }), on: () => {}
|
|
78
|
+
},
|
|
79
|
+
BrowserWindow: function () {},
|
|
80
|
+
dialog: { showErrorBox: (_title, message) => errors.push(message) },
|
|
81
|
+
ipcMain: { on: (name, handler) => { handlers[name] = handler; }, handle: () => {} }
|
|
82
|
+
};
|
|
83
|
+
const filesystem = { existsSync: () => true, mkdirSync: () => {} };
|
|
84
|
+
const childProcess = {
|
|
85
|
+
execSync: () => '',
|
|
86
|
+
spawn: (exe, args) => {
|
|
87
|
+
launches.push({ exe, args });
|
|
88
|
+
const child = new EventEmitter();
|
|
89
|
+
child.unref = () => {};
|
|
90
|
+
return child;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const sandbox = {
|
|
94
|
+
require: id => id === 'electron' ? electron : id === 'fs' ? filesystem :
|
|
95
|
+
id === 'child_process' ? childProcess : id === './instances' || id === './usage' ? {} : require(id),
|
|
96
|
+
process: { env: { APPDATA: 'C:\\Users\\Test\\AppData\\Roaming', LOCALAPPDATA: 'C:\\Users\\Test\\AppData\\Local' }, platform: 'win32' },
|
|
97
|
+
__dirname: path.resolve(__dirname, '..'),
|
|
98
|
+
console
|
|
99
|
+
};
|
|
100
|
+
vm.runInNewContext(fs.readFileSync(path.join(__dirname, '..', 'main.js'), 'utf8'), sandbox);
|
|
101
|
+
assert.deepEqual(appNames, ['claude-desktop-manager', 'Claude Instance Manager']);
|
|
102
|
+
assert.deepEqual(appPaths, { userData: 'legacy-user-data', sessionData: 'legacy-session-data' });
|
|
103
|
+
handlers['launch-claude']({}, null);
|
|
104
|
+
handlers['launch-claude']({}, 'Work__Team');
|
|
105
|
+
handlers['launch-claude']({}, '..\\outside');
|
|
106
|
+
assert.equal(launches.length, 2);
|
|
107
|
+
assert.equal(launches[0].args.length, 0);
|
|
108
|
+
assert.equal(launches[1].args[0], `--user-data-dir=${path.join(sandbox.process.env.APPDATA, 'Claude-Profile-Work__Team')}`);
|
|
109
|
+
assert.match(errors[0], /valid Windows instance name/);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('confirms before deleting a managed instance and rejects invalid targets', async () => {
|
|
113
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-delete-test-'));
|
|
114
|
+
const instanceDir = path.join(dir, 'Claude-Profile-Work__Team');
|
|
115
|
+
fs.mkdirSync(instanceDir);
|
|
116
|
+
fs.writeFileSync(path.join(instanceDir, 'data.txt'), 'instance data');
|
|
117
|
+
const handlers = {};
|
|
118
|
+
const errors = [];
|
|
119
|
+
let response = 0;
|
|
120
|
+
let confirmation;
|
|
121
|
+
let removeError = null;
|
|
122
|
+
const filesystem = {
|
|
123
|
+
...fs,
|
|
124
|
+
promises: {
|
|
125
|
+
...fs.promises,
|
|
126
|
+
rm: async (...args) => {
|
|
127
|
+
if (removeError) throw removeError;
|
|
128
|
+
return fs.promises.rm(...args);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const electron = {
|
|
133
|
+
app: { setName: () => {}, getPath: name => name, setPath: () => {}, whenReady: () => ({ then: () => {} }), on: () => {} },
|
|
134
|
+
BrowserWindow: { fromWebContents: () => null },
|
|
135
|
+
dialog: {
|
|
136
|
+
showErrorBox: (_title, message) => errors.push(message),
|
|
137
|
+
showMessageBox: async (_window, options) => { confirmation = options; return { response }; }
|
|
138
|
+
},
|
|
139
|
+
ipcMain: { on: () => {}, handle: (name, handler) => { handlers[name] = handler; } }
|
|
140
|
+
};
|
|
141
|
+
const sandbox = {
|
|
142
|
+
require: id => id === 'electron' ? electron : id === 'fs' ? filesystem : id === './instances' || id === './usage' ? {} : require(id),
|
|
143
|
+
process: { env: { APPDATA: dir }, platform: 'win32' },
|
|
144
|
+
__dirname: path.resolve(__dirname, '..'),
|
|
145
|
+
console
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
vm.runInNewContext(fs.readFileSync(path.join(__dirname, '..', 'main.js'), 'utf8'), sandbox);
|
|
150
|
+
const remove = handlers['delete-instance'];
|
|
151
|
+
assert.equal(await remove({}, 'Work__Team'), 'cancelled');
|
|
152
|
+
assert.ok(fs.existsSync(path.join(instanceDir, 'data.txt')));
|
|
153
|
+
assert.equal(confirmation.defaultId, 0);
|
|
154
|
+
assert.match(confirmation.detail, /permanently deleted/);
|
|
155
|
+
|
|
156
|
+
assert.equal(await remove({}, '..\\outside'), 'failed');
|
|
157
|
+
assert.equal(await remove({}, null), 'failed');
|
|
158
|
+
assert.equal(errors.length, 2);
|
|
159
|
+
assert.ok(fs.existsSync(instanceDir));
|
|
160
|
+
|
|
161
|
+
response = 1;
|
|
162
|
+
removeError = new Error('Access denied');
|
|
163
|
+
assert.equal(await remove({}, 'Work__Team'), 'failed');
|
|
164
|
+
assert.match(errors[2], /Access denied/);
|
|
165
|
+
assert.ok(fs.existsSync(path.join(instanceDir, 'data.txt')));
|
|
166
|
+
removeError = null;
|
|
167
|
+
assert.equal(await remove({}, 'Work__Team'), 'deleted');
|
|
168
|
+
assert.equal(fs.existsSync(instanceDir), false);
|
|
169
|
+
assert.equal(await remove({}, 'Work__Team'), 'failed');
|
|
170
|
+
assert.equal(errors.length, 4);
|
|
171
|
+
} finally {
|
|
172
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('maps packaged default and managed usage to their own local data directories', async () => {
|
|
177
|
+
const handlers = {};
|
|
178
|
+
const directories = [];
|
|
179
|
+
let packageQueries = 0;
|
|
180
|
+
const appData = 'C:\\Users\\Test\\AppData\\Roaming';
|
|
181
|
+
const localAppData = 'C:\\Users\\Test\\AppData\\Local';
|
|
182
|
+
const packageDir = path.join(localAppData, 'Packages');
|
|
183
|
+
const executable = path.join(packageDir, 'Claude_mock', 'app', 'claude.exe');
|
|
184
|
+
const electron = {
|
|
185
|
+
app: { setName: () => {}, getPath: name => name, setPath: () => {}, whenReady: () => ({ then: () => {} }), on: () => {} },
|
|
186
|
+
BrowserWindow: function () {},
|
|
187
|
+
dialog: {},
|
|
188
|
+
ipcMain: { on: () => {}, handle: (name, handler) => { handlers[name] = handler; } }
|
|
189
|
+
};
|
|
190
|
+
const sandbox = {
|
|
191
|
+
require: id => {
|
|
192
|
+
if (id === 'electron') return electron;
|
|
193
|
+
if (id === 'fs') return {
|
|
194
|
+
existsSync: file => file === packageDir || file === executable,
|
|
195
|
+
readdirSync: () => ['Claude_mock']
|
|
196
|
+
};
|
|
197
|
+
if (id === 'child_process') return {
|
|
198
|
+
execFile: (_exe, _args, _options, callback) => {
|
|
199
|
+
packageQueries++;
|
|
200
|
+
callback(null, 'AnthropicPBC.Claude_family\n');
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
if (id === './instances') return {
|
|
204
|
+
listInstances: () => [{ id: 'Work', name: 'Work' }],
|
|
205
|
+
getRunningInstances: async () => ({ defaultRunning: false, instances: [] })
|
|
206
|
+
};
|
|
207
|
+
if (id === './usage') return {
|
|
208
|
+
readUsageHistory: async dir => {
|
|
209
|
+
directories.push(dir);
|
|
210
|
+
return { recordedAt: 1, metrics: { 'Five-hour': 20 } };
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
return require(id);
|
|
214
|
+
},
|
|
215
|
+
process: { env: { APPDATA: appData, LOCALAPPDATA: localAppData }, platform: 'win32' },
|
|
216
|
+
__dirname: path.resolve(__dirname, '..'),
|
|
217
|
+
console
|
|
218
|
+
};
|
|
219
|
+
vm.runInNewContext(fs.readFileSync(path.join(__dirname, '..', 'main.js'), 'utf8'), sandbox);
|
|
220
|
+
const first = await handlers['get-instance-state']();
|
|
221
|
+
await handlers['get-instance-state']();
|
|
222
|
+
assert.equal(first.usage.default.history.metrics['Five-hour'], 20);
|
|
223
|
+
assert.equal(first.usage.managed[0].id, 'work');
|
|
224
|
+
assert.equal(first.usage.managed[0].history.metrics['Five-hour'], 20);
|
|
225
|
+
assert.equal(directories[0], path.join(localAppData, 'Claude-Data', 'AnthropicPBC.Claude_family'));
|
|
226
|
+
assert.equal(directories[1], path.join(appData, 'Claude-Profile-Work'));
|
|
227
|
+
assert.equal(packageQueries, 1);
|
|
228
|
+
});
|