158 lines
5.4 KiB
TypeScript
158 lines
5.4 KiB
TypeScript
import { strict as assert } from "node:assert";
|
|
import test from "node:test";
|
|
import { createPinia, setActivePinia } from "pinia";
|
|
import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts";
|
|
import { useQueryStore } from "../../apps/desktop/src/stores/queryStore.ts";
|
|
import type { ConnectionConfig } from "../../apps/desktop/src/types/database.ts";
|
|
import type { QueryResult } from "../../apps/desktop/src/types/database.ts";
|
|
|
|
function installMemoryStorage() {
|
|
const values = new Map<string, string>();
|
|
const original = Object.getOwnPropertyDescriptor(globalThis, "localStorage");
|
|
Object.defineProperty(globalThis, "localStorage", {
|
|
configurable: true,
|
|
value: {
|
|
getItem: (key: string) => values.get(key) ?? null,
|
|
setItem: (key: string, value: string) => values.set(key, value),
|
|
removeItem: (key: string) => values.delete(key),
|
|
clear: () => values.clear(),
|
|
},
|
|
});
|
|
return () => {
|
|
if (original) Object.defineProperty(globalThis, "localStorage", original);
|
|
else Reflect.deleteProperty(globalThis, "localStorage");
|
|
};
|
|
}
|
|
|
|
function conn(id: string): ConnectionConfig {
|
|
return {
|
|
id,
|
|
name: id,
|
|
db_type: "postgres",
|
|
host: "localhost",
|
|
port: 5432,
|
|
username: "postgres",
|
|
password: "",
|
|
};
|
|
}
|
|
|
|
test("setErrorResult stops loading and shows the error result", () => {
|
|
setActivePinia(createPinia());
|
|
const store = useQueryStore();
|
|
const tabId = store.createTab("conn-1", "db", "users", "data");
|
|
|
|
store.setExecuting(tabId, true);
|
|
store.setErrorResult(tabId, new Error("metadata failed"));
|
|
|
|
const tab = store.tabs.find((item) => item.id === tabId);
|
|
assert.equal(tab?.isExecuting, false);
|
|
assert.equal(tab?.isCancelling, false);
|
|
assert.equal(tab?.executionId, undefined);
|
|
assert.deepEqual(tab?.result?.columns, ["Error"]);
|
|
assert.deepEqual(tab?.result?.rows, [["metadata failed"]]);
|
|
});
|
|
|
|
test("evicting cached tab results releases multi-result payloads and sessions", async () => {
|
|
const restoreStorage = installMemoryStorage();
|
|
setActivePinia(createPinia());
|
|
const connectionStore = useConnectionStore();
|
|
const store = useQueryStore();
|
|
const originalFetch = globalThis.fetch;
|
|
let executeCount = 0;
|
|
const closedSessions: string[] = [];
|
|
|
|
connectionStore.addEphemeralConnection(conn("conn-1"));
|
|
|
|
globalThis.fetch = (async (input, init) => {
|
|
const url = String(input);
|
|
if (url === "/api/query/execute-multi") {
|
|
executeCount++;
|
|
const results: QueryResult[] = [
|
|
{
|
|
columns: ["id"],
|
|
rows: [[executeCount]],
|
|
affected_rows: 0,
|
|
execution_time_ms: 1,
|
|
session_id: `session-${executeCount}`,
|
|
},
|
|
{
|
|
columns: ["detail"],
|
|
rows: [[`payload-${executeCount}`]],
|
|
affected_rows: 0,
|
|
execution_time_ms: 1,
|
|
},
|
|
];
|
|
return new Response(JSON.stringify(results), { status: 200, headers: { "Content-Type": "application/json" } });
|
|
}
|
|
if (url === "/api/query/close-session") {
|
|
const body = JSON.parse(String(init?.body ?? "{}"));
|
|
closedSessions.push(body.sessionId);
|
|
return new Response(JSON.stringify(true), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
if (url === "/api/query/analyze-editability") {
|
|
return new Response(JSON.stringify({ editable: false, reason: "complex-source" }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
if (url === "/api/query/prepare-pagination-plan") {
|
|
const body = JSON.parse(String(init?.body ?? "{}"));
|
|
return new Response(
|
|
JSON.stringify({
|
|
sqlToExecute: body.options.sql,
|
|
useAgentResultSession: false,
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
return new Response("unexpected request", { status: 500 });
|
|
}) as typeof fetch;
|
|
|
|
try {
|
|
const tabIds: string[] = [];
|
|
for (let i = 0; i < 7; i++) {
|
|
const tabId = store.createTab("conn-1", "db", `Query ${i + 1}`);
|
|
tabIds.push(tabId);
|
|
await store.executeTabSql(tabId, `select ${i + 1}; select ${i + 1} as detail`);
|
|
}
|
|
|
|
const evicted = store.tabs.find((tab) => tab.id === tabIds[0]);
|
|
assert.equal(executeCount, 7);
|
|
assert.equal(evicted?.result, undefined);
|
|
assert.equal(evicted?.results, undefined);
|
|
assert.equal(evicted?.activeResultIndex, undefined);
|
|
assert.equal(evicted?.resultSessionId, undefined);
|
|
assert.equal(evicted?.resultEvicted, true);
|
|
assert.deepEqual(closedSessions, ["session-1"]);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
restoreStorage();
|
|
}
|
|
});
|
|
|
|
test("tab reuse is scoped by mode and schema instead of title alone", () => {
|
|
const restoreStorage = installMemoryStorage();
|
|
try {
|
|
setActivePinia(createPinia());
|
|
const store = useQueryStore();
|
|
|
|
const dataTabId = store.createTab("conn-1", "db", "users", "data", "public");
|
|
const sourceTabId = store.createTab("conn-1", "db", "users", "query", "public");
|
|
const otherSchemaTabId = store.createTab("conn-1", "db", "users", "data", "audit");
|
|
const reusedDataTabId = store.createTab("conn-1", "db", "users", "data", "public");
|
|
|
|
assert.notEqual(sourceTabId, dataTabId);
|
|
assert.notEqual(otherSchemaTabId, dataTabId);
|
|
assert.equal(reusedDataTabId, dataTabId);
|
|
assert.equal(store.tabs.length, 3);
|
|
} finally {
|
|
restoreStorage();
|
|
}
|
|
});
|