Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chat2db-community-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:ai-markdown-code": "tsx src/blocks/AI/markdownCode.test.tsx",
"test:base-table-interaction": "tsx src/components/BaseTable/treeInteraction.test.ts",
"test:database-object-sorting": "tsx src/blocks/NewTree/utils/sortTreeNodes.test.ts",
"test:execution-console": "tsx src/blocks/SearchResult/components/ExecutionConsole/executionConsolePreferences.test.ts",
"test:redis-explorer": "tsx src/blocks/RedisAllData/redisExplorer.test.ts",
"test:search-result-query-composer": "tsx src/blocks/SearchResult/components/ScreeningResult/queryComposer.test.ts",
"test:search-result-tab-selection": "tsx src/blocks/SearchResult/tabSelection.test.ts",
Expand Down
4 changes: 2 additions & 2 deletions chat2db-community-client/scripts/i18n-source-hashes.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"es-ES": {
"ai.ts": "391a6e14c58d8401bf3bc245eca5bdd4b66efd8225a877bfbbcae01a1c1c019a",
"chat.ts": "0cbe009eacbaa95a9a29bbc69588cb547e90b563667c53edec3e54ee64f5667b",
"common.ts": "a7609e98f3debb2d0215d0ad6ce8c569829228f57ee887f010ced6fe7ca9deac",
"common.ts": "91dad744d8a1a7b829c61e0dc47e6912953f106f2c96bab1ad0d9858278badcd",
"connection.ts": "41d07fd10c9550ae51995620cc998de5e99add35edb31c2bb592864c76c13688",
"dashboard.ts": "b217bbe260c7674d4efad1dd1b61136653858884f5b9b2134cfdf7d1de5a44ee",
"editTable.ts": "0f7a808f50053cd52b183d156bb4fd46bb125b8058f8774deb61c7f02894a350",
Expand All @@ -32,7 +32,7 @@
"ko-KR": {
"ai.ts": "391a6e14c58d8401bf3bc245eca5bdd4b66efd8225a877bfbbcae01a1c1c019a",
"chat.ts": "0cbe009eacbaa95a9a29bbc69588cb547e90b563667c53edec3e54ee64f5667b",
"common.ts": "a7609e98f3debb2d0215d0ad6ce8c569829228f57ee887f010ced6fe7ca9deac",
"common.ts": "91dad744d8a1a7b829c61e0dc47e6912953f106f2c96bab1ad0d9858278badcd",
"connection.ts": "41d07fd10c9550ae51995620cc998de5e99add35edb31c2bb592864c76c13688",
"dashboard.ts": "b217bbe260c7674d4efad1dd1b61136653858884f5b9b2134cfdf7d1de5a44ee",
"editTable.ts": "0f7a808f50053cd52b183d156bb4fd46bb125b8058f8774deb61c7f02894a350",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import assert from 'node:assert/strict';
import type { SqlExecutionLogRecord } from '@/service/sqlExecutionLog';
import {
createExecutionConsoleOrderStorageKey,
getLatestExecutionEdgeScrollTop,
orderExecutionLogRecords,
persistExecutionConsoleOrder,
readExecutionConsoleOrder,
} from './executionConsolePreferences';

function record(id: string, executionId: string, statementSequence: number): SqlExecutionLogRecord {
return {
id,
executionId,
statementSequence,
startedAtEpochMs: statementSequence,
status: 'success',
sql: `select ${statementSequence}`,
context: {},
outputs: [],
pendingRowCounts: {},
};
}

const storageKey = createExecutionConsoleOrderStorageKey('community', 'community');
const values = new Map<string, string>();
const storage = {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => {
values.set(key, value);
},
};

assert.equal(storageKey, 'chat2db.community.community.execution-console.order.v1');
assert.notEqual(
storageKey,
createExecutionConsoleOrderStorageKey('community', 'desktop'),
'runtime modes keep independent preferences',
);
assert.equal(readExecutionConsoleOrder(undefined, storageKey), 'oldest-first');
assert.equal(readExecutionConsoleOrder(storage, storageKey), 'oldest-first');

values.set(storageKey, 'invalid');
assert.equal(readExecutionConsoleOrder(storage, storageKey), 'oldest-first');
values.set(storageKey, 'newest-first');
assert.equal(readExecutionConsoleOrder(storage, storageKey), 'newest-first');
persistExecutionConsoleOrder(storage, storageKey, 'oldest-first');
assert.equal(values.get(storageKey), 'oldest-first');

const unavailableStorage = {
getItem: () => {
throw new Error('unavailable');
},
setItem: () => {
throw new Error('unavailable');
},
};
assert.equal(readExecutionConsoleOrder(unavailableStorage, storageKey), 'oldest-first');
assert.doesNotThrow(() => persistExecutionConsoleOrder(unavailableStorage, storageKey, 'newest-first'));

const records = [
record('execution-1-statement-1', 'execution-1', 1),
record('execution-1-statement-2', 'execution-1', 2),
record('execution-2-statement-1', 'execution-2', 1),
record('execution-3-statement-1', 'execution-3', 1),
record('execution-3-statement-2', 'execution-3', 2),
];

assert.deepEqual(
orderExecutionLogRecords(records, 'oldest-first').map((item) => item.id),
records.map((item) => item.id),
);
assert.deepEqual(
orderExecutionLogRecords(records, 'newest-first').map((item) => item.id),
[
'execution-3-statement-1',
'execution-3-statement-2',
'execution-2-statement-1',
'execution-1-statement-1',
'execution-1-statement-2',
],
'newest-first reverses execution groups without reversing statements inside a group',
);
assert.deepEqual(
records.map((item) => item.id),
[
'execution-1-statement-1',
'execution-1-statement-2',
'execution-2-statement-1',
'execution-3-statement-1',
'execution-3-statement-2',
],
'ordering does not mutate reducer state',
);

assert.equal(getLatestExecutionEdgeScrollTop(500, 'newest-first'), 0);
assert.equal(getLatestExecutionEdgeScrollTop(500, 'oldest-first'), 500);

console.log('Execution console preference tests passed');
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { SqlExecutionLogRecord } from '@/service/sqlExecutionLog';

export type ExecutionConsoleOrder = 'oldest-first' | 'newest-first';

interface ExecutionConsolePreferenceStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}

export const DEFAULT_EXECUTION_CONSOLE_ORDER: ExecutionConsoleOrder = 'oldest-first';

export function createExecutionConsoleOrderStorageKey(clientEdition: string, runtimeEnv: string) {
return `chat2db.${clientEdition}.${runtimeEnv}.execution-console.order.v1`;
}

export function getExecutionConsolePreferenceStorage(): ExecutionConsolePreferenceStorage | undefined {
try {
return typeof window === 'undefined' ? undefined : window.localStorage;
} catch {
return undefined;
}
}

export function readExecutionConsoleOrder(
storage: ExecutionConsolePreferenceStorage | undefined,
storageKey: string,
): ExecutionConsoleOrder {
try {
const storedValue = storage?.getItem(storageKey);
return storedValue === 'oldest-first' || storedValue === 'newest-first'
? storedValue
: DEFAULT_EXECUTION_CONSOLE_ORDER;
} catch {
return DEFAULT_EXECUTION_CONSOLE_ORDER;
}
}

export function persistExecutionConsoleOrder(
storage: ExecutionConsolePreferenceStorage | undefined,
storageKey: string,
order: ExecutionConsoleOrder,
) {
try {
storage?.setItem(storageKey, order);
} catch {
// Storage can be unavailable in restricted browser contexts.
}
}

export function orderExecutionLogRecords(
records: readonly SqlExecutionLogRecord[],
order: ExecutionConsoleOrder,
): SqlExecutionLogRecord[] {
if (order === 'oldest-first') {
return [...records];
}

const executionGroups = new Map<string, SqlExecutionLogRecord[]>();
records.forEach((record) => {
const group = executionGroups.get(record.executionId);
if (group) {
group.push(record);
} else {
executionGroups.set(record.executionId, [record]);
}
});

return Array.from(executionGroups.values())
.reverse()
.flat();
}

export function getLatestExecutionEdgeScrollTop(scrollHeight: number, order: ExecutionConsoleOrder) {
return order === 'newest-first' ? 0 : scrollHeight;
}
Loading
Loading