11import assert from 'node:assert/strict' ;
22import type { SqlExecutionLogRecord } from '@/service/sqlExecutionLog' ;
33import {
4+ createExecutionConsoleKeepHistoryStorageKey ,
45 createExecutionConsoleOrderStorageKey ,
56 getLatestExecutionEdgeScrollTop ,
67 orderExecutionLogRecords ,
8+ persistExecutionConsoleKeepHistory ,
79 persistExecutionConsoleOrder ,
10+ readExecutionConsoleKeepHistory ,
811 readExecutionConsoleOrder ,
12+ subscribeExecutionConsoleKeepHistory ,
913} from './executionConsolePreferences' ;
1014
1115function record ( id : string , executionId : string , statementSequence : number ) : SqlExecutionLogRecord {
@@ -23,6 +27,7 @@ function record(id: string, executionId: string, statementSequence: number): Sql
2327}
2428
2529const storageKey = createExecutionConsoleOrderStorageKey ( 'community' , 'community' ) ;
30+ const keepHistoryStorageKey = createExecutionConsoleKeepHistoryStorageKey ( 'community' , 'community' ) ;
2631const values = new Map < string , string > ( ) ;
2732const storage = {
2833 getItem : ( key : string ) => values . get ( key ) ?? null ,
@@ -32,6 +37,7 @@ const storage = {
3237} ;
3338
3439assert . equal ( storageKey , 'chat2db.community.community.execution-console.order.v1' ) ;
40+ assert . equal ( keepHistoryStorageKey , 'chat2db.community.community.execution-console.keep-history.v1' ) ;
3541assert . notEqual (
3642 storageKey ,
3743 createExecutionConsoleOrderStorageKey ( 'community' , 'desktop' ) ,
@@ -47,6 +53,35 @@ assert.equal(readExecutionConsoleOrder(storage, storageKey), 'newest-first');
4753persistExecutionConsoleOrder ( storage , storageKey , 'oldest-first' ) ;
4854assert . equal ( values . get ( storageKey ) , 'oldest-first' ) ;
4955
56+ assert . equal ( readExecutionConsoleKeepHistory ( undefined , keepHistoryStorageKey ) , true ) ;
57+ assert . equal ( readExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey ) , true ) ;
58+ values . set ( keepHistoryStorageKey , 'invalid' ) ;
59+ assert . equal ( readExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey ) , true ) ;
60+ values . set ( keepHistoryStorageKey , 'false' ) ;
61+ assert . equal ( readExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey ) , false ) ;
62+ persistExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey , true ) ;
63+ assert . equal ( values . get ( keepHistoryStorageKey ) , 'true' ) ;
64+
65+ const keepHistoryUpdates : Array < [ string , boolean ] > = [ ] ;
66+ const secondKeepHistoryUpdates : Array < [ string , boolean ] > = [ ] ;
67+ const unsubscribeKeepHistory = subscribeExecutionConsoleKeepHistory ( ( key , keepHistory ) => {
68+ keepHistoryUpdates . push ( [ key , keepHistory ] ) ;
69+ } ) ;
70+ const unsubscribeSecondKeepHistory = subscribeExecutionConsoleKeepHistory ( ( key , keepHistory ) => {
71+ secondKeepHistoryUpdates . push ( [ key , keepHistory ] ) ;
72+ } ) ;
73+ persistExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey , false ) ;
74+ assert . deepEqual ( keepHistoryUpdates , [ [ keepHistoryStorageKey , false ] ] ) ;
75+ assert . deepEqual ( secondKeepHistoryUpdates , [ [ keepHistoryStorageKey , false ] ] ) ;
76+ unsubscribeKeepHistory ( ) ;
77+ persistExecutionConsoleKeepHistory ( storage , keepHistoryStorageKey , true ) ;
78+ assert . deepEqual ( keepHistoryUpdates , [ [ keepHistoryStorageKey , false ] ] , 'unsubscribed editors stop receiving updates' ) ;
79+ assert . deepEqual ( secondKeepHistoryUpdates , [
80+ [ keepHistoryStorageKey , false ] ,
81+ [ keepHistoryStorageKey , true ] ,
82+ ] ) ;
83+ unsubscribeSecondKeepHistory ( ) ;
84+
5085const unavailableStorage = {
5186 getItem : ( ) => {
5287 throw new Error ( 'unavailable' ) ;
@@ -57,6 +92,8 @@ const unavailableStorage = {
5792} ;
5893assert . equal ( readExecutionConsoleOrder ( unavailableStorage , storageKey ) , 'oldest-first' ) ;
5994assert . doesNotThrow ( ( ) => persistExecutionConsoleOrder ( unavailableStorage , storageKey , 'newest-first' ) ) ;
95+ assert . equal ( readExecutionConsoleKeepHistory ( unavailableStorage , keepHistoryStorageKey ) , true ) ;
96+ assert . doesNotThrow ( ( ) => persistExecutionConsoleKeepHistory ( unavailableStorage , keepHistoryStorageKey , false ) ) ;
6097
6198const records = [
6299 record ( 'execution-1-statement-1' , 'execution-1' , 1 ) ,
0 commit comments