Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions extensions/git-id-switcher/src/test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
isFatalError,
getUserSafeMessage,
} from '../core/errors';
import { toFieldError } from '../core/validation-types';

/**
* Test SecurityError constructor
Expand Down Expand Up @@ -393,71 +392,6 @@ function testGetUserSafeMessageFunction(): void {
console.log('✅ getUserSafeMessage function tests passed!');
}

/**
* Test toFieldError function
*/
function testToFieldError(): void {
console.log('Testing toFieldError...');

// Should parse colon-separated format
{
const result = toFieldError('email: Invalid format');
assert.strictEqual(result.field, 'email');
assert.strictEqual(result.message, 'Invalid format');
}

// Should handle no colon (plain error message)
{
const result = toFieldError('Unknown error');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, 'Unknown error');
}

// Should handle empty string
{
const result = toFieldError('');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, '');
}

// Should handle multiple colons (only split on first)
{
const result = toFieldError('path: /home/user: invalid');
assert.strictEqual(result.field, 'path');
assert.strictEqual(result.message, '/home/user: invalid');
}

// Should handle colon with no message
{
const result = toFieldError('field:');
assert.strictEqual(result.field, 'field');
assert.strictEqual(result.message, '');
}

// Should handle colon at the beginning (empty field)
{
const result = toFieldError(': message only');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, ': message only');
}

// Should trim whitespace
{
const result = toFieldError(' name : has spaces ');
assert.strictEqual(result.field, 'name');
assert.strictEqual(result.message, 'has spaces');
}

// Should handle whitespace-only field name
{
const result = toFieldError(' : message');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, ' : message');
}

console.log('✅ toFieldError tests passed!');
}

/**
* Test isFatalError function
*/
Expand Down Expand Up @@ -519,7 +453,6 @@ export async function runErrorTests(): Promise<void> {
testTypeGuards();
testIsFatalError();
testGetUserSafeMessageFunction();
testToFieldError();

console.log('\n✅ All error tests passed!\n');
} catch (error) {
Expand Down
6 changes: 5 additions & 1 deletion extensions/git-id-switcher/src/test/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { runDisplayLimitsTests } from './displayLimits.test';
import { runSshAgentParsingTests } from './sshAgentParsing.test';
import { runSyncCheckerTests } from './syncChecker.test';
import { runErrorTests } from './errors.test';
import { runValidationTypesTests } from './validation-types.test';
import { runGetSafeStackTests } from './getSafeStack.test';
import { runHtmlTemplatesTests } from './htmlTemplates.test';
import { runMarkdownEscapeTests } from './markdownEscape.test';
Expand Down Expand Up @@ -119,9 +120,12 @@ async function main(): Promise<void> {
// Run sync checker tests (profile vs git config comparison)
await runSyncCheckerTests();

// Run error classes and validation types tests
// Run error classes tests
await runErrorTests();

// Run validation-types tests (toFieldError)
await runValidationTypesTests();

// Run getSafeStack cross-OS path sanitization tests
await runGetSafeStackTests();

Expand Down
92 changes: 92 additions & 0 deletions extensions/git-id-switcher/src/test/validation-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Tests for validation-types module (toFieldError)
*/

import * as assert from 'node:assert';
import { toFieldError } from '../core/validation-types';

/**
* Test toFieldError function
*/
function testToFieldError(): void {
console.log('Testing toFieldError...');

// Should parse colon-separated format
{
const result = toFieldError('email: Invalid format');
assert.strictEqual(result.field, 'email');
assert.strictEqual(result.message, 'Invalid format');
}

// Should handle no colon (plain error message)
{
const result = toFieldError('Unknown error');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, 'Unknown error');
}

// Should handle empty string
{
const result = toFieldError('');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, '');
}

// Should handle multiple colons (only split on first)
{
const result = toFieldError('path: /home/user: invalid');
assert.strictEqual(result.field, 'path');
assert.strictEqual(result.message, '/home/user: invalid');
}

// Should handle colon with no message
{
const result = toFieldError('field:');
assert.strictEqual(result.field, 'field');
assert.strictEqual(result.message, '');
}

// Should handle colon at the beginning (empty field)
{
const result = toFieldError(': message only');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, ': message only');
}

// Should trim whitespace
{
const result = toFieldError(' name : has spaces ');
assert.strictEqual(result.field, 'name');
assert.strictEqual(result.message, 'has spaces');
}

// Should handle whitespace-only field name
{
const result = toFieldError(' : message');
assert.strictEqual(result.field, 'unknown');
assert.strictEqual(result.message, ' : message');
}

console.log('✅ toFieldError tests passed!');
}

/**
* Run all validation-types tests
*/
export async function runValidationTypesTests(): Promise<void> {
console.log('\n=== Validation Types Tests ===\n');

try {
testToFieldError();

console.log('\n✅ All validation-types tests passed!\n');
} catch (error) {
console.error('\n❌ Test failed:', error instanceof Error ? error.message : String(error));
process.exit(1);
}
}

// Run tests when executed directly
if (require.main === module) {
runValidationTypesTests().catch(console.error);
}
Loading