@malloy-publisher/server 0.0.167 → 0.0.168

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.
Files changed (29) hide show
  1. package/.eslintrc.json +9 -1
  2. package/dist/app/api-doc.yaml +36 -1
  3. package/dist/app/assets/HomePage-D2tUw_9U.js +1 -0
  4. package/dist/app/assets/{MainPage-C9Fr5IN8.js → MainPage-DBQW76L7.js} +2 -2
  5. package/dist/app/assets/{ModelPage-BkU6HAHA.js → ModelPage-BnfOKuhQ.js} +1 -1
  6. package/dist/app/assets/PackagePage-zPhE-rDg.js +1 -0
  7. package/dist/app/assets/ProjectPage-BpSTvuW6.js +1 -0
  8. package/dist/app/assets/RouteError-Cp9-yCK5.js +1 -0
  9. package/dist/app/assets/{WorkbookPage-D3rUQZj6.js → WorkbookPage-FD_gmxeE.js} +1 -1
  10. package/dist/app/assets/{index-BLxl0XLH.js → index-D5QBYuLK.js} +150 -150
  11. package/dist/app/assets/{index-lhDwptrQ.js → index-DNCvL_5f.js} +1 -1
  12. package/dist/app/assets/{index-hkABoiMV.js → index-x9S1fsYn.js} +1 -1
  13. package/dist/app/assets/{index.umd-BkXQ-YAe.js → index.umd-CTYdFEHH.js} +1 -1
  14. package/dist/app/index.html +1 -1
  15. package/dist/server.js +261 -27
  16. package/package.json +1 -1
  17. package/src/controller/connection.controller.ts +22 -2
  18. package/src/server.ts +5 -1
  19. package/src/service/connection.spec.ts +105 -0
  20. package/src/service/connection.ts +293 -17
  21. package/src/service/db_utils.ts +85 -4
  22. package/src/service/project.ts +20 -3
  23. package/tests/harness/mcp_test_setup.ts +166 -26
  24. package/tests/unit/duckdb/attached_databases.test.ts +61 -3
  25. package/tests/unit/ducklake/ducklake.test.ts +950 -0
  26. package/dist/app/assets/HomePage-D76UaGFV.js +0 -1
  27. package/dist/app/assets/PackagePage-BhE9Wi7b.js +0 -1
  28. package/dist/app/assets/ProjectPage-BatZLVap.js +0 -1
  29. package/dist/app/assets/RouteError-Bo5zJ8Xa.js +0 -1
@@ -16,22 +16,51 @@ import { URL } from "url";
16
16
 
17
17
  // --- E2E Test Environment Setup ---
18
18
 
19
- // Store the original SERVER_ROOT to restore it later.
20
- let originalServerRoot: string | undefined;
21
-
22
19
  export interface McpE2ETestEnvironment {
23
20
  httpServer: http.Server;
24
21
  serverUrl: string;
25
22
  mcpClient: Client<Request, Notification, Result>;
23
+ originalServerRoot: string | undefined;
24
+ originalInitializeStorage: string | undefined;
26
25
  }
27
26
 
27
+ // Counter for unique port assignment per test suite
28
+ let portCounter = 0;
29
+
30
+ // Mutex to prevent concurrent initialization (since all tests share the same database)
31
+ let initializationLock: Promise<void> | null = null;
32
+
28
33
  /**
29
34
  * Starts the real application server and connects a real MCP client.
30
35
  */
31
36
  export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment> {
37
+ // Wait for any ongoing initialization to complete (prevents concurrent DB initialization)
38
+ if (initializationLock) {
39
+ console.log(
40
+ "[E2E Test Setup] Waiting for previous initialization to complete...",
41
+ );
42
+ await initializationLock;
43
+ }
44
+
45
+ // Create a new lock for this initialization
46
+ let resolveLock: () => void;
47
+ initializationLock = new Promise((resolve) => {
48
+ resolveLock = resolve;
49
+ });
50
+
51
+ try {
52
+ return await setupE2ETestEnvironmentInternal();
53
+ } finally {
54
+ // Release the lock
55
+ resolveLock!();
56
+ initializationLock = null;
57
+ }
58
+ }
59
+
60
+ async function setupE2ETestEnvironmentInternal(): Promise<McpE2ETestEnvironment> {
32
61
  // --- Store and Set SERVER_ROOT Env Var ---
33
62
  // The ProjectStore relies on SERVER_ROOT to find publisher.config.json.
34
- originalServerRoot = process.env.SERVER_ROOT; // Store original value
63
+ const originalServerRoot = process.env.SERVER_ROOT; // Store original value
35
64
  // Resolve the path to 'packages/server' based on the location of this file
36
65
  // Use import.meta.url for cross-platform compatibility (works on Windows)
37
66
  const __filename = fileURLToPath(import.meta.url);
@@ -42,6 +71,14 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
42
71
  `[E2E Test Setup] Temporarily set SERVER_ROOT=${process.env.SERVER_ROOT}`,
43
72
  );
