postgresai 0.14.0-dev.7 → 0.14.0-dev.70
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 +161 -61
- package/bin/postgres-ai.ts +1957 -404
- package/bun.lock +258 -0
- package/bunfig.toml +20 -0
- package/dist/bin/postgres-ai.js +29351 -1576
- package/dist/sql/01.role.sql +16 -0
- package/dist/sql/02.permissions.sql +37 -0
- package/dist/sql/03.optional_rds.sql +6 -0
- package/dist/sql/04.optional_self_managed.sql +8 -0
- package/dist/sql/05.helpers.sql +439 -0
- package/dist/sql/sql/01.role.sql +16 -0
- package/dist/sql/sql/02.permissions.sql +37 -0
- package/dist/sql/sql/03.optional_rds.sql +6 -0
- package/dist/sql/sql/04.optional_self_managed.sql +8 -0
- package/dist/sql/sql/05.helpers.sql +439 -0
- package/lib/auth-server.ts +124 -106
- package/lib/checkup-api.ts +386 -0
- package/lib/checkup.ts +1396 -0
- package/lib/config.ts +6 -3
- package/lib/init.ts +512 -156
- package/lib/issues.ts +400 -191
- package/lib/mcp-server.ts +213 -90
- package/lib/metrics-embedded.ts +79 -0
- package/lib/metrics-loader.ts +127 -0
- package/lib/supabase.ts +769 -0
- package/lib/util.ts +61 -0
- package/package.json +20 -10
- package/packages/postgres-ai/README.md +26 -0
- package/packages/postgres-ai/bin/postgres-ai.js +27 -0
- package/packages/postgres-ai/package.json +27 -0
- package/scripts/embed-metrics.ts +154 -0
- package/sql/01.role.sql +16 -0
- package/sql/02.permissions.sql +37 -0
- package/sql/03.optional_rds.sql +6 -0
- package/sql/04.optional_self_managed.sql +8 -0
- package/sql/05.helpers.sql +439 -0
- package/test/auth.test.ts +258 -0
- package/test/checkup.integration.test.ts +321 -0
- package/test/checkup.test.ts +1117 -0
- package/test/init.integration.test.ts +500 -0
- package/test/init.test.ts +527 -0
- package/test/issues.cli.test.ts +314 -0
- package/test/issues.test.ts +456 -0
- package/test/mcp-server.test.ts +988 -0
- package/test/schema-validation.test.ts +81 -0
- package/test/supabase.test.ts +568 -0
- package/test/test-utils.ts +128 -0
- package/tsconfig.json +12 -20
- package/dist/bin/postgres-ai.d.ts +0 -3
- package/dist/bin/postgres-ai.d.ts.map +0 -1
- package/dist/bin/postgres-ai.js.map +0 -1
- package/dist/lib/auth-server.d.ts +0 -31
- package/dist/lib/auth-server.d.ts.map +0 -1
- package/dist/lib/auth-server.js +0 -263
- package/dist/lib/auth-server.js.map +0 -1
- package/dist/lib/config.d.ts +0 -45
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js +0 -181
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/init.d.ts +0 -61
- package/dist/lib/init.d.ts.map +0 -1
- package/dist/lib/init.js +0 -359
- package/dist/lib/init.js.map +0 -1
- package/dist/lib/issues.d.ts +0 -75
- package/dist/lib/issues.d.ts.map +0 -1
- package/dist/lib/issues.js +0 -336
- package/dist/lib/issues.js.map +0 -1
- package/dist/lib/mcp-server.d.ts +0 -9
- package/dist/lib/mcp-server.d.ts.map +0 -1
- package/dist/lib/mcp-server.js +0 -168
- package/dist/lib/mcp-server.js.map +0 -1
- package/dist/lib/pkce.d.ts +0 -32
- package/dist/lib/pkce.d.ts.map +0 -1
- package/dist/lib/pkce.js +0 -101
- package/dist/lib/pkce.js.map +0 -1
- package/dist/lib/util.d.ts +0 -27
- package/dist/lib/util.d.ts.map +0 -1
- package/dist/lib/util.js +0 -46
- package/dist/lib/util.js.map +0 -1
- package/dist/package.json +0 -46
- package/test/init.integration.test.cjs +0 -269
- package/test/init.test.cjs +0 -69
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { mkdtempSync } from "fs";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
|
|
6
|
+
function runCli(args: string[], env: Record<string, string> = {}) {
|
|
7
|
+
const cliPath = resolve(import.meta.dir, "..", "bin", "postgres-ai.ts");
|
|
8
|
+
const bunBin = typeof process.execPath === "string" && process.execPath.length > 0 ? process.execPath : "bun";
|
|
9
|
+
const result = Bun.spawnSync([bunBin, cliPath, ...args], {
|
|
10
|
+
env: { ...process.env, ...env },
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
status: result.exitCode,
|
|
14
|
+
stdout: new TextDecoder().decode(result.stdout),
|
|
15
|
+
stderr: new TextDecoder().decode(result.stderr),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function runCliAsync(args: string[], env: Record<string, string> = {}) {
|
|
20
|
+
const cliPath = resolve(import.meta.dir, "..", "bin", "postgres-ai.ts");
|
|
21
|
+
const bunBin = typeof process.execPath === "string" && process.execPath.length > 0 ? process.execPath : "bun";
|
|
22
|
+
const proc = Bun.spawn([bunBin, cliPath, ...args], {
|
|
23
|
+
env: { ...process.env, ...env },
|
|
24
|
+
stdout: "pipe",
|
|
25
|
+
stderr: "pipe",
|
|
26
|
+
});
|
|
27
|
+
const [status, stdout, stderr] = await Promise.all([
|
|
28
|
+
proc.exited,
|
|
29
|
+
new Response(proc.stdout).text(),
|
|
30
|
+
new Response(proc.stderr).text(),
|
|
31
|
+
]);
|
|
32
|
+
return { status, stdout, stderr };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isolatedEnv(extra: Record<string, string> = {}) {
|
|
36
|
+
// Ensure tests do not depend on any real user config on the machine running them.
|
|
37
|
+
const cfgHome = mkdtempSync(resolve(tmpdir(), "postgresai-cli-test-"));
|
|
38
|
+
return {
|
|
39
|
+
XDG_CONFIG_HOME: cfgHome,
|
|
40
|
+
HOME: cfgHome,
|
|
41
|
+
...extra,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function startFakeApi() {
|
|
46
|
+
const requests: Array<{
|
|
47
|
+
method: string;
|
|
48
|
+
pathname: string;
|
|
49
|
+
headers: Record<string, string>;
|
|
50
|
+
bodyText: string;
|
|
51
|
+
bodyJson: any | null;
|
|
52
|
+
}> = [];
|
|
53
|
+
|
|
54
|
+
const server = Bun.serve({
|
|
55
|
+
hostname: "127.0.0.1",
|
|
56
|
+
port: 0,
|
|
57
|
+
async fetch(req) {
|
|
58
|
+
const url = new URL(req.url);
|
|
59
|
+
const headers: Record<string, string> = {};
|
|
60
|
+
for (const [k, v] of req.headers.entries()) headers[k.toLowerCase()] = v;
|
|
61
|
+
|
|
62
|
+
const bodyText = await req.text();
|
|
63
|
+
let bodyJson: any | null = null;
|
|
64
|
+
try {
|
|
65
|
+
bodyJson = bodyText ? JSON.parse(bodyText) : null;
|
|
66
|
+
} catch {
|
|
67
|
+
bodyJson = null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
requests.push({
|
|
71
|
+
method: req.method,
|
|
72
|
+
pathname: url.pathname,
|
|
73
|
+
headers,
|
|
74
|
+
bodyText,
|
|
75
|
+
bodyJson,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Minimal fake PostgREST RPC endpoints used by our CLI.
|
|
79
|
+
if (req.method === "POST" && url.pathname.endsWith("/rpc/issue_create")) {
|
|
80
|
+
return new Response(
|
|
81
|
+
JSON.stringify({
|
|
82
|
+
id: "issue-1",
|
|
83
|
+
title: bodyJson?.title ?? "",
|
|
84
|
+
description: bodyJson?.description ?? null,
|
|
85
|
+
created_at: "2025-01-01T00:00:00Z",
|
|
86
|
+
status: 0,
|
|
87
|
+
project_id: bodyJson?.project_id ?? null,
|
|
88
|
+
labels: bodyJson?.labels ?? null,
|
|
89
|
+
}),
|
|
90
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (req.method === "POST" && url.pathname.endsWith("/rpc/issue_update")) {
|
|
95
|
+
return new Response(
|
|
96
|
+
JSON.stringify({
|
|
97
|
+
id: bodyJson?.p_id ?? "issue-1",
|
|
98
|
+
title: bodyJson?.p_title ?? "unchanged",
|
|
99
|
+
description: bodyJson?.p_description ?? null,
|
|
100
|
+
status: bodyJson?.p_status ?? 0,
|
|
101
|
+
updated_at: "2025-01-02T00:00:00Z",
|
|
102
|
+
labels: bodyJson?.p_labels ?? null,
|
|
103
|
+
}),
|
|
104
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (req.method === "POST" && url.pathname.endsWith("/rpc/issue_comment_update")) {
|
|
109
|
+
return new Response(
|
|
110
|
+
JSON.stringify({
|
|
111
|
+
id: bodyJson?.p_id ?? "comment-1",
|
|
112
|
+
issue_id: "issue-1",
|
|
113
|
+
content: bodyJson?.p_content ?? "",
|
|
114
|
+
updated_at: "2025-01-02T00:00:00Z",
|
|
115
|
+
}),
|
|
116
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (req.method === "POST" && url.pathname.endsWith("/rpc/issue_comment_create")) {
|
|
121
|
+
return new Response(
|
|
122
|
+
JSON.stringify({
|
|
123
|
+
id: "comment-1",
|
|
124
|
+
issue_id: bodyJson?.issue_id ?? "issue-1",
|
|
125
|
+
author_id: 1,
|
|
126
|
+
parent_comment_id: bodyJson?.parent_comment_id ?? null,
|
|
127
|
+
content: bodyJson?.content ?? "",
|
|
128
|
+
created_at: "2025-01-01T00:00:00Z",
|
|
129
|
+
updated_at: "2025-01-01T00:00:00Z",
|
|
130
|
+
data: null,
|
|
131
|
+
}),
|
|
132
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return new Response("not found", { status: 404 });
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const baseUrl = `http://${server.hostname}:${server.port}/api/general`;
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
baseUrl,
|
|
144
|
+
requests,
|
|
145
|
+
stop: () => server.stop(true),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
describe("CLI issues command group", () => {
|
|
150
|
+
test("issues help exposes the canonical subcommands and no legacy names", () => {
|
|
151
|
+
const r = runCli(["issues", "--help"], isolatedEnv());
|
|
152
|
+
expect(r.status).toBe(0);
|
|
153
|
+
|
|
154
|
+
const out = `${r.stdout}\n${r.stderr}`;
|
|
155
|
+
|
|
156
|
+
// Canonical subcommands
|
|
157
|
+
expect(out).toContain("create [options] <title>");
|
|
158
|
+
expect(out).toContain("update [options] <issueId>");
|
|
159
|
+
expect(out).toContain("update-comment [options] <commentId> <content>");
|
|
160
|
+
expect(out).toContain("post-comment [options] <issueId> <content>");
|
|
161
|
+
|
|
162
|
+
// Legacy / removed names
|
|
163
|
+
expect(out).not.toContain("create-issue");
|
|
164
|
+
expect(out).not.toContain("update-issue");
|
|
165
|
+
expect(out).not.toContain("update-issue-comment");
|
|
166
|
+
expect(out).not.toContain("post_comment");
|
|
167
|
+
expect(out).not.toContain("create_issue");
|
|
168
|
+
expect(out).not.toContain("update_issue");
|
|
169
|
+
expect(out).not.toContain("update_issue_comment");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("issues create fails fast when API key is missing", () => {
|
|
173
|
+
const r = runCli(["issues", "create", "Test issue"], isolatedEnv());
|
|
174
|
+
expect(r.status).toBe(1);
|
|
175
|
+
expect(`${r.stdout}\n${r.stderr}`).toContain("API key is required");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("issues create fails fast when org id is missing (no config fallback)", () => {
|
|
179
|
+
const r = runCli(["issues", "create", "Test issue"], isolatedEnv({ PGAI_API_KEY: "test-key" }));
|
|
180
|
+
expect(r.status).toBe(1);
|
|
181
|
+
expect(`${r.stdout}\n${r.stderr}`).toContain("org_id is required");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("issues update fails fast when API key is missing", () => {
|
|
185
|
+
const r = runCli(["issues", "update", "00000000-0000-0000-0000-000000000000", "--title", "New title"], isolatedEnv());
|
|
186
|
+
expect(r.status).toBe(1);
|
|
187
|
+
expect(`${r.stdout}\n${r.stderr}`).toContain("API key is required");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("issues update-comment fails fast when API key is missing", () => {
|
|
191
|
+
const r = runCli(["issues", "update-comment", "00000000-0000-0000-0000-000000000000", "hello"], isolatedEnv());
|
|
192
|
+
expect(r.status).toBe(1);
|
|
193
|
+
expect(`${r.stdout}\n${r.stderr}`).toContain("API key is required");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("issues post-comment fails fast when API key is missing", () => {
|
|
197
|
+
const r = runCli(["issues", "post-comment", "00000000-0000-0000-0000-000000000000", "hello"], isolatedEnv());
|
|
198
|
+
expect(r.status).toBe(1);
|
|
199
|
+
expect(`${r.stdout}\n${r.stderr}`).toContain("API key is required");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("issues create succeeds against a fake API and sends the expected request", async () => {
|
|
203
|
+
const api = await startFakeApi();
|
|
204
|
+
try {
|
|
205
|
+
const r = await runCliAsync(
|
|
206
|
+
["issues", "create", "Hello", "--org-id", "123", "--description", "line1\\nline2", "--label", "a", "--label", "b"],
|
|
207
|
+
isolatedEnv({
|
|
208
|
+
PGAI_API_KEY: "test-key",
|
|
209
|
+
PGAI_API_BASE_URL: api.baseUrl,
|
|
210
|
+
})
|
|
211
|
+
);
|
|
212
|
+
expect(r.status).toBe(0);
|
|
213
|
+
|
|
214
|
+
const out = JSON.parse(r.stdout.trim());
|
|
215
|
+
expect(out.id).toBe("issue-1");
|
|
216
|
+
expect(out.title).toBe("Hello");
|
|
217
|
+
expect(out.description).toBe("line1\nline2");
|
|
218
|
+
expect(out.labels).toEqual(["a", "b"]);
|
|
219
|
+
|
|
220
|
+
const req = api.requests.find((x) => x.pathname.endsWith("/rpc/issue_create"));
|
|
221
|
+
expect(req).toBeTruthy();
|
|
222
|
+
expect(req!.headers["access-token"]).toBe("test-key");
|
|
223
|
+
expect(req!.method).toBe("POST");
|
|
224
|
+
expect(req!.bodyJson.org_id).toBe(123);
|
|
225
|
+
expect(req!.bodyJson.title).toBe("Hello");
|
|
226
|
+
expect(req!.bodyJson.description).toBe("line1\nline2");
|
|
227
|
+
expect(req!.bodyJson.labels).toEqual(["a", "b"]);
|
|
228
|
+
} finally {
|
|
229
|
+
api.stop();
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("issues update succeeds against a fake API (including status mapping)", async () => {
|
|
234
|
+
const api = await startFakeApi();
|
|
235
|
+
try {
|
|
236
|
+
const r = await runCliAsync(
|
|
237
|
+
["issues", "update", "issue-1", "--title", "New title", "--status", "closed"],
|
|
238
|
+
isolatedEnv({
|
|
239
|
+
PGAI_API_KEY: "test-key",
|
|
240
|
+
PGAI_API_BASE_URL: api.baseUrl,
|
|
241
|
+
})
|
|
242
|
+
);
|
|
243
|
+
expect(r.status).toBe(0);
|
|
244
|
+
|
|
245
|
+
const out = JSON.parse(r.stdout.trim());
|
|
246
|
+
expect(out.id).toBe("issue-1");
|
|
247
|
+
expect(out.title).toBe("New title");
|
|
248
|
+
expect(out.status).toBe(1);
|
|
249
|
+
|
|
250
|
+
const req = api.requests.find((x) => x.pathname.endsWith("/rpc/issue_update"));
|
|
251
|
+
expect(req).toBeTruthy();
|
|
252
|
+
expect(req!.headers["access-token"]).toBe("test-key");
|
|
253
|
+
expect(req!.bodyJson.p_id).toBe("issue-1");
|
|
254
|
+
expect(req!.bodyJson.p_title).toBe("New title");
|
|
255
|
+
expect(req!.bodyJson.p_status).toBe(1);
|
|
256
|
+
} finally {
|
|
257
|
+
api.stop();
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("issues update-comment succeeds against a fake API and decodes escapes", async () => {
|
|
262
|
+
const api = await startFakeApi();
|
|
263
|
+
try {
|
|
264
|
+
const r = await runCliAsync(
|
|
265
|
+
["issues", "update-comment", "comment-1", "hello\\nworld"],
|
|
266
|
+
isolatedEnv({
|
|
267
|
+
PGAI_API_KEY: "test-key",
|
|
268
|
+
PGAI_API_BASE_URL: api.baseUrl,
|
|
269
|
+
})
|
|
270
|
+
);
|
|
271
|
+
expect(r.status).toBe(0);
|
|
272
|
+
|
|
273
|
+
const out = JSON.parse(r.stdout.trim());
|
|
274
|
+
expect(out.id).toBe("comment-1");
|
|
275
|
+
expect(out.content).toBe("hello\nworld");
|
|
276
|
+
|
|
277
|
+
const req = api.requests.find((x) => x.pathname.endsWith("/rpc/issue_comment_update"));
|
|
278
|
+
expect(req).toBeTruthy();
|
|
279
|
+
expect(req!.headers["access-token"]).toBe("test-key");
|
|
280
|
+
expect(req!.bodyJson.p_id).toBe("comment-1");
|
|
281
|
+
expect(req!.bodyJson.p_content).toBe("hello\nworld");
|
|
282
|
+
} finally {
|
|
283
|
+
api.stop();
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("issues post-comment succeeds against a fake API and decodes escapes", async () => {
|
|
288
|
+
const api = await startFakeApi();
|
|
289
|
+
try {
|
|
290
|
+
const r = await runCliAsync(
|
|
291
|
+
["issues", "post-comment", "issue-1", "hello\\nworld"],
|
|
292
|
+
isolatedEnv({
|
|
293
|
+
PGAI_API_KEY: "test-key",
|
|
294
|
+
PGAI_API_BASE_URL: api.baseUrl,
|
|
295
|
+
})
|
|
296
|
+
);
|
|
297
|
+
expect(r.status).toBe(0);
|
|
298
|
+
|
|
299
|
+
const out = JSON.parse(r.stdout.trim());
|
|
300
|
+
expect(out.id).toBe("comment-1");
|
|
301
|
+
expect(out.issue_id).toBe("issue-1");
|
|
302
|
+
expect(out.content).toBe("hello\nworld");
|
|
303
|
+
|
|
304
|
+
const req = api.requests.find((x) => x.pathname.endsWith("/rpc/issue_comment_create"));
|
|
305
|
+
expect(req).toBeTruthy();
|
|
306
|
+
expect(req!.headers["access-token"]).toBe("test-key");
|
|
307
|
+
expect(req!.bodyJson.issue_id).toBe("issue-1");
|
|
308
|
+
expect(req!.bodyJson.content).toBe("hello\nworld");
|
|
309
|
+
} finally {
|
|
310
|
+
api.stop();
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|