postgresai 0.15.0-dev.1 → 0.15.0-dev.10

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.
@@ -259,3 +259,81 @@ describe("registerMonitoringInstance", () => {
259
259
  expect(fetchCalls[0].url).toBe("https://custom.api.com/v2/rpc/monitoring_instance_register");
260
260
  });
261
261
  });
262
+
263
+ describe("demo mode instances.demo.yml", () => {
264
+ const repoRoot = path.resolve(import.meta.dir, "..", "..");
265
+
266
+ test("instances.demo.yml exists in repo root", () => {
267
+ const demoFile = path.join(repoRoot, "instances.demo.yml");
268
+ expect(fs.existsSync(demoFile)).toBe(true);
269
+ });
270
+
271
+ test("instances.demo.yml contains demo target connection", () => {
272
+ const demoFile = path.join(repoRoot, "instances.demo.yml");
273
+ const content = fs.readFileSync(demoFile, "utf8");
274
+ expect(content).toContain("name: target_database");
275
+ expect(content).toContain("conn_str: postgresql://monitor:monitor_pass@target-db:5432/target_database");
276
+ expect(content).toContain("is_enabled: true");
277
+ expect(content).toContain("preset_metrics: full");
278
+ });
279
+
280
+ test("instances.demo.yml has required YAML structure", () => {
281
+ const demoFile = path.join(repoRoot, "instances.demo.yml");
282
+ const content = fs.readFileSync(demoFile, "utf8");
283
+ // Verify it's a YAML list (starts with "- name:")
284
+ expect(content).toMatch(/^- name: target_database/m);
285
+ // Verify required fields are present with correct indentation
286
+ expect(content).toMatch(/^\s+conn_str:/m);
287
+ expect(content).toMatch(/^\s+preset_metrics: full/m);
288
+ expect(content).toMatch(/^\s+is_enabled: true/m);
289
+ // ~sink_type~ is a sed token substituted by generate-pgwatch-sources.sh; values: postgres, prometheus
290
+ expect(content).toMatch(/^\s+sink_type: ~sink_type~/m);
291
+ });
292
+
293
+ test("instances.yml is gitignored (not tracked)", () => {
294
+ const gitignore = fs.readFileSync(path.join(repoRoot, ".gitignore"), "utf8");
295
+ expect(gitignore).toMatch(/^instances\.yml$/m);
296
+ });
297
+
298
+ test("demo config can be copied to instances.yml in temp dir", () => {
299
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "demo-install-test-"));
300
+ try {
301
+ const demoSrc = path.join(repoRoot, "instances.demo.yml");
302
+ const instancesDest = path.join(tempDir, "instances.yml");
303
+
304
+ fs.copyFileSync(demoSrc, instancesDest);
305
+
306
+ expect(fs.existsSync(instancesDest)).toBe(true);
307
+ const content = fs.readFileSync(instancesDest, "utf8");
308
+ expect(content).toContain("name: target_database");
309
+ expect(content).toContain("conn_str: postgresql://monitor:monitor_pass@target-db:5432/target_database");
310
+ } finally {
311
+ fs.rmSync(tempDir, { recursive: true, force: true });
312
+ }
313
+ });
314
+
315
+ test("demo config copy overwrites directory at instances.yml path", () => {
316
+ // Docker bind-mounts create missing paths as directories; the copy must handle this
317
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "demo-eisdir-test-"));
318
+ try {
319
+ const demoSrc = path.join(repoRoot, "instances.demo.yml");
320
+ const instancesDest = path.join(tempDir, "instances.yml");
321
+
322
+ // Simulate Docker creating a directory at instances.yml path
323
+ fs.mkdirSync(instancesDest);
324
+ expect(fs.statSync(instancesDest).isDirectory()).toBe(true);
325
+
326
+ // The fix: remove directory then copy
327
+ if (fs.statSync(instancesDest).isDirectory()) {
328
+ fs.rmSync(instancesDest, { recursive: true, force: true });
329
+ }
330
+ fs.copyFileSync(demoSrc, instancesDest);
331
+
332
+ expect(fs.statSync(instancesDest).isFile()).toBe(true);
333
+ const content = fs.readFileSync(instancesDest, "utf8");
334
+ expect(content).toContain("name: target_database");
335
+ } finally {
336
+ fs.rmSync(tempDir, { recursive: true, force: true });
337
+ }
338
+ });
339
+ });
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Test the SQL logic for checking postgres_ai.pg_statistic view existence
3
+ * across different permission scenarios.
4
+ */
5
+ import { describe, test, expect } from "bun:test";
6
+
7
+ describe("postgres_ai.pg_statistic permission check SQL", () => {
8
+ test("to_regclass() returns NULL when schema doesn't exist", () => {
9
+ // Simulate the SQL check behavior
10
+ const viewExists = null; // to_regclass('postgres_ai.pg_statistic') when schema doesn't exist
11
+ const granted = viewExists !== null;
12
+
13
+ expect(granted).toBe(false);
14
+ });
15
+
16
+ test("to_regclass() returns NULL when user lacks USAGE on schema", () => {
17
+ // When user lacks USAGE on postgres_ai schema, to_regclass() returns NULL
18
+ // even if the schema and view exist
19
+ const viewExists = null; // to_regclass('postgres_ai.pg_statistic') when no USAGE
20
+ const granted = viewExists !== null;
21
+
22
+ expect(granted).toBe(false);
23
+ });
24
+
25
+ test("to_regclass() returns oid when view exists and user has access", () => {
26
+ // When user has USAGE on schema and view exists
27
+ const viewExists = 12345; // to_regclass('postgres_ai.pg_statistic') returns oid
28
+ const granted = viewExists !== null;
29
+
30
+ expect(granted).toBe(true);
31
+ });
32
+
33
+ test("has_table_privilege is skipped (returns null) when view doesn't exist", () => {
34
+ const viewExists = null;
35
+ const selectGranted = viewExists === null ? null : true; // skipped
36
+
37
+ expect(selectGranted).toBeNull();
38
+ });
39
+
40
+ test("has_table_privilege is checked when view exists", () => {
41
+ const viewExists = 12345;
42
+ const userHasSelect = true;
43
+ const selectGranted = viewExists === null ? null : userHasSelect;
44
+
45
+ expect(selectGranted).toBe(true);
46
+ });
47
+ });
48
+
49
+ describe("Expected behavior per scenario", () => {
50
+ test("Scenario 1: Superuser with postgres_ai.pg_statistic", () => {
51
+ // to_regclass returns oid, has_table_privilege returns true
52
+ const checkViewExists = true; // to_regclass('postgres_ai.pg_statistic') is not null
53
+ const checkSelectPrivilege = true; // has_table_privilege returns true
54
+
55
+ const missingOptional: string[] = [];
56
+ if (!checkViewExists) {
57
+ missingOptional.push("postgres_ai.pg_statistic view exists");
58
+ }
59
+ if (checkSelectPrivilege === false) {
60
+ missingOptional.push("select on postgres_ai.pg_statistic");
61
+ }
62
+
63
+ expect(missingOptional).toHaveLength(0);
64
+ });
65
+
66
+ test("Scenario 2: pg_monitor, no postgres_ai schema access (before prepare-db)", () => {
67
+ // to_regclass returns NULL because user lacks USAGE on postgres_ai schema
68
+ const checkViewExists = false; // to_regclass('postgres_ai.pg_statistic') is null
69
+ const checkSelectPrivilege = null; // skipped because view doesn't exist
70
+
71
+ const missingOptional: string[] = [];
72
+ if (!checkViewExists) {
73
+ missingOptional.push("postgres_ai.pg_statistic view exists");
74
+ }
75
+ if (checkSelectPrivilege === false) {
76
+ missingOptional.push("select on postgres_ai.pg_statistic");
77
+ }
78
+
79
+ // Should show warning about missing view but NOT crash
80
+ expect(missingOptional).toEqual(["postgres_ai.pg_statistic view exists"]);
81
+ });
82
+
83
+ test("Scenario 3: No pg_monitor (before prepare-db)", () => {
84
+ // to_regclass returns NULL because schema doesn't exist yet
85
+ const checkViewExists = false; // to_regclass('postgres_ai.pg_statistic') is null
86
+ const checkSelectPrivilege = null; // skipped
87
+
88
+ const missingOptional: string[] = [];
89
+ if (!checkViewExists) {
90
+ missingOptional.push("postgres_ai.pg_statistic view exists");
91
+ }
92
+ if (checkSelectPrivilege === false) {
93
+ missingOptional.push("select on postgres_ai.pg_statistic");
94
+ }
95
+
96
+ // Should show warning but NOT crash
97
+ expect(missingOptional).toEqual(["postgres_ai.pg_statistic view exists"]);
98
+ });
99
+
100
+ test("Scenario 8: After prepare-db with schema grants", () => {
101
+ // to_regclass returns oid, has_table_privilege returns true
102
+ const checkViewExists = true; // to_regclass('postgres_ai.pg_statistic') is not null
103
+ const checkSelectPrivilege = true; // has_table_privilege returns true
104
+
105
+ const missingOptional: string[] = [];
106
+ if (!checkViewExists) {
107
+ missingOptional.push("postgres_ai.pg_statistic view exists");
108
+ }
109
+ if (checkSelectPrivilege === false) {
110
+ missingOptional.push("select on postgres_ai.pg_statistic");
111
+ }
112
+
113
+ // Should be clean, no warnings
114
+ expect(missingOptional).toHaveLength(0);
115
+ });
116
+ });