|
| 1 | +import assert from 'assert' |
| 2 | +import { NotificationHandler } from '../../jsonrpc2/handlers' |
| 3 | +import { |
| 4 | + ConfigurationCascade, |
| 5 | + ConfigurationUpdateParams, |
| 6 | + ConfigurationUpdateRequest, |
| 7 | + DidChangeConfigurationParams, |
| 8 | + Settings, |
| 9 | +} from '../../protocol' |
| 10 | +import { IConnection } from '../server' |
| 11 | +import { RemoteConfiguration, setValueAtKeyPath } from './configuration' |
| 12 | + |
| 13 | +const EMPTY_MOCK_CONNECTION: IConnection = { |
| 14 | + onDidChangeConfiguration: () => void 0, |
| 15 | + sendRequest: () => Promise.resolve(void 0), |
| 16 | +} as any |
| 17 | + |
| 18 | +const FIXTURE_CONFIGURATION_CASCADE: ConfigurationCascade<Settings> = { merged: { a: 1 } } |
| 19 | + |
| 20 | +describe('RemoteConfigurationImpl', () => { |
| 21 | + const create = ( |
| 22 | + connection: IConnection = EMPTY_MOCK_CONNECTION, |
| 23 | + configurationCascade = FIXTURE_CONFIGURATION_CASCADE |
| 24 | + ): RemoteConfiguration<Settings> => { |
| 25 | + const remote = new RemoteConfiguration<Settings>() |
| 26 | + remote.attach(connection) |
| 27 | + remote.initialize({ |
| 28 | + root: null, |
| 29 | + capabilities: {}, |
| 30 | + configurationCascade, |
| 31 | + workspaceFolders: null, |
| 32 | + }) |
| 33 | + return remote |
| 34 | + } |
| 35 | + |
| 36 | + it('initially reports an empty config', () => { |
| 37 | + const remote = new RemoteConfiguration<Settings>() |
| 38 | + assert.deepStrictEqual(remote.configuration.value, {} as Settings) |
| 39 | + }) |
| 40 | + |
| 41 | + it('records the configuration value from initialize', () => { |
| 42 | + const remote = create() |
| 43 | + assert.deepStrictEqual(remote.configuration.value, { a: 1 } as Settings) |
| 44 | + }) |
| 45 | + |
| 46 | + it('records the configuration value from client update notifications', () => { |
| 47 | + let onDidChangeConfiguration: NotificationHandler<DidChangeConfigurationParams> | undefined |
| 48 | + const remote = create({ |
| 49 | + ...EMPTY_MOCK_CONNECTION, |
| 50 | + onDidChangeConfiguration: h => (onDidChangeConfiguration = h), |
| 51 | + }) |
| 52 | + assert.ok(onDidChangeConfiguration) |
| 53 | + onDidChangeConfiguration!({ configurationCascade: { merged: { b: 2 } } }) |
| 54 | + assert.deepStrictEqual(remote.configuration.value, { b: 2 } as Settings) |
| 55 | + }) |
| 56 | + |
| 57 | + it('updateConfiguration edit is sent as request and reflected immediately', async () => { |
| 58 | + const remote = create({ |
| 59 | + ...EMPTY_MOCK_CONNECTION, |
| 60 | + sendRequest: async (type: any, params: any) => { |
| 61 | + assert.strictEqual(type, ConfigurationUpdateRequest.type) |
| 62 | + assert.deepStrictEqual(params, { path: ['b'], value: 2 } as ConfigurationUpdateParams) |
| 63 | + }, |
| 64 | + }) |
| 65 | + const updated = remote.updateConfiguration(['b'], 2) |
| 66 | + assert.deepStrictEqual(remote.configuration.value, { a: 1, b: 2 } as Settings) |
| 67 | + await updated |
| 68 | + assert.deepStrictEqual(remote.configuration.value, { a: 1, b: 2 } as Settings) |
| 69 | + }) |
| 70 | + |
| 71 | + it('handles interleaved updateConfiguration and didChangeConfiguration (authoritative)', async () => { |
| 72 | + let onDidChangeConfiguration: NotificationHandler<DidChangeConfigurationParams> | undefined |
| 73 | + const remote = create({ |
| 74 | + ...EMPTY_MOCK_CONNECTION, |
| 75 | + sendRequest: async (type: any, params: any) => { |
| 76 | + assert.strictEqual(type, ConfigurationUpdateRequest.type) |
| 77 | + assert.deepStrictEqual(params, { path: ['b'], value: 2 } as ConfigurationUpdateParams) |
| 78 | + }, |
| 79 | + onDidChangeConfiguration: h => (onDidChangeConfiguration = h), |
| 80 | + }) |
| 81 | + const updated = remote.updateConfiguration(['b'], 2) |
| 82 | + assert.deepStrictEqual(remote.configuration.value, { a: 1, b: 2 } as Settings) |
| 83 | + onDidChangeConfiguration!({ configurationCascade: { merged: { c: 3 } } }) |
| 84 | + await updated |
| 85 | + assert.deepStrictEqual(remote.configuration.value, { c: 3 } as Settings) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('setValueAtKeyPath', () => { |
| 90 | + it('overwrites the top level', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, [], { b: 2 }), { b: 2 })) |
| 91 | + it('overwrites an existing property', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, ['a'], 2), { a: 2 })) |
| 92 | + it('sets a new property', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, ['b'], 2), { a: 1, b: 2 })) |
| 93 | + it('sets a property overwriting an array', () => assert.deepStrictEqual(setValueAtKeyPath([1], ['a'], 2), { a: 2 })) |
| 94 | + it('sets a property overwriting a primitive', () => |
| 95 | + assert.deepStrictEqual(setValueAtKeyPath(1 as any, ['a'], 2), { a: 2 })) |
| 96 | + it('overwrites an existing nested property', () => |
| 97 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a', 'b'], 2), { a: { b: 2 } })) |
| 98 | + it('deletes a property', () => |
| 99 | + assert.deepStrictEqual(setValueAtKeyPath({ a: 1, b: 2 }, ['a'], undefined), { b: 2 })) |
| 100 | + it('sets a new nested property', () => |
| 101 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a', 'c'], 2), { a: { b: 1, c: 2 } })) |
| 102 | + it('sets a new deeply nested property', () => |
| 103 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: { c: 1 } } }, ['a', 'b', 'd'], 2), { |
| 104 | + a: { b: { c: 1, d: 2 } }, |
| 105 | + })) |
| 106 | + it('overwrites an object', () => assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a'], 2), { a: 2 })) |
| 107 | + it('sets a value that requires a new object', () => |
| 108 | + assert.deepStrictEqual(setValueAtKeyPath({}, ['a', 'b'], 1), { a: { b: 1 } })) |
| 109 | + |
| 110 | + it('overwrites an existing index', () => assert.deepStrictEqual(setValueAtKeyPath([1], [0], 2), [2])) |
| 111 | + it('inserts a new index', () => assert.deepStrictEqual(setValueAtKeyPath([1], [1], 2), [1, 2])) |
| 112 | + it('inserts a new index at end', () => assert.deepStrictEqual(setValueAtKeyPath([1, 2], [-1], 3), [1, 2, 3])) |
| 113 | + it('inserts an index overwriting an object', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, [0], 2), [2])) |
| 114 | + it('inserts an index overwriting a primitive', () => |
| 115 | + assert.deepStrictEqual(setValueAtKeyPath(1 as any, [0], 2), [2])) |
| 116 | + it('overwrites an existing nested index', () => |
| 117 | + assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1, 0], 3), [1, [3]])) |
| 118 | + it('deletes an index', () => assert.deepStrictEqual(setValueAtKeyPath([1, 2, 3], [1], undefined), [1, 3])) |
| 119 | + it('sets a new nested index', () => |
| 120 | + assert.deepStrictEqual(setValueAtKeyPath([1, [1, 2, [1, 2, 3, 4]]], [1, 2, 3], 5), [1, [1, 2, [1, 2, 3, 5]]])) |
| 121 | + it('inserts a new nested index at end', () => |
| 122 | + assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1, -1], 3), [1, [2, 3]])) |
| 123 | + it('overwrites an array', () => assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1], 3), [1, 3])) |
| 124 | + it('sets a value that requires a new array', () => assert.deepStrictEqual(setValueAtKeyPath([], [0, 0], 1), [[1]])) |
| 125 | + |
| 126 | + it('sets a nested property (and does not modify input)', () => { |
| 127 | + const input = { a: [{}, { b: [1, 2] }] } |
| 128 | + const origInput = JSON.parse(JSON.stringify(input)) |
| 129 | + assert.deepStrictEqual(setValueAtKeyPath(input, ['a', 1, 'b', -1], { c: 3 }), { |
| 130 | + a: [{}, { b: [1, 2, { c: 3 }] }], |
| 131 | + }) |
| 132 | + assert.deepStrictEqual(input, origInput) |
| 133 | + }) |
| 134 | + it('throws on invalid key type', () => assert.throws(() => setValueAtKeyPath({}, [true as any], {}))) |
| 135 | +}) |
0 commit comments