forked from dwavesystems/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistentState.unit.test.ts
More file actions
167 lines (142 loc) · 7.06 KB
/
persistentState.unit.test.ts
File metadata and controls
167 lines (142 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { assert, expect } from 'chai';
import * as TypeMoq from 'typemoq';
import { Memento } from 'vscode';
import { ICommandManager } from '../../client/common/application/types';
import { Commands } from '../../client/common/constants';
import {
GLOBAL_PERSISTENT_KEYS_DEPRECATED,
KeysStorage,
PersistentStateFactory,
WORKSPACE_PERSISTENT_KEYS_DEPRECATED,
} from '../../client/common/persistentState';
import { IDisposable } from '../../client/common/types';
import { sleep } from '../core';
import { MockMemento } from '../mocks/mementos';
suite('Persistent State', () => {
let cmdManager: TypeMoq.IMock<ICommandManager>;
let persistentStateFactory: PersistentStateFactory;
let workspaceMemento: Memento;
let globalMemento: Memento;
setup(() => {
cmdManager = TypeMoq.Mock.ofType<ICommandManager>();
workspaceMemento = new MockMemento();
globalMemento = new MockMemento();
persistentStateFactory = new PersistentStateFactory(globalMemento, workspaceMemento, cmdManager.object);
});
test('Global states created are restored on invoking clean storage command', async () => {
let clearStorageCommand: (() => Promise<void>) | undefined;
cmdManager
.setup((c) => c.registerCommand(Commands.ClearStorage, TypeMoq.It.isAny()))
.callback((_, c) => {
clearStorageCommand = c;
})
.returns(() => TypeMoq.Mock.ofType<IDisposable>().object);
// Register command to clean storage
await persistentStateFactory.activate();
expect(clearStorageCommand).to.not.equal(undefined, 'Callback not registered');
const globalKey1State = persistentStateFactory.createGlobalPersistentState('key1', 'defaultKey1Value');
await globalKey1State.updateValue('key1Value');
const globalKey2State = persistentStateFactory.createGlobalPersistentState<string | undefined>(
'key2',
undefined,
);
await globalKey2State.updateValue('key2Value');
// Verify states are updated correctly
expect(globalKey1State.value).to.equal('key1Value');
expect(globalKey2State.value).to.equal('key2Value');
await clearStorageCommand!(); // Invoke command
// Verify states are now reset to their default value.
expect(globalKey1State.value).to.equal('defaultKey1Value');
expect(globalKey2State.value).to.equal(undefined);
});
test('Workspace states created are restored on invoking clean storage command', async () => {
let clearStorageCommand: (() => Promise<void>) | undefined;
cmdManager
.setup((c) => c.registerCommand(Commands.ClearStorage, TypeMoq.It.isAny()))
.callback((_, c) => {
clearStorageCommand = c;
})
.returns(() => TypeMoq.Mock.ofType<IDisposable>().object);
// Register command to clean storage
await persistentStateFactory.activate();
expect(clearStorageCommand).to.not.equal(undefined, 'Callback not registered');
const workspaceKey1State = persistentStateFactory.createWorkspacePersistentState('key1');
await workspaceKey1State.updateValue('key1Value');
const workspaceKey2State = persistentStateFactory.createWorkspacePersistentState('key2', 'defaultKey2Value');
await workspaceKey2State.updateValue('key2Value');
// Verify states are updated correctly
expect(workspaceKey1State.value).to.equal('key1Value');
expect(workspaceKey2State.value).to.equal('key2Value');
await clearStorageCommand!(); // Invoke command
// Verify states are now reset to their default value.
expect(workspaceKey1State.value).to.equal(undefined);
expect(workspaceKey2State.value).to.equal('defaultKey2Value');
});
test('Ensure internal global storage extension uses to track other storages does not contain duplicate entries', async () => {
persistentStateFactory.createGlobalPersistentState('key1');
await sleep(1);
persistentStateFactory.createGlobalPersistentState('key2', ['defaultValue1']); // Default value type is an array
await sleep(1);
persistentStateFactory.createGlobalPersistentState('key2', ['defaultValue1']);
await sleep(1);
persistentStateFactory.createGlobalPersistentState('key1');
await sleep(1);
const { value } = persistentStateFactory._globalKeysStorage;
assert.deepEqual(
value.sort((k1, k2) => k1.key.localeCompare(k2.key)),
[
{ key: 'key1', defaultValue: undefined },
{ key: 'key2', defaultValue: ['defaultValue1'] },
].sort((k1, k2) => k1.key.localeCompare(k2.key)),
);
});
test('Ensure internal workspace storage extension uses to track other storages does not contain duplicate entries', async () => {
persistentStateFactory.createWorkspacePersistentState('key2', 'defaultValue1'); // Default value type is a string
await sleep(1);
persistentStateFactory.createWorkspacePersistentState('key1');
await sleep(1);
persistentStateFactory.createWorkspacePersistentState('key2', 'defaultValue1');
await sleep(1);
persistentStateFactory.createWorkspacePersistentState('key1');
await sleep(1);
const { value } = persistentStateFactory._workspaceKeysStorage;
assert.deepEqual(
value.sort((k1, k2) => k1.key.localeCompare(k2.key)),
[
{ key: 'key1', defaultValue: undefined },
{ key: 'key2', defaultValue: 'defaultValue1' },
].sort((k1, k2) => k1.key.localeCompare(k2.key)),
);
});
test('Ensure deprecated global storage extension used to track other storages with is reset', async () => {
const global = persistentStateFactory.createGlobalPersistentState<KeysStorage[]>(
GLOBAL_PERSISTENT_KEYS_DEPRECATED,
);
await global.updateValue([
{ key: 'oldKey', defaultValue: [] },
{ key: 'oldKey2', defaultValue: [{}] },
{ key: 'oldKey3', defaultValue: ['1', '2', '3'] },
]);
expect(global.value.length).to.equal(3);
await persistentStateFactory.activate();
await sleep(1);
expect(global.value.length).to.equal(0);
});
test('Ensure deprecated global storage extension used to track other storages with is reset', async () => {
const workspace = persistentStateFactory.createWorkspacePersistentState<KeysStorage[]>(
WORKSPACE_PERSISTENT_KEYS_DEPRECATED,
);
await workspace.updateValue([
{ key: 'oldKey', defaultValue: [] },
{ key: 'oldKey2', defaultValue: [{}] },
{ key: 'oldKey3', defaultValue: ['1', '2', '3'] },
]);
expect(workspace.value.length).to.equal(3);
await persistentStateFactory.activate();
await sleep(1);
expect(workspace.value.length).to.equal(0);
});
});