44
73
 
74
+ // --- Set INITIALIZE_STORAGE to ensure packages are downloaded ---
75
+ // This ensures packages from GitHub are cloned/downloaded during initialization
76
+ const originalInitializeStorage = process.env.INITIALIZE_STORAGE; // Store original value
77
+ process.env.INITIALIZE_STORAGE = "true";
78
+ console.log(
79
+ `[E2E Test Setup] Temporarily set INITIALIZE_STORAGE=${process.env.INITIALIZE_STORAGE}`,
80
+ );
81
+
45
82
  // --- IMPORTANT: Import server *after* setting env var ---
46
83
  // Dynamically import the actual app instance
47
84
  const { mcpApp } = await import("../../src/server");
@@ -49,8 +86,10 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
49
86
  let serverInstance: http.Server;
50
87
  let serverUrl: string;
51
88
 
52
- // Define an explicit port for the test server to avoid conflicts
53
- const TEST_MCP_PORT = Number(process.env.MCP_PORT || 4040) + 2; // e.g., 4042
89
+ // Use unique port per test suite to avoid conflicts when running in parallel
90
+ // Increment port counter and use it to create unique ports: 4042, 4043, 4044, etc.
91
+ const portOffset = portCounter++;
92
+ const TEST_MCP_PORT = Number(process.env.MCP_PORT || 4040) + 2 + portOffset;
54
93
 
