|
21 | 21 |
|
22 | 22 | import * as assert from 'node:assert'; |
23 | 23 | import * as vscode from 'vscode'; |
| 24 | +import { deleteIdentityFromConfig } from '../../identity/identity'; |
24 | 25 |
|
25 | 26 | const EXTENSION_ID = 'nullvariant.git-id-switcher'; |
26 | 27 | const CONFIG_SECTION = 'gitIdSwitcher'; |
@@ -706,4 +707,156 @@ describe('Identity E2E Test Suite', function () { |
706 | 707 | assert.strictEqual(inspection?.key, 'gitIdSwitcher.identities', 'Key should be fully qualified'); |
707 | 708 | }); |
708 | 709 | }); |
| 710 | + |
| 711 | + describe('deleteIdentityFromConfig', () => { |
| 712 | + it('should delete an existing identity successfully', async () => { |
| 713 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 714 | + const currentIdentities = config.get<TestIdentity[]>('identities', []); |
| 715 | + |
| 716 | + // Add a test identity to delete |
| 717 | + const testIdentity: TestIdentity = { |
| 718 | + id: 'test-delete-identity', |
| 719 | + name: 'Delete Test User', |
| 720 | + email: 'delete-test@example.com', |
| 721 | + }; |
| 722 | + const identitiesWithTest = [...currentIdentities, testIdentity]; |
| 723 | + await config.update('identities', identitiesWithTest, vscode.ConfigurationTarget.Global); |
| 724 | + |
| 725 | + // Verify test identity was added |
| 726 | + const configAfterAdd = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 727 | + const afterAdd = configAfterAdd.get<TestIdentity[]>('identities', []); |
| 728 | + assert.ok( |
| 729 | + afterAdd.some(i => i.id === 'test-delete-identity'), |
| 730 | + 'Test identity should be added before deletion' |
| 731 | + ); |
| 732 | + |
| 733 | + // Delete the test identity |
| 734 | + await deleteIdentityFromConfig('test-delete-identity'); |
| 735 | + |
| 736 | + // Verify identity was deleted |
| 737 | + const freshConfig = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 738 | + const afterDelete = freshConfig.get<TestIdentity[]>('identities', []); |
| 739 | + assert.ok( |
| 740 | + !afterDelete.some(i => i.id === 'test-delete-identity'), |
| 741 | + 'Test identity should be deleted' |
| 742 | + ); |
| 743 | + |
| 744 | + // Restore original state |
| 745 | + await config.update('identities', currentIdentities, vscode.ConfigurationTarget.Global); |
| 746 | + }); |
| 747 | + |
| 748 | + it('should preserve other identities after deletion', async () => { |
| 749 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 750 | + const currentIdentities = config.get<TestIdentity[]>('identities', []); |
| 751 | + |
| 752 | + // Add multiple test identities |
| 753 | + const testIdentities: TestIdentity[] = [ |
| 754 | + { id: 'test-keep-1', name: 'Keep User 1', email: 'keep1@example.com' }, |
| 755 | + { id: 'test-delete-me', name: 'Delete Me', email: 'delete@example.com' }, |
| 756 | + { id: 'test-keep-2', name: 'Keep User 2', email: 'keep2@example.com' }, |
| 757 | + ]; |
| 758 | + await config.update('identities', testIdentities, vscode.ConfigurationTarget.Global); |
| 759 | + |
| 760 | + // Delete the middle identity |
| 761 | + await deleteIdentityFromConfig('test-delete-me'); |
| 762 | + |
| 763 | + // Verify correct identities remain |
| 764 | + const freshConfig = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 765 | + const afterDelete = freshConfig.get<TestIdentity[]>('identities', []); |
| 766 | + assert.strictEqual(afterDelete.length, 2, 'Should have 2 identities remaining'); |
| 767 | + assert.ok(afterDelete.some(i => i.id === 'test-keep-1'), 'First identity should remain'); |
| 768 | + assert.ok(afterDelete.some(i => i.id === 'test-keep-2'), 'Second identity should remain'); |
| 769 | + assert.ok(!afterDelete.some(i => i.id === 'test-delete-me'), 'Deleted identity should not exist'); |
| 770 | + |
| 771 | + // Restore original state |
| 772 | + await config.update('identities', currentIdentities, vscode.ConfigurationTarget.Global); |
| 773 | + }); |
| 774 | + |
| 775 | + it('should throw error for non-existent identity ID', async () => { |
| 776 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 777 | + const currentIdentities = config.get<TestIdentity[]>('identities', []); |
| 778 | + |
| 779 | + // Set up a known state |
| 780 | + const testIdentity: TestIdentity = { |
| 781 | + id: 'existing-identity', |
| 782 | + name: 'Existing User', |
| 783 | + email: 'existing@example.com', |
| 784 | + }; |
| 785 | + await config.update('identities', [testIdentity], vscode.ConfigurationTarget.Global); |
| 786 | + |
| 787 | + // Try to delete non-existent identity |
| 788 | + try { |
| 789 | + await deleteIdentityFromConfig('non-existent-id'); |
| 790 | + assert.fail('Should have thrown an error'); |
| 791 | + } catch (error) { |
| 792 | + assert.ok(error instanceof Error, 'Should throw an Error'); |
| 793 | + assert.ok( |
| 794 | + error.message.includes('not found'), |
| 795 | + `Error message should indicate identity not found, got: ${error.message}` |
| 796 | + ); |
| 797 | + } |
| 798 | + |
| 799 | + // Restore original state |
| 800 | + await config.update('identities', currentIdentities, vscode.ConfigurationTarget.Global); |
| 801 | + }); |
| 802 | + |
| 803 | + it('should throw error for empty string ID', async () => { |
| 804 | + try { |
| 805 | + await deleteIdentityFromConfig(''); |
| 806 | + assert.fail('Should have thrown an error'); |
| 807 | + } catch (error) { |
| 808 | + assert.ok(error instanceof Error, 'Should throw an Error'); |
| 809 | + assert.ok( |
| 810 | + error.message.includes('Invalid'), |
| 811 | + `Error message should indicate invalid ID, got: ${error.message}` |
| 812 | + ); |
| 813 | + } |
| 814 | + }); |
| 815 | + |
| 816 | + it('should throw error for invalid ID format', async () => { |
| 817 | + // Test with characters that are not allowed in identity IDs |
| 818 | + const invalidIds = [ |
| 819 | + 'id with spaces', |
| 820 | + 'id@with@symbols', |
| 821 | + 'id/with/slashes', |
| 822 | + 'id$with$dollar', |
| 823 | + ]; |
| 824 | + |
| 825 | + for (const invalidId of invalidIds) { |
| 826 | + try { |
| 827 | + await deleteIdentityFromConfig(invalidId); |
| 828 | + assert.fail(`Should have thrown an error for invalid ID: ${invalidId}`); |
| 829 | + } catch (error) { |
| 830 | + assert.ok(error instanceof Error, 'Should throw an Error'); |
| 831 | + assert.ok( |
| 832 | + error.message.includes('Invalid'), |
| 833 | + `Error message should indicate invalid ID format for "${invalidId}", got: ${error.message}` |
| 834 | + ); |
| 835 | + } |
| 836 | + } |
| 837 | + }); |
| 838 | + |
| 839 | + it('should throw error when deleting from empty identities array', async () => { |
| 840 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 841 | + const currentIdentities = config.get<TestIdentity[]>('identities', []); |
| 842 | + |
| 843 | + // Set empty identities array |
| 844 | + await config.update('identities', [], vscode.ConfigurationTarget.Global); |
| 845 | + |
| 846 | + // Try to delete from empty array |
| 847 | + try { |
| 848 | + await deleteIdentityFromConfig('any-valid-id'); |
| 849 | + assert.fail('Should have thrown an error'); |
| 850 | + } catch (error) { |
| 851 | + assert.ok(error instanceof Error, 'Should throw an Error'); |
| 852 | + assert.ok( |
| 853 | + error.message.includes('not found'), |
| 854 | + `Error message should indicate identity not found, got: ${error.message}` |
| 855 | + ); |
| 856 | + } |
| 857 | + |
| 858 | + // Restore original state |
| 859 | + await config.update('identities', currentIdentities, vscode.ConfigurationTarget.Global); |
| 860 | + }); |
| 861 | + }); |
709 | 862 | }); |
0 commit comments