|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { before, after, suite, test } from 'mocha'; |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import * as state from '../../../state'; |
| 5 | +import { |
| 6 | + getEffectiveJackInDependencyVersions, |
| 7 | + JackInDependencyKey, |
| 8 | + refreshJackInDependencyVersions, |
| 9 | +} from '../../../nrepl/jack-in-dependency-versions'; |
| 10 | + |
| 11 | +const SUITE = 'Jack-in dependency versions'; |
| 12 | +const GLOBAL_STATE_KEY = 'calva.jackIn.latestDependencyVersions'; |
| 13 | + |
| 14 | +type Versions = Partial<Record<JackInDependencyKey, string>>; |
| 15 | + |
| 16 | +let prevWorkspaceValue: Versions | undefined; |
| 17 | +let prevStoredValue: Versions | undefined; |
| 18 | + |
| 19 | +suite(SUITE, () => { |
| 20 | + before(async () => { |
| 21 | + const ext = vscode.extensions.getExtension('betterthantomorrow.calva'); |
| 22 | + await ext?.activate(); |
| 23 | + |
| 24 | + await refreshJackInDependencyVersions(); |
| 25 | + |
| 26 | + const inspected = vscode.workspace |
| 27 | + .getConfiguration('calva') |
| 28 | + .inspect<Versions>('jackInDependencyVersions'); |
| 29 | + prevWorkspaceValue = inspected?.workspaceValue; |
| 30 | + |
| 31 | + prevStoredValue = state.extensionContext?.globalState.get<Versions>(GLOBAL_STATE_KEY); |
| 32 | + }); |
| 33 | + |
| 34 | + after(async () => { |
| 35 | + await vscode.workspace |
| 36 | + .getConfiguration('calva') |
| 37 | + .update('jackInDependencyVersions', prevWorkspaceValue, vscode.ConfigurationTarget.Workspace); |
| 38 | + |
| 39 | + await state.extensionContext?.globalState.update(GLOBAL_STATE_KEY, prevStoredValue); |
| 40 | + }); |
| 41 | + |
| 42 | + test('happy path: uses configured versions when set at workspace level', async () => { |
| 43 | + const configured: Record<JackInDependencyKey, string> = { |
| 44 | + nrepl: 'TEST-NREPL-1', |
| 45 | + 'cider-nrepl': 'TEST-CIDER-NREPL-2', |
| 46 | + 'cider/piggieback': 'TEST-PIGGIEBACK-3', |
| 47 | + }; |
| 48 | + |
| 49 | + await vscode.workspace |
| 50 | + .getConfiguration('calva') |
| 51 | + .update('jackInDependencyVersions', configured, vscode.ConfigurationTarget.Workspace); |
| 52 | + |
| 53 | + const effective = getEffectiveJackInDependencyVersions(); |
| 54 | + |
| 55 | + assert.deepStrictEqual( |
| 56 | + effective, |
| 57 | + configured, |
| 58 | + `Expected configured versions to be used. Got ${JSON.stringify(effective)}` |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('partial configuration: missing keys fall back while set keys are respected', async () => { |
| 63 | + // Clear stored values to avoid influencing this test |
| 64 | + await state.extensionContext?.globalState.update(GLOBAL_STATE_KEY, {}); |
| 65 | + |
| 66 | + const inspectedDefaults = vscode.workspace |
| 67 | + .getConfiguration('calva') |
| 68 | + .inspect<Record<JackInDependencyKey, string>>('jackInDependencyVersions'); |
| 69 | + const defaults = (inspectedDefaults?.defaultValue ?? {}) as Record<JackInDependencyKey, string>; |
| 70 | + |
| 71 | + const configured: Versions = { |
| 72 | + nrepl: 'PARTIAL-NREPL-1', |
| 73 | + }; |
| 74 | + |
| 75 | + await vscode.workspace |
| 76 | + .getConfiguration('calva') |
| 77 | + .update('jackInDependencyVersions', configured, vscode.ConfigurationTarget.Workspace); |
| 78 | + |
| 79 | + const effective = getEffectiveJackInDependencyVersions(); |
| 80 | + |
| 81 | + assert.strictEqual(effective.nrepl, 'PARTIAL-NREPL-1', 'nrepl should use configured value'); |
| 82 | + assert.strictEqual( |
| 83 | + effective['cider-nrepl'], |
| 84 | + defaults['cider-nrepl'], |
| 85 | + 'cider-nrepl should fall back to default' |
| 86 | + ); |
| 87 | + assert.strictEqual( |
| 88 | + effective['cider/piggieback'], |
| 89 | + defaults['cider/piggieback'], |
| 90 | + 'cider/piggieback should fall back to default' |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + test('precedence: stored values are used when nothing is configured', async () => { |
| 95 | + const stored: Record<JackInDependencyKey, string> = { |
| 96 | + nrepl: 'STORED-NREPL-1', |
| 97 | + 'cider-nrepl': 'STORED-CIDER-2', |
| 98 | + 'cider/piggieback': 'STORED-PIGGIE-3', |
| 99 | + }; |
| 100 | + await state.extensionContext?.globalState.update(GLOBAL_STATE_KEY, stored); |
| 101 | + |
| 102 | + await vscode.workspace |
| 103 | + .getConfiguration('calva') |
| 104 | + .update('jackInDependencyVersions', undefined, vscode.ConfigurationTarget.Workspace); |
| 105 | + |
| 106 | + const effective = getEffectiveJackInDependencyVersions(); |
| 107 | + assert.deepStrictEqual( |
| 108 | + effective, |
| 109 | + stored, |
| 110 | + 'When nothing is configured, stored values should be used' |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test('precedence: configured overrides stored', async () => { |
| 115 | + const stored: Record<JackInDependencyKey, string> = { |
| 116 | + nrepl: 'STORED-NREPL-1', |
| 117 | + 'cider-nrepl': 'STORED-CIDER-2', |
| 118 | + 'cider/piggieback': 'STORED-PIGGIE-3', |
| 119 | + }; |
| 120 | + await state.extensionContext?.globalState.update(GLOBAL_STATE_KEY, stored); |
| 121 | + |
| 122 | + const configured: Versions = { |
| 123 | + nrepl: 'CONFIG-NREPL-1', |
| 124 | + 'cider/piggieback': 'CONFIG-PIGGIE-3', |
| 125 | + }; |
| 126 | + await vscode.workspace |
| 127 | + .getConfiguration('calva') |
| 128 | + .update('jackInDependencyVersions', configured, vscode.ConfigurationTarget.Workspace); |
| 129 | + |
| 130 | + const effective = getEffectiveJackInDependencyVersions(); |
| 131 | + assert.strictEqual(effective.nrepl, 'CONFIG-NREPL-1', 'configured should override stored'); |
| 132 | + assert.strictEqual( |
| 133 | + effective['cider/piggieback'], |
| 134 | + 'CONFIG-PIGGIE-3', |
| 135 | + 'configured should override stored' |
| 136 | + ); |
| 137 | + assert.strictEqual( |
| 138 | + effective['cider-nrepl'], |
| 139 | + 'STORED-CIDER-2', |
| 140 | + 'stored should be used when not configured' |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + test('precedence: default is used when neither configured nor stored', async () => { |
| 145 | + const inspectedDefaults = vscode.workspace |
| 146 | + .getConfiguration('calva') |
| 147 | + .inspect<Record<JackInDependencyKey, string>>('jackInDependencyVersions'); |
| 148 | + const defaults = (inspectedDefaults?.defaultValue ?? {}) as Record<JackInDependencyKey, string>; |
| 149 | + |
| 150 | + await state.extensionContext?.globalState.update(GLOBAL_STATE_KEY, {}); |
| 151 | + await vscode.workspace |
| 152 | + .getConfiguration('calva') |
| 153 | + .update('jackInDependencyVersions', undefined, vscode.ConfigurationTarget.Workspace); |
| 154 | + |
| 155 | + const effective = getEffectiveJackInDependencyVersions(); |
| 156 | + |
| 157 | + assert.deepStrictEqual( |
| 158 | + effective, |
| 159 | + defaults, |
| 160 | + 'effective should equal defaults when nothing is configured or stored' |
| 161 | + ); |
| 162 | + }); |
| 163 | +}); |
0 commit comments