55
94
  await new Promise<void>((resolve, reject) => {
56
95
  const server = http
@@ -84,6 +123,72 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
84
123
  const listeningServerInstance = serverInstance!;
85
124
  const listeningServerUrl = serverUrl!;
86
125
 
126
+ // --- Wait a moment for server to start accepting connections ---
127
+ console.log("[E2E Test Setup] Waiting for server to accept connections...");
128
+ await new Promise((resolve) => setTimeout(resolve, 1000));
129
+
130
+ // --- Wait for server to be ready (packages downloaded) ---
131
+ // Poll the readiness endpoint to ensure initialization completes before tests run
132
+ // Note: Reduced timeout to 90s to be under the 100s test timeout
133
+ console.log("[E2E Test Setup] Waiting for server to be ready...");
134
+ const maxWaitTime = 90000; // 90 seconds max wait (under 100s test timeout)
135
+ const pollInterval = 1000; // Check every second
136
+ const startTime = Date.now();
137
+ let isReady = false;
138
+
139
+ while (!isReady && Date.now() - startTime < maxWaitTime) {
140
+ try {
141
+ // Use Promise.race to add timeout to fetch
142
+ const fetchPromise = fetch(`${listeningServerUrl}/health/readiness`);
143
+ const timeoutPromise = new Promise<never>((_, reject) =>
144
+ setTimeout(() => reject(new Error("Fetch timeout")), 5000),
145
+ );
146
+ const response = await Promise.race([fetchPromise, timeoutPromise]);
147
+
148
+ if (response.ok) {
149
+ const data = await response.json();
150
+ if (data.status === "UP") {
151
+ isReady = true;
152
+ console.log("[E2E Test Setup] Server is ready.");
153
+ break;
154
+ } else {
155
+ console.log(
156
+ `[E2E Test Setup] Server not ready yet (status: ${data.status}), waiting... (${Math.round((Date.now() - startTime) / 1000)}s)`,
157
+ );
158
+ }
159
+ } else {
160
+ console.log(
161
+ `[E2E Test Setup] Readiness check returned ${response.status}, waiting... (${Math.round((Date.now() - startTime) / 1000)}s)`,
162
+ );
163
+ }
164
+ } catch (error) {
165
+ // Server might not be ready yet, continue polling
166
+ const errorMsg =
167
+ error instanceof Error ? error.message : String(error);
168
+ // Only log every 5 seconds to reduce noise
169
+ const elapsed = Math.round((Date.now() - startTime) / 1000);
170
+ if (elapsed % 5 === 0) {
171
+ console.log(
172
+ `[E2E Test Setup] Readiness check failed (${errorMsg}), waiting... (${elapsed}s)`,
173
+ );
174
+ }
175
+ }
176
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
177
+ }
178
+
179
+ if (!isReady) {
180
+ // Cleanup before throwing
181
+ if (listeningServerInstance?.listening) {
182
+ listeningServerInstance.closeAllConnections?.();
183
+ await new Promise<void>((res) =>
184
+ listeningServerInstance.close(() => res()),
185
+ );
186
+ }
187
+ throw new Error(
188
+ `Server did not become ready within ${maxWaitTime / 1000} seconds. Package downloads may have failed.`,
189
+ );
190
+ }
191
+
87
192
  // --- Client Setup ---
88
193
  const mcpClient = new Client<Request, Notification, Result>({
89
194
  name: "mcp-e2e-test-client",
@@ -136,6 +241,8 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
136
241
  httpServer: listeningServerInstance,
137
242
  serverUrl: listeningServerUrl,
138
243
  mcpClient,
244
+ originalServerRoot,
245
+ originalInitializeStorage,
139
246
  };
140
247
  }
141
248
 
@@ -145,8 +252,14 @@ export async function setupE2ETestEnvironment(): Promise<McpE2ETestEnvironment>
145
252
  export async function cleanupE2ETestEnvironment(
146
253
  env: McpE2ETestEnvironment | null,
147
254
  ): Promise<void> {
148
- // --- Restore Original SERVER_ROOT ---
255
+ if (!env) {
256
+ // Attempt cleanup even if env is null (e.g., setup failed after config creation)
257
+ return;
258
+ }
259
+
260
+ // --- Restore Original SERVER_ROOT and INITIALIZE_STORAGE ---
149
261
  // Restore the original SERVER_ROOT value after tests are complete.
262
+ const { originalServerRoot, originalInitializeStorage } = env;
150
263
  if (originalServerRoot === undefined) {
151
264
  delete process.env.SERVER_ROOT;
152
265
  console.log("[E2E Test Cleanup] Restored SERVER_ROOT (deleted)");
@@ -157,44 +270,71 @@ export async function cleanupE2ETestEnvironment(
157
270
  );
158
271
  }
159
272
 
160
- if (!env) {
161
- // Attempt cleanup even if env is null (e.g., setup failed after config creation)
162
- return;
273
+ // Restore the original INITIALIZE_STORAGE value after tests are complete.
274
+ if (originalInitializeStorage === undefined) {
275
+ delete process.env.INITIALIZE_STORAGE;
276
+ console.log("[E2E Test Cleanup] Restored INITIALIZE_STORAGE (deleted)");
277
+ } else {
278
+ process.env.INITIALIZE_STORAGE = originalInitializeStorage;
279
+ console.log(
280
+ `[E2E Test Cleanup] Restored INITIALIZE_STORAGE=${process.env.INITIALIZE_STORAGE}`,
281
+ );
163
282
  }
164
283
 
165
284
  const { mcpClient, httpServer } = env;
166
285
 
167
- // 1. Close client first
286
+ // 1. Close client first (with timeout)
168
287
  if (mcpClient) {
169
288
  try {
170
- await mcpClient.close();
171
- } catch {
289
+ const closePromise = mcpClient.close();
290
+ const timeoutPromise = new Promise<void>((_, reject) => {
291
+ setTimeout(() => reject(new Error("Client close timeout")), 5000);
292
+ });
293
+ await Promise.race([closePromise, timeoutPromise]);
294
+ } catch (error) {
172
295
  // Ignore client close errors during cleanup potentially
173
296
  console.warn(
174
297
  "[E2E Test Cleanup] Error closing MCP client (ignoring):",
298
+ error instanceof Error ? error.message : String(error),
175
299
  );
176
300
  }
177
301
  }
178
302
 
179
- // 2. Close HTTP server connections and then the server itself
303
+ // 2. Close HTTP server connections and then the server itself (with timeout)
180
304
  if (httpServer) {
181
305
  // Force close any remaining connections immediately
182
306
  httpServer.closeAllConnections?.();
183
307
 
184
308
  if (httpServer.listening) {
185
- await new Promise<void>((resolve) => {
186
- httpServer.close((err: NodeJS.ErrnoException | undefined) => {
187
- if (err && err.code !== "ERR_SERVER_NOT_RUNNING") {
188
- console.error(
189
- "[E2E Test Cleanup] Error closing HTTP server (after closing connections):",
190
- err,
191
- );
192
- } else if (!err) {
193
- console.log("[E2E Test Cleanup] HTTP server closed.");
194
- }
195
- resolve();
309
+ try {
310
+ const closePromise = new Promise<void>((resolve, reject) => {
311
+ const timeout = setTimeout(() => {
312
+ reject(new Error("Server close timeout"));
313
+ }, 5000);
314
+ httpServer.close((err: NodeJS.ErrnoException | undefined) => {
315
+ clearTimeout(timeout);
316
+ if (err && err.code !== "ERR_SERVER_NOT_RUNNING") {
317
+ console.error(
318
+ "[E2E Test Cleanup] Error closing HTTP server (after closing connections):",
319
+ err,
320
+ );
321
+ } else if (!err) {
322
+ console.log("[E2E Test Cleanup] HTTP server closed.");
323
+ }
324
+ resolve();
325
+ });
196
326
  });
197
- });
327
+ await closePromise;
328
+ } catch (error) {
329
+ console.warn(
330
+ "[E2E Test Cleanup] Server close timeout or error (forcing close):",
331
+ error instanceof Error ? error.message : String(error),
332
+ );
333
+ // Force destroy if close times out
334
+ if (httpServer.listening) {
335
+ httpServer.closeAllConnections?.();
336
+ }
337
+ }
198
338
  } else {
199
339
  console.log(
200
340
  "[E2E Test Cleanup] HTTP server was not listening or already closed.",
@@ -92,7 +92,7 @@ describe("DuckDB Attached Databases", () => {
92
92
  message.includes("Extension")
93
93
  ) {
94
94
  console.error(
95
- `\n⚠️ BigQuery extension not available for this DuckDB version/platform.`,
95
+ `\nBigQuery extension not available for this DuckDB version/platform.`,
96
96
  );
97
97
  console.error(`Error: ${message}\n`);
98
98
  }
@@ -115,7 +115,30 @@ describe("DuckDB Attached Databases", () => {
115
115
  message.includes("Extension")
116
116
  ) {
117
117
  console.error(
118
- `\n⚠️ Snowflake extension not available for this DuckDB version/platform.`,
118
+ `\nSnowflake extension not available for this DuckDB version/platform.`,
119
+ );
120
+ console.error(`Error: ${message}\n`);
121
+ }
122
+ throw error;
123
+ }
124
+ });
125
+
126
+ it("should load ducklake extension", async () => {
127
+ try {
128
+ await connection.runSQL("INSTALL ducklake;");
129
+ await connection.runSQL("LOAD ducklake;");
130
+ const result = await connection.runSQL(
131
+ "SELECT * FROM duckdb_extensions() WHERE extension_name = 'ducklake';",
132
+ );
133
+ expect(result.rows.length).toBeGreaterThan(0);
134
+ } catch (error) {
135
+ const message = (error as Error).message;
136
+ if (
137
+ message.includes("not found") ||
138
+ message.includes("Extension")
139
+ ) {
140
+ console.error(
141
+ `\nDuckLake extension not available for this DuckDB version/platform.`,
119
142
  );
120
143
  console.error(`Error: ${message}\n`);
121
144
  }
@@ -707,6 +730,41 @@ describe("createProjectConnections - DuckDB", () => {
707
730
  ).rejects.toThrow("accessKeyId and secretAccessKey are required");
708
731
  });
709
732
 
733
+ it("should throw on DuckLake without catalog connection", async () => {
734
+ const connections = [
735
+ {
736
+ name: "ducklake_no_catalog",
737
+ type: "ducklake",
738
+ ducklakeConnection: {
739
+ storage: {
740
+ bucketUrl: "s3://test-bucket",
741
+ s3Connection: {
742
+ accessKeyId: "test",
743
+ secretAccessKey: "test",
744
+ },
745
+ },
746
+ },
747
+ },
748
+ ] as ApiConnection[];
749
+
750
+ await expect(
751
+ createProjectConnections(connections, PROJECT_TEST_DIR),
752
+ ).rejects.toThrow("PostgreSQL connection configuration is required");
753
+ });
754
+
755
+ it("should throw on DuckLake without connection config", async () => {
756
+ const connections = [
757
+ {
758
+ name: "ducklake_no_config",
759
+ type: "ducklake",
760
+ },
761
+ ] as ApiConnection[];
762
+
763
+ await expect(
764
+ createProjectConnections(connections, PROJECT_TEST_DIR),
765
+ ).rejects.toThrow("DuckLake connection configuration is missing");
766
+ });
767
+
710
768
  it("should throw on Snowflake attach without credentials", async () => {
711
769
  const connections: ApiConnection[] = [
712
770
  {
@@ -892,7 +950,7 @@ describe("createProjectConnections - Other Connection Types", () => {
892
950
  os.tmpdir(),
893
951
  "connection-validation-tests",
894
952
  );
895
- let createdConnections: Map<string, unknown> = new Map();
953
+ const createdConnections: Map<string, unknown> = new Map();
896
954
 
897
955
  beforeEach(async () => {
898
956
  await fs.mkdir(PROJECT_TEST_DIR, { recursive: true });