@myrialabs/clopen 0.4.39 → 0.4.40
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/backend/auth/permissions.ts +23 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/index.ts +6 -0
- package/backend/database/queries/worktree-branch-queries.ts +331 -0
- package/backend/db-client/cloning.test.ts +161 -0
- package/backend/db-client/cloning.ts +672 -0
- package/backend/db-client/connection-url.test.ts +117 -0
- package/backend/db-client/connection-url.ts +126 -0
- package/backend/db-client/drivers/mongodb.ts +2 -14
- package/backend/db-client/drivers/mysql.ts +2 -8
- package/backend/db-client/drivers/postgres.ts +10 -13
- package/backend/db-client/drivers/redis.ts +2 -6
- package/backend/db-client/env/detect.ts +438 -0
- package/backend/db-client/env/diff.ts +94 -0
- package/backend/db-client/env/env.test.ts +574 -0
- package/backend/db-client/env/index.ts +15 -0
- package/backend/db-client/env/naming.ts +202 -0
- package/backend/db-client/env/service.ts +487 -0
- package/backend/db-client/integrations/index.ts +7 -0
- package/backend/db-client/integrations/links.ts +41 -10
- package/backend/db-client/providers/neon/adapter.ts +522 -0
- package/backend/db-client/providers/neon/api-types.ts +182 -0
- package/backend/db-client/providers/neon/client.ts +298 -0
- package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
- package/backend/db-client/providers/neon/endpoints.ts +314 -0
- package/backend/db-client/providers/neon/list.test.ts +181 -0
- package/backend/db-client/providers/supabase/adapter.ts +25 -2
- package/backend/db-client/providers/types.ts +57 -0
- package/backend/env-files/discovery.ts +48 -0
- package/backend/env-files/edit.ts +213 -0
- package/backend/env-files/env-files.test.ts +515 -0
- package/backend/env-files/git.ts +27 -0
- package/backend/env-files/index.ts +44 -0
- package/backend/env-files/keys.ts +69 -0
- package/backend/env-files/parse.ts +157 -0
- package/backend/env-files/write.ts +229 -0
- package/backend/integrations/health.ts +18 -4
- package/backend/integrations/projections/types.ts +9 -2
- package/backend/integrations/registry/index.ts +3 -0
- package/backend/integrations/registry/providers/context7.ts +1 -0
- package/backend/integrations/registry/providers/firecrawl.ts +1 -0
- package/backend/integrations/registry/providers/github.ts +1 -0
- package/backend/integrations/registry/providers/neon.ts +55 -0
- package/backend/integrations/registry/providers/supabase.ts +1 -0
- package/backend/integrations/registry/providers/vercel.ts +1 -0
- package/backend/integrations/registry/types.ts +2 -0
- package/backend/work/start-work.ts +4 -0
- package/backend/worktrees/branching/engine.ts +557 -0
- package/backend/worktrees/branching/index.ts +56 -0
- package/backend/worktrees/branching/naming.test.ts +118 -0
- package/backend/worktrees/branching/naming.ts +97 -0
- package/backend/worktrees/branching/projector.test.ts +229 -0
- package/backend/worktrees/branching/projector.ts +139 -0
- package/backend/worktrees/branching/providers/neon.ts +220 -0
- package/backend/worktrees/branching/registry.ts +27 -0
- package/backend/worktrees/branching/service.ts +620 -0
- package/backend/worktrees/branching/suggest.test.ts +154 -0
- package/backend/worktrees/branching/suggest.ts +202 -0
- package/backend/worktrees/branching/types.ts +88 -0
- package/backend/worktrees/index.ts +7 -0
- package/backend/worktrees/service.ts +48 -1
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/projects/index.ts +6 -1
- package/backend/ws/projects/info.ts +70 -6
- package/backend/ws/projects/overview.test.ts +108 -0
- package/backend/ws/projects/overview.ts +300 -0
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/worktrees/branching.ts +212 -0
- package/backend/ws/worktrees/crud.ts +33 -7
- package/backend/ws/worktrees/index.ts +14 -0
- package/dist/assets/{index-C8Gi-b6U.js → index-D4igjF6e.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DONNwiXm.js} +1 -1
- package/dist/assets/main-BrfXH1bL.js +1205 -0
- package/dist/assets/main-CT1PygsW.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/tool-icons.ts +6 -2
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +1 -0
- package/shared/types/db-client/integrations.ts +30 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* db-client — the connection as environment variables.
|
|
3
|
+
*
|
|
4
|
+
* THESE ROUTES HAND BACK A PASSWORD, which no other db-client route does:
|
|
5
|
+
* `db-client:list` redacts every secret before it crosses the wire. That is not
|
|
6
|
+
* an oversight being reversed, it is a different question — a connection string
|
|
7
|
+
* the user cannot copy solves nothing, and the whole feature is "put this into
|
|
8
|
+
* my project's `.env`". So the secret is gated at the ROUTE instead of stripped
|
|
9
|
+
* from the payload.
|
|
10
|
+
*
|
|
11
|
+
* Two gates, both already load-bearing elsewhere:
|
|
12
|
+
*
|
|
13
|
+
* - `requireDbClientConnectionAccess` is the owner-or-admin check the rest of
|
|
14
|
+
* the surface uses. A projected connection has NO owner, which in
|
|
15
|
+
* db-client's access model means only an admin can reach it — the same
|
|
16
|
+
* reason `db-client:link` is admin-only, arrived at without a second rule.
|
|
17
|
+
* - `requireProjectWorkspace` decides which tree a write lands in, and it is
|
|
18
|
+
* worktree-aware. A user looking at a worktree gets the worktree's `.env`,
|
|
19
|
+
* because that is the checkout their dev server is running from.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { t } from 'elysia';
|
|
23
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
24
|
+
import type { WSConnection } from '$shared/utils/ws-server';
|
|
25
|
+
import { ws } from '$backend/utils/ws';
|
|
26
|
+
import { worktreeQueries } from '$backend/database/queries';
|
|
27
|
+
import { dbClientEnv, type EnvScope } from '../../db-client/env';
|
|
28
|
+
import type { DbEnvRequest } from '$shared/types/db-client';
|
|
29
|
+
import { requireProjectWorkspace } from '../access';
|
|
30
|
+
import { requireDbClientConnectionAccess } from './access';
|
|
31
|
+
|
|
32
|
+
const requestSchema = t.Object({
|
|
33
|
+
connectionId: t.String({ minLength: 1 }),
|
|
34
|
+
projectId: t.Optional(t.String()),
|
|
35
|
+
database: t.Optional(t.String()),
|
|
36
|
+
shape: t.Union([t.Literal('url'), t.Literal('split')]),
|
|
37
|
+
prefix: t.Optional(t.String()),
|
|
38
|
+
keyMap: t.Optional(t.Record(t.String(), t.String())),
|
|
39
|
+
fileName: t.Optional(t.String()),
|
|
40
|
+
includeAlternate: t.Optional(t.Boolean()),
|
|
41
|
+
allowTracked: t.Optional(t.Boolean())
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The tree a write would land in, or null.
|
|
46
|
+
*
|
|
47
|
+
* Null rather than an error when no project is in scope: the panel still shows
|
|
48
|
+
* the variables and still lets them be copied, which is most of the value. A
|
|
49
|
+
* route that refused would turn "no project open" into "this feature is
|
|
50
|
+
* broken".
|
|
51
|
+
*/
|
|
52
|
+
function resolveScope(conn: WSConnection, projectId?: string): EnvScope | null {
|
|
53
|
+
const target = (projectId ?? '').trim() || safeCurrentProject(conn);
|
|
54
|
+
if (!target) return null;
|
|
55
|
+
|
|
56
|
+
const { project, root, worktreeId } = requireProjectWorkspace(conn, target);
|
|
57
|
+
const worktree = worktreeId ? worktreeQueries.getById(worktreeId) : null;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
projectId: project.id,
|
|
61
|
+
// Named so the panel can say WHERE it is about to write. A worktree is a
|
|
62
|
+
// different checkout with its own dotenv file, and not saying so is how a
|
|
63
|
+
// user ends up looking for a variable in the main project.
|
|
64
|
+
projectName: worktree ? `${project.name} / ${worktree.name}` : project.name,
|
|
65
|
+
root
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function safeCurrentProject(conn: WSConnection): string | null {
|
|
70
|
+
try {
|
|
71
|
+
return ws.getProjectId(conn);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function requireScope(conn: WSConnection, projectId?: string): EnvScope {
|
|
78
|
+
const scope = resolveScope(conn, projectId);
|
|
79
|
+
if (!scope) throw new Error('Open a project before writing to its environment file.');
|
|
80
|
+
return scope;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const envHandler = createRouter()
|
|
84
|
+
.http('db-client:env-state', {
|
|
85
|
+
data: t.Object({
|
|
86
|
+
connectionId: t.String({ minLength: 1 }),
|
|
87
|
+
projectId: t.Optional(t.String()),
|
|
88
|
+
database: t.Optional(t.String())
|
|
89
|
+
}),
|
|
90
|
+
response: t.Any()
|
|
91
|
+
}, async ({ data, conn }) => {
|
|
92
|
+
const connection = requireDbClientConnectionAccess(conn, data.connectionId);
|
|
93
|
+
return dbClientEnv.state(connection, resolveScope(conn, data.projectId), data.database);
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
.http('db-client:env-preview', {
|
|
97
|
+
data: requestSchema,
|
|
98
|
+
response: t.Any()
|
|
99
|
+
}, async ({ data, conn }) => {
|
|
100
|
+
const connection = requireDbClientConnectionAccess(conn, data.connectionId);
|
|
101
|
+
return dbClientEnv.plan(connection, resolveScope(conn, data.projectId), toRequest(data));
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
.http('db-client:env-apply', {
|
|
105
|
+
data: requestSchema,
|
|
106
|
+
response: t.Any()
|
|
107
|
+
}, async ({ data, conn }) => {
|
|
108
|
+
const connection = requireDbClientConnectionAccess(conn, data.connectionId);
|
|
109
|
+
return dbClientEnv.apply(connection, requireScope(conn, data.projectId), toRequest(data));
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
.http('db-client:env-remove', {
|
|
113
|
+
data: requestSchema,
|
|
114
|
+
response: t.Any()
|
|
115
|
+
}, async ({ data, conn }) => {
|
|
116
|
+
const connection = requireDbClientConnectionAccess(conn, data.connectionId);
|
|
117
|
+
return dbClientEnv.remove(connection, requireScope(conn, data.projectId), toRequest(data));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
function toRequest(data: {
|
|
121
|
+
connectionId: string;
|
|
122
|
+
projectId?: string;
|
|
123
|
+
database?: string;
|
|
124
|
+
shape: string;
|
|
125
|
+
prefix?: string;
|
|
126
|
+
keyMap?: Record<string, string>;
|
|
127
|
+
fileName?: string;
|
|
128
|
+
includeAlternate?: boolean;
|
|
129
|
+
allowTracked?: boolean;
|
|
130
|
+
}): DbEnvRequest {
|
|
131
|
+
return {
|
|
132
|
+
connectionId: data.connectionId,
|
|
133
|
+
projectId: data.projectId,
|
|
134
|
+
database: data.database,
|
|
135
|
+
shape: data.shape as DbEnvRequest['shape'],
|
|
136
|
+
prefix: data.prefix ?? '',
|
|
137
|
+
keyMap: data.keyMap as DbEnvRequest['keyMap'],
|
|
138
|
+
fileName: data.fileName,
|
|
139
|
+
includeAlternate: data.includeAlternate,
|
|
140
|
+
allowTracked: data.allowTracked
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -18,6 +18,7 @@ import { structureHandler } from './structure';
|
|
|
18
18
|
import { ioHandler } from './io';
|
|
19
19
|
import { accountsHandler } from './accounts';
|
|
20
20
|
import { supabaseHandler } from './supabase';
|
|
21
|
+
import { envHandler } from './env';
|
|
21
22
|
|
|
22
23
|
export const dbClientRouter = createRouter()
|
|
23
24
|
.merge(connectionsHandler)
|
|
@@ -27,6 +28,7 @@ export const dbClientRouter = createRouter()
|
|
|
27
28
|
.merge(ioHandler)
|
|
28
29
|
.merge(accountsHandler)
|
|
29
30
|
.merge(supabaseHandler)
|
|
31
|
+
.merge(envHandler)
|
|
30
32
|
/**
|
|
31
33
|
* Broadcast when the connection list changes for a reason the requesting
|
|
32
34
|
* client did not cause — a link created, a link dropped, an account
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* - crud.ts: HTTP endpoints for CRUD operations (list, create, get, update, delete)
|
|
8
8
|
* - status.ts: Real-time status updates and watching (get-status, watch, unwatch, events)
|
|
9
9
|
* - presence.ts: User presence management (update-presence with broadcast)
|
|
10
|
+
* - overview.ts: Per-project resource usage for every project (projects:overview)
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
13
|
import { createRouter } from '$shared/utils/ws-server';
|
|
@@ -14,6 +15,7 @@ import { crudHandler } from './crud';
|
|
|
14
15
|
import { statusHandler } from './status';
|
|
15
16
|
import { presenceHandler } from './presence';
|
|
16
17
|
import { infoHandler } from './info';
|
|
18
|
+
import { overviewHandler } from './overview';
|
|
17
19
|
|
|
18
20
|
export const projectsRouter = createRouter()
|
|
19
21
|
// CRUD Operations (HTTP)
|
|
@@ -26,4 +28,7 @@ export const projectsRouter = createRouter()
|
|
|
26
28
|
.merge(presenceHandler)
|
|
27
29
|
|
|
28
30
|
// Per-project resource info (storage + process-isolated CPU/RAM)
|
|
29
|
-
.merge(infoHandler)
|
|
31
|
+
.merge(infoHandler)
|
|
32
|
+
|
|
33
|
+
// Per-project resource usage across every project the caller can see
|
|
34
|
+
.merge(overviewHandler);
|
|
@@ -24,7 +24,9 @@ import { collectProcessTree, getHostFacts, getProcessTable } from '../../host/me
|
|
|
24
24
|
const MAX_ENTRIES = 300_000;
|
|
25
25
|
/** Wall-clock ceiling — the backstop for slow disks and network mounts. */
|
|
26
26
|
const WALK_DEADLINE_MS = 15_000;
|
|
27
|
-
/** How many `stat` calls are in flight at once while walking one directory.
|
|
27
|
+
/** How many `stat` calls are in flight at once while walking one directory.
|
|
28
|
+
* Multiplied by the number of concurrent walks, so `MAX_BACKGROUND_WALKS`
|
|
29
|
+
* bounds the descriptor fan-out on platforms with a low `ulimit -n`. */
|
|
28
30
|
const STAT_BATCH = 64;
|
|
29
31
|
/** A folder's size changes slowly; a poll should not re-walk it every tick. */
|
|
30
32
|
const STORAGE_CACHE_TTL_MS = 15_000;
|
|
@@ -37,7 +39,7 @@ const PORTS_CACHE_TTL_MS = 5_000;
|
|
|
37
39
|
|
|
38
40
|
// ── Storage ──────────────────────────────────────────────────────────────────
|
|
39
41
|
|
|
40
|
-
interface FolderStats {
|
|
42
|
+
export interface FolderStats {
|
|
41
43
|
sizeBytes: number;
|
|
42
44
|
fileCount: number;
|
|
43
45
|
dirCount: number;
|
|
@@ -49,7 +51,9 @@ const storageCache = new Map<string, { stats: FolderStats; at: number; ttl: numb
|
|
|
49
51
|
const storageInFlight = new Map<string, Promise<FolderStats>>();
|
|
50
52
|
|
|
51
53
|
/** Cached and single-flighted: a big walk outlives the poll interval, so a
|
|
52
|
-
* result-only cache would let every tick start another one.
|
|
54
|
+
* result-only cache would let every tick start another one. Callers that
|
|
55
|
+
* cannot afford the wait go through `peekFolderStats()` below, which shares
|
|
56
|
+
* this cache and this in-flight guard. */
|
|
53
57
|
function getFolderStats(root: string): Promise<FolderStats> {
|
|
54
58
|
const cached = storageCache.get(root);
|
|
55
59
|
if (cached && Date.now() - cached.at < cached.ttl) {
|
|
@@ -83,6 +87,61 @@ function getFolderStats(root: string): Promise<FolderStats> {
|
|
|
83
87
|
return walk;
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
/** Walk slots for callers that ask about every project at once.
|
|
91
|
+
*
|
|
92
|
+
* One walk already keeps `STAT_BATCH` `stat` calls in flight; fanning out
|
|
93
|
+
* unbounded multiplies that by the project count, which is how a 20-project
|
|
94
|
+
* install reaches thousands of open descriptors — past the 256 soft limit
|
|
95
|
+
* macOS hands a GUI-launched process. */
|
|
96
|
+
const MAX_BACKGROUND_WALKS = 4;
|
|
97
|
+
let runningBackgroundWalks = 0;
|
|
98
|
+
const queuedWalks: string[] = [];
|
|
99
|
+
const queuedWalkPaths = new Set<string>();
|
|
100
|
+
|
|
101
|
+
function pumpBackgroundWalks(): void {
|
|
102
|
+
while (runningBackgroundWalks < MAX_BACKGROUND_WALKS && queuedWalks.length > 0) {
|
|
103
|
+
const root = queuedWalks.shift() as string;
|
|
104
|
+
queuedWalkPaths.delete(root);
|
|
105
|
+
runningBackgroundWalks++;
|
|
106
|
+
// `getFolderStats` turns walk failures into error stats, so a rejection
|
|
107
|
+
// here is a programming fault, not a slow disk — swallow it and free
|
|
108
|
+
// the slot either way.
|
|
109
|
+
void getFolderStats(root)
|
|
110
|
+
.catch((error) => {
|
|
111
|
+
debug.warn('project', 'background folder walk failed:', error);
|
|
112
|
+
})
|
|
113
|
+
.finally(() => {
|
|
114
|
+
runningBackgroundWalks--;
|
|
115
|
+
pumpBackgroundWalks();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Last known stats for a folder, never a wait.
|
|
122
|
+
*
|
|
123
|
+
* `getFolderStats` blocks for the whole walk on a cold cache, which is right
|
|
124
|
+
* for `projects:info` (one folder, one modal) and wrong for `projects:overview`
|
|
125
|
+
* (every folder, polled). The overview would hold its request open for the full
|
|
126
|
+
* walk deadline on first open — exactly the long skeleton this panel exists to
|
|
127
|
+
* avoid. Instead, callers get the previous answer immediately (`null` before
|
|
128
|
+
* the first walk ever finished) and a refresh is queued behind a bounded pool.
|
|
129
|
+
*/
|
|
130
|
+
export function peekFolderStats(root: string): FolderStats | null {
|
|
131
|
+
const cached = storageCache.get(root);
|
|
132
|
+
if (cached && Date.now() - cached.at < cached.ttl) return cached.stats;
|
|
133
|
+
|
|
134
|
+
if (!storageInFlight.has(root) && !queuedWalkPaths.has(root)) {
|
|
135
|
+
queuedWalkPaths.add(root);
|
|
136
|
+
queuedWalks.push(root);
|
|
137
|
+
pumpBackgroundWalks();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Stale beats empty: the size a project had a minute ago is still the right
|
|
141
|
+
// order of magnitude, and the next poll replaces it.
|
|
142
|
+
return cached?.stats ?? null;
|
|
143
|
+
}
|
|
144
|
+
|
|
86
145
|
async function walkFolder(root: string): Promise<FolderStats> {
|
|
87
146
|
try {
|
|
88
147
|
const rootStat = await stat(root);
|
|
@@ -149,7 +208,7 @@ async function walkFolder(root: string): Promise<FolderStats> {
|
|
|
149
208
|
|
|
150
209
|
// ── Per-project slice of the host process table ──────────────────────────────
|
|
151
210
|
|
|
152
|
-
interface ProjectProcess {
|
|
211
|
+
export interface ProjectProcess {
|
|
153
212
|
pid: number;
|
|
154
213
|
parentPid: number;
|
|
155
214
|
name: string;
|
|
@@ -158,7 +217,7 @@ interface ProjectProcess {
|
|
|
158
217
|
command: string;
|
|
159
218
|
}
|
|
160
219
|
|
|
161
|
-
interface ProcessStats {
|
|
220
|
+
export interface ProcessStats {
|
|
162
221
|
status: 'running' | 'not_running';
|
|
163
222
|
cpuPercent: number | null;
|
|
164
223
|
memRssBytes: number | null;
|
|
@@ -178,7 +237,12 @@ const IDLE: ProcessStats = {
|
|
|
178
237
|
rootPids: []
|
|
179
238
|
};
|
|
180
239
|
|
|
181
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Exported for reuse by `projects:overview` so per-project CPU/RAM there is
|
|
242
|
+
* measured exactly the way `projects:info` measures it — same roots, same
|
|
243
|
+
* shared process table, same capacity basis. No behavior change for callers.
|
|
244
|
+
*/
|
|
245
|
+
export async function getProjectProcessStats(projectId: string, totalMemBytes: number): Promise<ProcessStats> {
|
|
182
246
|
const rootPids = projectShellPids(projectId);
|
|
183
247
|
|
|
184
248
|
// No shells means nothing is running, so zero is the truth here.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
sortByFootprint,
|
|
4
|
+
summarizeStorage,
|
|
5
|
+
type ProjectResourceEntry,
|
|
6
|
+
type ProjectStorage,
|
|
7
|
+
type StorageState
|
|
8
|
+
} from './overview';
|
|
9
|
+
|
|
10
|
+
function storage(state: StorageState, sizeBytes = 0, opts?: { files?: number; dirs?: number; truncated?: boolean; error?: string }): ProjectStorage {
|
|
11
|
+
return {
|
|
12
|
+
state,
|
|
13
|
+
sizeBytes,
|
|
14
|
+
fileCount: opts?.files ?? 0,
|
|
15
|
+
dirCount: opts?.dirs ?? 0,
|
|
16
|
+
truncated: opts?.truncated ?? false,
|
|
17
|
+
error: opts?.error ?? null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function entry(
|
|
22
|
+
id: string,
|
|
23
|
+
storageValue: ProjectStorage,
|
|
24
|
+
opts?: { status?: 'running' | 'idle'; lastOpened?: string; cpuPercent?: number | null }
|
|
25
|
+
): ProjectResourceEntry {
|
|
26
|
+
return {
|
|
27
|
+
id,
|
|
28
|
+
name: id,
|
|
29
|
+
path: `/tmp/${id}`,
|
|
30
|
+
created_at: '2026-01-01T00:00:00.000Z',
|
|
31
|
+
last_opened_at: opts?.lastOpened ?? '2026-02-01T00:00:00.000Z',
|
|
32
|
+
status: opts?.status ?? 'idle',
|
|
33
|
+
cpuPercent: opts?.cpuPercent ?? 0,
|
|
34
|
+
memRssBytes: 0,
|
|
35
|
+
memPercent: 0,
|
|
36
|
+
storage: storageValue
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('summarizeStorage', () => {
|
|
41
|
+
it('sums measured entries and counts the rest by state', () => {
|
|
42
|
+
const totals = summarizeStorage([
|
|
43
|
+
entry('a', storage('ready', 100, { files: 5, dirs: 1 })),
|
|
44
|
+
entry('b', storage('ready', 200, { files: 7, dirs: 3 })),
|
|
45
|
+
entry('busy', storage('ready', 500, { files: 50, dirs: 9 }), { status: 'running' }),
|
|
46
|
+
entry('cold', storage('measuring')),
|
|
47
|
+
entry('gone', storage('unavailable', 0, { error: 'ENOENT: no such file or directory' }))
|
|
48
|
+
]);
|
|
49
|
+
expect(totals).toEqual({
|
|
50
|
+
measuredCount: 3,
|
|
51
|
+
measuringCount: 1,
|
|
52
|
+
unavailableCount: 1,
|
|
53
|
+
totalStorageBytes: 800,
|
|
54
|
+
totalFiles: 62,
|
|
55
|
+
totalDirs: 13
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('keeps a pending walk out of the totals instead of reading it as 0 B', () => {
|
|
60
|
+
expect(summarizeStorage([entry('cold', storage('measuring'))])).toEqual({
|
|
61
|
+
measuredCount: 0,
|
|
62
|
+
measuringCount: 1,
|
|
63
|
+
unavailableCount: 0,
|
|
64
|
+
totalStorageBytes: 0,
|
|
65
|
+
totalFiles: 0,
|
|
66
|
+
totalDirs: 0
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('sortByFootprint', () => {
|
|
72
|
+
it('ranks by sizeBytes DESC across statuses', () => {
|
|
73
|
+
const sorted = sortByFootprint([
|
|
74
|
+
entry('s', storage('ready', 10)),
|
|
75
|
+
entry('xl', storage('ready', 200)),
|
|
76
|
+
entry('xxl', storage('ready', 500)),
|
|
77
|
+
entry('biggest', storage('ready', 900), { status: 'running', cpuPercent: 12.5 }),
|
|
78
|
+
entry('tiny', storage('ready', 1))
|
|
79
|
+
]);
|
|
80
|
+
expect(sorted.map((e) => e.id)).toEqual(['biggest', 'xxl', 'xl', 's', 'tiny']);
|
|
81
|
+
expect(sorted[0].status).toBe('running');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('lists measured projects first, then measuring, then unavailable', () => {
|
|
85
|
+
const sorted = sortByFootprint([
|
|
86
|
+
entry('gone', storage('unavailable', 0, { error: 'ENOENT' })),
|
|
87
|
+
entry('cold', storage('measuring')),
|
|
88
|
+
entry('partial', storage('ready', 400, { truncated: true })),
|
|
89
|
+
entry('full', storage('ready', 300))
|
|
90
|
+
]);
|
|
91
|
+
expect(sorted.map((e) => e.id)).toEqual(['partial', 'full', 'cold', 'gone']);
|
|
92
|
+
expect(sorted[0].storage.truncated).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('breaks size ties by oldest last_opened_at first', () => {
|
|
96
|
+
const sorted = sortByFootprint([
|
|
97
|
+
entry('newer', storage('ready', 100), { lastOpened: '2026-03-01T00:00:00.000Z' }),
|
|
98
|
+
entry('older', storage('ready', 100), { lastOpened: '2026-01-15T00:00:00.000Z' })
|
|
99
|
+
]);
|
|
100
|
+
expect(sorted.map((e) => e.id)).toEqual(['older', 'newer']);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('does not mutate the caller array', () => {
|
|
104
|
+
const entries = [entry('a', storage('ready', 1)), entry('b', storage('ready', 2))];
|
|
105
|
+
sortByFootprint(entries);
|
|
106
|
+
expect(entries.map((e) => e.id)).toEqual(['a', 'b']);
|
|
107
|
+
});
|
|
108
|
+
});
|