@myrialabs/clopen 0.4.39 → 0.4.41
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/.env.example +1 -1
- package/backend/artifacts/detect.ts +15 -3
- package/backend/artifacts/generate.ts +2 -2
- package/backend/artifacts/index.ts +12 -4
- package/backend/artifacts/markers.ts +24 -1
- package/backend/artifacts/matrix.ts +47 -3
- package/backend/artifacts/sync.test.ts +3 -3
- package/backend/artifacts/sync.ts +8 -20
- package/backend/artifacts/types.ts +9 -3
- package/backend/auth/permissions.ts +29 -11
- package/backend/auth/session-cleanup.ts +10 -1
- package/backend/auth/tokens.ts +9 -1
- package/backend/chat/stream-manager.ts +42 -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/079_merge_commands_into_skills.ts +222 -0
- package/backend/database/migrations/080_create_file_shares.ts +111 -0
- package/backend/database/migrations/081_create_push_subscriptions.ts +32 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +12 -4
- package/backend/database/queries/profile-queries.ts +5 -3
- package/backend/database/queries/push-subscription-queries.test.ts +90 -0
- package/backend/database/queries/push-subscription-queries.ts +99 -0
- package/backend/database/queries/skill-queries.ts +96 -18
- 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/engine/adapters/claude/stream.ts +2 -1
- package/backend/engine/adapters/cline/stream.ts +3 -3
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +3 -3
- package/backend/engine/adapters/cursor/stream.ts +25 -3
- package/backend/engine/adapters/opencode/stream.ts +4 -6
- package/backend/engine/adapters/pi/stream.ts +2 -2
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/artifact-sync.ts +26 -20
- package/backend/engine/docs/artifacts.md +81 -22
- package/backend/engine/docs/quick-reference.md +2 -1
- package/backend/engine/resolve-model.test.ts +108 -0
- package/backend/engine/resolve-model.ts +53 -10
- package/backend/engine/structured-helpers.ts +22 -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/files/file-shares.ts +369 -0
- package/backend/files/path-browsing.ts +27 -21
- package/backend/http/files-shared.test.ts +505 -0
- package/backend/http/files-shared.ts +351 -0
- package/backend/http/push.ts +150 -0
- package/backend/index.ts +8 -0
- package/backend/instructions/index.ts +1 -1
- package/backend/instructions/sync.ts +30 -6
- 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/memory/compose.ts +2 -2
- package/backend/memory/consolidate.ts +2 -2
- package/backend/memory/extract/episodic.ts +2 -2
- package/backend/memory/judge.ts +2 -2
- package/backend/profiles/service.ts +7 -6
- package/backend/push/sender.ts +172 -0
- package/backend/push/tags.ts +25 -0
- package/backend/push/vapid-keys.ts +77 -0
- package/backend/push/webpush-crypto.test.ts +200 -0
- package/backend/push/webpush-crypto.ts +231 -0
- package/backend/skills/detect.ts +28 -0
- package/backend/skills/index.ts +22 -4
- package/backend/skills/invoke.test.ts +114 -0
- package/backend/skills/invoke.ts +222 -0
- package/backend/skills/service.ts +194 -44
- package/backend/skills/spec.ts +35 -0
- package/backend/skills/sync.ts +85 -55
- package/backend/subagents/detect.ts +2 -3
- package/backend/subagents/sync.ts +15 -8
- package/backend/work/pull-requests.ts +2 -2
- 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/chat/stream.ts +100 -8
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/files/index.ts +4 -0
- package/backend/ws/files/share.ts +95 -0
- package/backend/ws/git/branch-name.ts +2 -2
- package/backend/ws/git/commit-message.ts +2 -2
- package/backend/ws/index.ts +3 -4
- package/backend/ws/profiles/crud.ts +2 -3
- 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/skills/catalog.ts +5 -1
- package/backend/ws/skills/crud.ts +128 -22
- package/backend/ws/skills/index.ts +4 -1
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/user/crud.ts +12 -3
- 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-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DW89566K.js} +1 -1
- package/dist/assets/main-Cy5DXWPT.js +1207 -0
- package/dist/assets/main-DJKj_4By.css +1 -0
- package/dist/index.html +7 -2
- package/dist/sw.js +112 -0
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +31 -6
- package/shared/constants/notification-messages.ts +142 -0
- 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/unified/stream.ts +7 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/commands/detect.ts +0 -25
- package/backend/commands/index.ts +0 -9
- package/backend/commands/service.ts +0 -178
- package/backend/commands/store.ts +0 -45
- package/backend/commands/sync.ts +0 -94
- package/backend/database/queries/command-queries.ts +0 -82
- package/backend/ws/commands/crud.ts +0 -157
- package/backend/ws/commands/index.ts +0 -8
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for DB Client → environment variables.
|
|
3
|
+
*
|
|
4
|
+
* The cases here are the ones that make the feature worth having, or make it
|
|
5
|
+
* dangerous:
|
|
6
|
+
*
|
|
7
|
+
* - Detection has to find the name the PROJECT uses, or the whole premise
|
|
8
|
+
* collapses into "we wrote DATABASE_URL and nothing read it".
|
|
9
|
+
* - It has to find it in `.env.example` and in `prisma/schema.prisma` too,
|
|
10
|
+
* because a fresh clone has a schema and no dotenv file.
|
|
11
|
+
* - It must NOT claim `NEXT_PUBLIC_API_URL` or `SMTP_HOST` as a database.
|
|
12
|
+
* - Applying must update the key where the project already has it, and must
|
|
13
|
+
* never touch a file git tracks without being told to.
|
|
14
|
+
* - A worktree's branch block owns the end of the file, so a write above it
|
|
15
|
+
* has no effect and must be refused rather than reported as a success.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { describe, it, expect, beforeAll, beforeEach, afterEach, afterAll } from 'bun:test';
|
|
19
|
+
import fs from 'fs/promises';
|
|
20
|
+
import os from 'os';
|
|
21
|
+
import path from 'path';
|
|
22
|
+
import { closeDatabase, initializeDatabase } from '$backend/database';
|
|
23
|
+
import { execGit } from '$backend/git/git-executor';
|
|
24
|
+
import type { DbClientConnection, DbDriver } from '$shared/types/db-client';
|
|
25
|
+
import { detectEnvUsage } from './detect';
|
|
26
|
+
import { diffLines } from './diff';
|
|
27
|
+
import { defaultPrefixFor, renderEnvVars } from './naming';
|
|
28
|
+
import { dbClientEnv, type EnvScope } from './service';
|
|
29
|
+
|
|
30
|
+
const PASSWORD = 's3cr3t';
|
|
31
|
+
|
|
32
|
+
function connection(overrides: Partial<DbClientConnection> = {}): DbClientConnection {
|
|
33
|
+
return {
|
|
34
|
+
id: 'conn-env-test',
|
|
35
|
+
name: 'Shop DB',
|
|
36
|
+
driver: 'postgres' as DbDriver,
|
|
37
|
+
host: 'db.example.com',
|
|
38
|
+
port: 5432,
|
|
39
|
+
username: 'app',
|
|
40
|
+
password: PASSWORD,
|
|
41
|
+
database: 'shop',
|
|
42
|
+
sslMode: 'require',
|
|
43
|
+
sslCa: null,
|
|
44
|
+
ssh: {
|
|
45
|
+
enabled: false,
|
|
46
|
+
connectionId: null,
|
|
47
|
+
host: '',
|
|
48
|
+
port: 22,
|
|
49
|
+
username: '',
|
|
50
|
+
authMethod: 'password'
|
|
51
|
+
},
|
|
52
|
+
options: {},
|
|
53
|
+
color: null,
|
|
54
|
+
createdAt: '',
|
|
55
|
+
updatedAt: '',
|
|
56
|
+
lastUsedAt: null,
|
|
57
|
+
...overrides
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let root: string;
|
|
62
|
+
let scope: EnvScope;
|
|
63
|
+
|
|
64
|
+
beforeAll(async () => {
|
|
65
|
+
await initializeDatabase();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterAll(() => {
|
|
69
|
+
closeDatabase();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
beforeEach(async () => {
|
|
73
|
+
root = await fs.mkdtemp(path.join(os.tmpdir(), 'clopen-dbenv-'));
|
|
74
|
+
scope = { projectId: 'p1', projectName: 'Shop', root };
|
|
75
|
+
await execGit(['init'], root);
|
|
76
|
+
await execGit(['config', 'user.email', 'test@example.com'], root);
|
|
77
|
+
await execGit(['config', 'user.name', 'Test'], root);
|
|
78
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n.env.local\n');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(async () => {
|
|
82
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const request = (overrides: Record<string, unknown> = {}) =>
|
|
86
|
+
({
|
|
87
|
+
connectionId: 'conn-env-test',
|
|
88
|
+
shape: 'url',
|
|
89
|
+
prefix: 'DATABASE',
|
|
90
|
+
fileName: '.env',
|
|
91
|
+
...overrides
|
|
92
|
+
}) as Parameters<typeof dbClientEnv.plan>[2];
|
|
93
|
+
|
|
94
|
+
describe('naming', () => {
|
|
95
|
+
const base = {
|
|
96
|
+
connection: connection(),
|
|
97
|
+
url: 'postgresql://app:s3cr3t@db.example.com:5432/shop?sslmode=require',
|
|
98
|
+
alternateUrl: null as string | null
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it('renders a single URL and the split form from the same connection', () => {
|
|
102
|
+
expect(renderEnvVars({ ...base, naming: { shape: 'url', prefix: 'DATABASE' } })).toEqual([
|
|
103
|
+
{
|
|
104
|
+
key: 'DATABASE_URL',
|
|
105
|
+
value: 'postgresql://app:s3cr3t@db.example.com:5432/shop?sslmode=require',
|
|
106
|
+
isSecret: true,
|
|
107
|
+
note: null
|
|
108
|
+
}
|
|
109
|
+
]);
|
|
110
|
+
|
|
111
|
+
const split = renderEnvVars({ ...base, naming: { shape: 'split', prefix: 'DB' } });
|
|
112
|
+
expect(split.map((entry) => entry.key)).toEqual([
|
|
113
|
+
'DB_HOST',
|
|
114
|
+
'DB_PORT',
|
|
115
|
+
'DB_USER',
|
|
116
|
+
'DB_PASSWORD',
|
|
117
|
+
'DB_NAME',
|
|
118
|
+
'DB_SSLMODE'
|
|
119
|
+
]);
|
|
120
|
+
expect(split.find((entry) => entry.key === 'DB_PASSWORD')).toMatchObject({
|
|
121
|
+
value: PASSWORD,
|
|
122
|
+
isSecret: true
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('writes the project’s own names when it has some', () => {
|
|
127
|
+
// The whole reason the framework presets are gone: a project using
|
|
128
|
+
// DB_USERNAME gets DB_USERNAME, not a DB_USER beside it that nothing reads.
|
|
129
|
+
const split = renderEnvVars({
|
|
130
|
+
...base,
|
|
131
|
+
naming: {
|
|
132
|
+
shape: 'split',
|
|
133
|
+
prefix: 'DB',
|
|
134
|
+
keyMap: { user: 'DB_USERNAME', database: 'DB_DATABASE', host: 'PGHOST' }
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const keys = split.map((entry) => entry.key);
|
|
139
|
+
expect(keys).toContain('DB_USERNAME');
|
|
140
|
+
expect(keys).toContain('DB_DATABASE');
|
|
141
|
+
expect(keys).toContain('PGHOST');
|
|
142
|
+
expect(keys).not.toContain('DB_USER');
|
|
143
|
+
expect(keys).not.toContain('DB_NAME');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('uses the driver’s own word for the URL, not one spelling for all', () => {
|
|
147
|
+
// MONGODB_URL is not a variant of MONGODB_URI, it is a variable nothing
|
|
148
|
+
// reads. That is a fact about the driver, not about a framework.
|
|
149
|
+
expect(defaultPrefixFor('mongodb', 'url')).toBe('MONGODB');
|
|
150
|
+
const mongo = renderEnvVars({
|
|
151
|
+
connection: connection({ driver: 'mongodb', port: 27017 }),
|
|
152
|
+
naming: { shape: 'url', prefix: 'MONGODB' },
|
|
153
|
+
url: 'mongodb://app:s3cr3t@db.example.com:27017/shop',
|
|
154
|
+
alternateUrl: null
|
|
155
|
+
});
|
|
156
|
+
expect(mongo[0].key).toBe('MONGODB_URI');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('leaves the user out of a Redis split, because there is not one', () => {
|
|
160
|
+
const redis = renderEnvVars({
|
|
161
|
+
connection: connection({ driver: 'redis', database: '0', port: 6379, username: null }),
|
|
162
|
+
naming: { shape: 'split', prefix: 'REDIS' },
|
|
163
|
+
url: 'redis://:s3cr3t@db.example.com:6379/0',
|
|
164
|
+
alternateUrl: null
|
|
165
|
+
});
|
|
166
|
+
expect(redis.map((entry) => entry.key)).not.toContain('REDIS_USER');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('keeps an empty password but drops the fields that describe nothing', () => {
|
|
170
|
+
// `DB_HOST=` reads as a configured empty host and fails later than not
|
|
171
|
+
// being there. An empty password is a real local configuration.
|
|
172
|
+
const split = renderEnvVars({
|
|
173
|
+
connection: connection({ password: null, username: null, database: null }),
|
|
174
|
+
naming: { shape: 'split', prefix: 'DB' },
|
|
175
|
+
url: '',
|
|
176
|
+
alternateUrl: null
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expect(split.map((entry) => entry.key)).toEqual(['DB_HOST', 'DB_PORT', 'DB_PASSWORD', 'DB_SSLMODE']);
|
|
180
|
+
expect(split.find((entry) => entry.key === 'DB_PASSWORD')?.value).toBe('');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('writes the second endpoint only when there is one, under the provider’s suffix', () => {
|
|
184
|
+
expect(
|
|
185
|
+
renderEnvVars({ ...base, naming: { shape: 'url', prefix: 'DATABASE' } }).map((e) => e.key)
|
|
186
|
+
).toEqual(['DATABASE_URL']);
|
|
187
|
+
|
|
188
|
+
expect(
|
|
189
|
+
renderEnvVars({
|
|
190
|
+
...base,
|
|
191
|
+
alternateUrl: 'postgresql://direct',
|
|
192
|
+
naming: { shape: 'url', prefix: 'POSTGRES', altSuffix: '_NON_POOLING' }
|
|
193
|
+
}).map((e) => e.key)
|
|
194
|
+
).toEqual(['POSTGRES_URL', 'POSTGRES_URL_NON_POOLING']);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('describes SQLite as a path rather than inventing an endpoint', () => {
|
|
198
|
+
const vars = renderEnvVars({
|
|
199
|
+
connection: connection({ driver: 'sqlite', database: '/data/app.db' }),
|
|
200
|
+
naming: { shape: 'split', prefix: 'DB' },
|
|
201
|
+
url: 'file:/data/app.db',
|
|
202
|
+
alternateUrl: null
|
|
203
|
+
});
|
|
204
|
+
expect(vars).toEqual([{ key: 'DB_PATH', value: '/data/app.db', isSecret: false, note: null }]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe('detectEnvUsage', () => {
|
|
209
|
+
it('finds the convention the project actually uses, most-precedent file first', async () => {
|
|
210
|
+
// Two groups neither of which names an engine, so the file decides:
|
|
211
|
+
// `.env.local` wins in Next.js and Vite.
|
|
212
|
+
await fs.writeFile(path.join(root, '.env'), 'MAIN_DB_URL=postgres://old\nSMTP_HOST=mail\n');
|
|
213
|
+
await fs.writeFile(path.join(root, '.env.local'), 'DATABASE_URL=postgres://local\n');
|
|
214
|
+
|
|
215
|
+
const detection = await detectEnvUsage({
|
|
216
|
+
root,
|
|
217
|
+
projectId: 'p1',
|
|
218
|
+
projectName: 'Shop',
|
|
219
|
+
driver: 'postgres'
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const labels = detection.existing.map((group) => group.keys.join(','));
|
|
223
|
+
expect(labels).toContain('DATABASE_URL');
|
|
224
|
+
expect(labels).toContain('MAIN_DB_URL');
|
|
225
|
+
expect(detection.existing[0].keys).toEqual(['DATABASE_URL']);
|
|
226
|
+
expect(detection.existing[0].files).toEqual(['.env.local']);
|
|
227
|
+
// SMTP_HOST is not a database, and one stray HOST must not become a group.
|
|
228
|
+
expect(labels.join(' ')).not.toContain('SMTP');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('opens on the group that names THIS engine, not the other one', async () => {
|
|
232
|
+
// A project with both is ordinary, and only one of them belongs to the
|
|
233
|
+
// connection being written. Without this the first thing a Redis
|
|
234
|
+
// connection offers is to overwrite the Postgres URL.
|
|
235
|
+
await fs.writeFile(
|
|
236
|
+
path.join(root, '.env'),
|
|
237
|
+
'DATABASE_URL=postgres://pg\nREDIS_URL=redis://localhost:6379\n'
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
const forRedis = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'redis' });
|
|
241
|
+
expect(forRedis.existing[0].keys).toEqual(['REDIS_URL']);
|
|
242
|
+
|
|
243
|
+
const forPostgres = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
244
|
+
expect(forPostgres.existing[0].keys).toEqual(['DATABASE_URL']);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('groups a split convention and names it', async () => {
|
|
248
|
+
await fs.writeFile(
|
|
249
|
+
path.join(root, '.env'),
|
|
250
|
+
['DB_HOST=localhost', 'DB_PORT=5432', 'DB_USERNAME=app', 'DB_PASSWORD=x', 'DB_DATABASE=shop'].join('\n')
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
254
|
+
|
|
255
|
+
expect(detection.existing).toHaveLength(1);
|
|
256
|
+
expect(detection.existing[0]).toMatchObject({ prefix: 'DB', shape: 'split' });
|
|
257
|
+
expect(detection.existing[0].isTemplateOnly).toBe(false);
|
|
258
|
+
// The roles, mapped onto the names THIS project uses — `DB_USERNAME` and
|
|
259
|
+
// `DB_DATABASE`, not the generic `DB_USER` and `DB_NAME`.
|
|
260
|
+
expect(detection.existing[0].keyMap).toMatchObject({
|
|
261
|
+
host: 'DB_HOST',
|
|
262
|
+
port: 'DB_PORT',
|
|
263
|
+
user: 'DB_USERNAME',
|
|
264
|
+
password: 'DB_PASSWORD',
|
|
265
|
+
database: 'DB_DATABASE'
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('reads a template for its shape and marks it as documentation', async () => {
|
|
270
|
+
// `.env.example` is often the only record of the names a project expects,
|
|
271
|
+
// and writing a password into it is the one thing that must not happen.
|
|
272
|
+
await fs.writeFile(path.join(root, '.env.example'), '# DATABASE_URL=\nAPP_NAME=shop\n');
|
|
273
|
+
|
|
274
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
275
|
+
|
|
276
|
+
expect(detection.existing[0]).toMatchObject({
|
|
277
|
+
keys: ['DATABASE_URL'],
|
|
278
|
+
isTemplateOnly: true
|
|
279
|
+
});
|
|
280
|
+
expect(detection.files.find((file) => file.name === '.env.example')?.isTemplate).toBe(true);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('finds the name a Prisma schema reads when no dotenv file exists', async () => {
|
|
284
|
+
await fs.mkdir(path.join(root, 'prisma'), { recursive: true });
|
|
285
|
+
await fs.writeFile(
|
|
286
|
+
path.join(root, 'prisma/schema.prisma'),
|
|
287
|
+
'datasource db {\n provider = "postgresql"\n url = env("POSTGRES_PRISMA_URL")\n directUrl = env("DIRECT_URL")\n}\n'
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
291
|
+
|
|
292
|
+
expect(detection.sources).toContain('prisma/schema.prisma');
|
|
293
|
+
const keys = detection.existing.flatMap((group) => group.keys);
|
|
294
|
+
expect(keys).toContain('POSTGRES_PRISMA_URL');
|
|
295
|
+
expect(keys).toContain('DIRECT_URL');
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('finds the name a drizzle config reads', async () => {
|
|
299
|
+
await fs.writeFile(
|
|
300
|
+
path.join(root, 'drizzle.config.ts'),
|
|
301
|
+
'export default { dbCredentials: { url: process.env.SHOP_DATABASE_URL! } };\n'
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
305
|
+
expect(detection.existing.flatMap((g) => g.keys)).toContain('SHOP_DATABASE_URL');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('does not claim an unrelated URL as a database', async () => {
|
|
309
|
+
await fs.writeFile(
|
|
310
|
+
path.join(root, '.env'),
|
|
311
|
+
'NEXT_PUBLIC_API_URL=https://api.example.com\nSENTRY_DSN=https://x@sentry.io/1\n'
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
315
|
+
expect(detection.existing).toEqual([]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('claims a neutrally named URL when its VALUE is a connection string', async () => {
|
|
319
|
+
await fs.writeFile(path.join(root, '.env'), 'STORE_URL=postgres://app@localhost/shop\n');
|
|
320
|
+
|
|
321
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
322
|
+
expect(detection.existing[0].keys).toEqual(['STORE_URL']);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('refuses to offer Prisma’s shadow database', async () => {
|
|
326
|
+
// It is a separate, disposable database Prisma diffs migrations against;
|
|
327
|
+
// pointing it at the real one would run that diff on real data.
|
|
328
|
+
await fs.writeFile(path.join(root, '.env'), 'SHADOW_DATABASE_URL=postgres://shadow\n');
|
|
329
|
+
|
|
330
|
+
const detection = await detectEnvUsage({ root, projectId: 'p1', projectName: 'Shop', driver: 'postgres' });
|
|
331
|
+
expect(detection.existing).toEqual([]);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('dbClientEnv.state', () => {
|
|
336
|
+
it('opens on the convention and the file the project already uses', async () => {
|
|
337
|
+
await fs.writeFile(path.join(root, '.env'), 'DATABASE_URL=postgres://old\n');
|
|
338
|
+
await fs.writeFile(path.join(root, '.env.local'), 'DB_HOST=localhost\nDB_USER=app\nDB_PASSWORD=x\n');
|
|
339
|
+
|
|
340
|
+
const state = await dbClientEnv.state(connection(), scope);
|
|
341
|
+
|
|
342
|
+
expect(state.suggestion.shape).toBe('split');
|
|
343
|
+
expect(state.suggestion.prefix).toBe('DB');
|
|
344
|
+
expect(state.suggestion.fileName).toBe('.env.local');
|
|
345
|
+
expect(state.suggestion.keyMap).toMatchObject({ host: 'DB_HOST', user: 'DB_USER' });
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('falls back to a preset, and to .env, for a project with nothing', async () => {
|
|
349
|
+
const state = await dbClientEnv.state(connection(), scope);
|
|
350
|
+
|
|
351
|
+
expect(state.suggestion.shape).toBe('url');
|
|
352
|
+
expect(state.suggestion.prefix).toBe('DATABASE');
|
|
353
|
+
expect(state.suggestion.fileName).toBe('.env');
|
|
354
|
+
expect(state.detection.existing).toEqual([]);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('still lists the variables when no project is in scope', async () => {
|
|
358
|
+
const state = await dbClientEnv.state(connection(), null);
|
|
359
|
+
|
|
360
|
+
expect(state.suggestion.fileName).toBe(null);
|
|
361
|
+
expect(state.shapes.map((entry) => entry.id)).toEqual(['url', 'split']);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('warns about an SSH tunnel the project cannot use', async () => {
|
|
365
|
+
const state = await dbClientEnv.state(
|
|
366
|
+
connection({ ssh: { enabled: true, connectionId: null, host: 'jump', port: 22, username: 'x', authMethod: 'key' } }),
|
|
367
|
+
scope
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
expect(state.warnings.join(' ')).toContain('SSH tunnel');
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
describe('dbClientEnv.plan', () => {
|
|
375
|
+
it('updates the key where the project already has it', async () => {
|
|
376
|
+
await fs.writeFile(path.join(root, '.env'), '# app\nDATABASE_URL=postgres://old\nAPP=1\n');
|
|
377
|
+
|
|
378
|
+
const plan = await dbClientEnv.plan(connection(), scope, request());
|
|
379
|
+
|
|
380
|
+
expect(plan.updated).toEqual(['DATABASE_URL']);
|
|
381
|
+
expect(plan.appended).toEqual([]);
|
|
382
|
+
expect(plan.diff.some((line) => line.kind === 'removed' && line.text.includes('postgres://old'))).toBe(true);
|
|
383
|
+
expect(plan.diff.some((line) => line.kind === 'added' && line.text.includes('db.example.com'))).toBe(true);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it('appends into a managed block when the project has no such key', async () => {
|
|
387
|
+
await fs.writeFile(path.join(root, '.env'), 'APP=1\n');
|
|
388
|
+
|
|
389
|
+
const plan = await dbClientEnv.plan(connection(), scope, request());
|
|
390
|
+
expect(plan.appended).toEqual(['DATABASE_URL']);
|
|
391
|
+
expect(plan.updated).toEqual([]);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('reports a tracked file rather than silently writing one', async () => {
|
|
395
|
+
await fs.writeFile(path.join(root, '.env.production'), 'APP=1\n');
|
|
396
|
+
await execGit(['add', '.env.production'], root);
|
|
397
|
+
await execGit(['commit', '-m', 'add env'], root);
|
|
398
|
+
|
|
399
|
+
const plan = await dbClientEnv.plan(connection(), scope, request({ fileName: '.env.production' }));
|
|
400
|
+
expect(plan.fileTracked).toBe(true);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('describes the database in scope, not the connection\u2019s empty one', async () => {
|
|
404
|
+
// A connection with no database of its own browses one, and it is THAT one
|
|
405
|
+
// the project needs a URL for. The connection's own empty field would
|
|
406
|
+
// produce a string pointing at the server with no database on the end.
|
|
407
|
+
const plan = await dbClientEnv.plan(
|
|
408
|
+
connection({ database: null }),
|
|
409
|
+
scope,
|
|
410
|
+
request({ database: 'analytics', fileName: '' })
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
expect(plan.vars[0].value).toContain('/analytics');
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('renders a pasteable fragment even with no file chosen', async () => {
|
|
417
|
+
const plan = await dbClientEnv.plan(connection(), scope, request({ fileName: '' }));
|
|
418
|
+
|
|
419
|
+
expect(plan.fileName).toBe(null);
|
|
420
|
+
expect(plan.text.trim()).toBe(
|
|
421
|
+
'DATABASE_URL=postgresql://app:s3cr3t@db.example.com:5432/shop?sslmode=require'
|
|
422
|
+
);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('refuses a prefix that is not a legal variable name', async () => {
|
|
426
|
+
await expect(
|
|
427
|
+
dbClientEnv.plan(connection(), scope, request({ prefix: 'my-db' }))
|
|
428
|
+
).rejects.toThrow('valid variable prefix');
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('refuses a shape it cannot write', async () => {
|
|
432
|
+
await expect(
|
|
433
|
+
dbClientEnv.plan(connection(), scope, request({ shape: 'framework' }))
|
|
434
|
+
).rejects.toThrow('is not a shape');
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
describe('dbClientEnv.apply and remove', () => {
|
|
439
|
+
it('writes, then puts the previous value back', async () => {
|
|
440
|
+
await fs.writeFile(path.join(root, '.env'), 'DATABASE_URL=postgres://mine\nAPP=1\n');
|
|
441
|
+
|
|
442
|
+
const applied = await dbClientEnv.apply(connection(), scope, request());
|
|
443
|
+
expect(applied.status).toBe('written');
|
|
444
|
+
expect(applied.updated).toEqual(['DATABASE_URL']);
|
|
445
|
+
|
|
446
|
+
let contents = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
447
|
+
expect(contents).toContain('db.example.com');
|
|
448
|
+
expect(contents).toContain('APP=1');
|
|
449
|
+
|
|
450
|
+
const removed = await dbClientEnv.remove(connection(), scope, request());
|
|
451
|
+
expect(removed.status).toBe('written');
|
|
452
|
+
|
|
453
|
+
contents = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
454
|
+
expect(contents).toContain('DATABASE_URL=postgres://mine');
|
|
455
|
+
expect(contents).not.toContain('db.example.com');
|
|
456
|
+
expect(contents).toContain('APP=1');
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it('leaves a value the user edited after applying', async () => {
|
|
460
|
+
await dbClientEnv.apply(connection(), scope, request());
|
|
461
|
+
const target = path.join(root, '.env');
|
|
462
|
+
const edited = (await fs.readFile(target, 'utf-8')).replace('db.example.com', 'their-host');
|
|
463
|
+
await fs.writeFile(target, edited);
|
|
464
|
+
|
|
465
|
+
await dbClientEnv.remove(connection(), scope, request());
|
|
466
|
+
|
|
467
|
+
expect(await fs.readFile(target, 'utf-8')).toContain('their-host');
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('refuses a tracked file until the caller says otherwise', async () => {
|
|
471
|
+
await fs.writeFile(path.join(root, '.env.production'), 'APP=1\n');
|
|
472
|
+
await execGit(['add', '.env.production'], root);
|
|
473
|
+
await execGit(['commit', '-m', 'add env'], root);
|
|
474
|
+
|
|
475
|
+
const refused = await dbClientEnv.apply(
|
|
476
|
+
connection(),
|
|
477
|
+
scope,
|
|
478
|
+
request({ fileName: '.env.production' })
|
|
479
|
+
);
|
|
480
|
+
expect(refused.status).toBe('skipped-tracked');
|
|
481
|
+
expect(await fs.readFile(path.join(root, '.env.production'), 'utf-8')).toBe('APP=1\n');
|
|
482
|
+
|
|
483
|
+
const allowed = await dbClientEnv.apply(
|
|
484
|
+
connection(),
|
|
485
|
+
scope,
|
|
486
|
+
request({ fileName: '.env.production', allowTracked: true })
|
|
487
|
+
);
|
|
488
|
+
expect(allowed.status).toBe('written');
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it('creates the file when the project has none', async () => {
|
|
492
|
+
const applied = await dbClientEnv.apply(connection(), scope, request());
|
|
493
|
+
|
|
494
|
+
expect(applied.status).toBe('written');
|
|
495
|
+
expect(await fs.readFile(path.join(root, '.env'), 'utf-8')).toContain('DATABASE_URL=');
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it('keeps two connections in one plain file', async () => {
|
|
499
|
+
// A project with a Postgres and a Redis connection is ordinary, and the
|
|
500
|
+
// file has to stay one a human would have written: no markers at all, and
|
|
501
|
+
// applying the second must not disturb the first.
|
|
502
|
+
const cache = connection({
|
|
503
|
+
id: 'conn-redis',
|
|
504
|
+
name: 'Cache',
|
|
505
|
+
driver: 'redis',
|
|
506
|
+
database: '0',
|
|
507
|
+
port: 6379
|
|
508
|
+
});
|
|
509
|
+
const cacheRequest = {
|
|
510
|
+
connectionId: 'conn-redis',
|
|
511
|
+
shape: 'url',
|
|
512
|
+
prefix: 'REDIS',
|
|
513
|
+
fileName: '.env'
|
|
514
|
+
} as Parameters<typeof dbClientEnv.plan>[2];
|
|
515
|
+
|
|
516
|
+
await dbClientEnv.apply(connection(), scope, request());
|
|
517
|
+
await dbClientEnv.apply(cache, scope, cacheRequest);
|
|
518
|
+
|
|
519
|
+
const contents = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
520
|
+
expect(contents).toContain('DATABASE_URL=');
|
|
521
|
+
expect(contents).toContain('REDIS_URL=');
|
|
522
|
+
// Nothing announcing itself. What a dotenv file should look like after
|
|
523
|
+
// this feature runs is a dotenv file with the variables in it.
|
|
524
|
+
expect(contents).not.toContain('clopen:db-client');
|
|
525
|
+
expect(contents).not.toContain('>>>');
|
|
526
|
+
|
|
527
|
+
// Undoing one leaves the other, which is the whole reason removal is
|
|
528
|
+
// per-key rather than per-block.
|
|
529
|
+
await dbClientEnv.remove(cache, scope, cacheRequest);
|
|
530
|
+
const after = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
531
|
+
expect(after).toContain('DATABASE_URL=');
|
|
532
|
+
expect(after).not.toContain('REDIS_URL=');
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it('reports a block that already says exactly this as unchanged', async () => {
|
|
536
|
+
// The panel shows "Undo write" off this, so an apply that wrote nothing
|
|
537
|
+
// must not look like one that did.
|
|
538
|
+
await dbClientEnv.apply(connection(), scope, request());
|
|
539
|
+
const plan = await dbClientEnv.plan(connection(), scope, request());
|
|
540
|
+
|
|
541
|
+
expect(plan.unchanged).toEqual(['DATABASE_URL']);
|
|
542
|
+
expect(plan.appended).toEqual([]);
|
|
543
|
+
expect(plan.diff).toEqual([]);
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
describe('diffLines', () => {
|
|
548
|
+
it('marks what went and what arrived', () => {
|
|
549
|
+
expect(diffLines('A=1\nB=2', 'A=1\nB=3')).toEqual([
|
|
550
|
+
{ kind: 'context', text: 'A=1' },
|
|
551
|
+
{ kind: 'removed', text: 'B=2' },
|
|
552
|
+
{ kind: 'added', text: 'B=3' }
|
|
553
|
+
]);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it('collapses a long unchanged run instead of dropping it silently', () => {
|
|
557
|
+
const before = Array.from({ length: 20 }, (_, i) => `K${i}=1`).join('\n');
|
|
558
|
+
const after = `${before}\nNEW=1`;
|
|
559
|
+
|
|
560
|
+
const diff = diffLines(before, after);
|
|
561
|
+
expect(diff.filter((line) => line.text === '…')).toHaveLength(1);
|
|
562
|
+
expect(diff.at(-1)).toEqual({ kind: 'added', text: 'NEW=1' });
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it('handles a file that did not exist, without a phantom blank line', () => {
|
|
566
|
+
expect(diffLines('', 'A=1\n')).toEqual([{ kind: 'added', text: 'A=1' }]);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it('answers empty when nothing changed', () => {
|
|
570
|
+
// An ellipsis standing in for the whole file reads as "something is
|
|
571
|
+
// hidden here"; the honest rendering of no change is no diff.
|
|
572
|
+
expect(diffLines('A=1\nB=2\n', 'A=1\nB=2\n')).toEqual([]);
|
|
573
|
+
});
|
|
574
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DB Client → environment variables.
|
|
3
|
+
*
|
|
4
|
+
* naming.ts the two shapes, and the driver words that are not framework words
|
|
5
|
+
* detect.ts what the project already calls it, which wins over both
|
|
6
|
+
* diff.ts the preview, because this edits someone's own file
|
|
7
|
+
* service.ts detect, preview, apply, undo
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type { EnvScope } from './service';
|
|
11
|
+
export { dbClientEnv } from './service';
|
|
12
|
+
export type { EnvNaming, EnvRenderInput } from './naming';
|
|
13
|
+
export { defaultPrefixFor, keyFor, renderEnvVars, shapeInfos } from './naming';
|
|
14
|
+
export { detectEnvUsage } from './detect';
|
|
15
|
+
export { diffLines } from './diff';
|