Skip to content
Open
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
71 changes: 70 additions & 1 deletion test/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { validateMessage, validateApiKey, sanitizeAgentId, checkRateLimit, sanitizeMessage } from '../src/protocol/validation.js';
import { validateMessage, validateApiKey, sanitizeAgentId, checkRateLimit, sanitizeMessage, parseApiKeys, ApiKeyScope } from '../src/protocol/validation.js';
import { DAPMessageSchema } from '../src/protocol/types.js';

describe('validateMessage', () => {
Expand Down Expand Up @@ -73,6 +73,75 @@ describe('validateMessage', () => {
});
});

describe('parseApiKeys', () => {
it('should return empty array for undefined or empty input', () => {
expect(parseApiKeys(undefined)).toEqual([]);
expect(parseApiKeys('')).toEqual([]);
expect(parseApiKeys(' ')).toEqual([]);
});

it('should parse single key without scope (defaults to read)', () => {
const result = parseApiKeys('my-key');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('my-key');
expect(result[0].scope).toBe(ApiKeyScope.READ);
expect(result[0].description).toBe('Key ending in ...-key');
expect(result[0].createdAt).toBeDefined();
});
Comment on lines +83 to +90

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to add a test case to verify how parseApiKeys handles short keys (less than 4 characters) when generating the key description. This ensures that the slice(-4) logic does not cause any unexpected behavior or crashes for short keys.

Suggested change
it('should parse single key without scope (defaults to read)', () => {
const result = parseApiKeys('my-key');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('my-key');
expect(result[0].scope).toBe(ApiKeyScope.READ);
expect(result[0].description).toBe('Key ending in ...-key');
expect(result[0].createdAt).toBeDefined();
});
it('should parse single key without scope (defaults to read)', () => {
const result = parseApiKeys('my-key');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('my-key');
expect(result[0].scope).toBe(ApiKeyScope.READ);
expect(result[0].description).toBe('Key ending in ...-key');
expect(result[0].createdAt).toBeDefined();
});
it('should handle short keys (less than 4 characters) for description', () => {
const result = parseApiKeys('abc');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('abc');
expect(result[0].description).toBe('Key ending in ...abc');
});


it('should parse single key with valid scope', () => {
const result = parseApiKeys('admin-key:admin');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('admin-key');
expect(result[0].scope).toBe(ApiKeyScope.ADMIN);
});

it('should fallback to read scope for invalid scope', () => {
const result = parseApiKeys('some-key:super-admin');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('some-key');
expect(result[0].scope).toBe(ApiKeyScope.READ);
});

it('should parse multiple keys with varying scopes', () => {
const result = parseApiKeys('key1,key2:write,key3:admin,key4:invalid');
expect(result).toHaveLength(4);

expect(result[0].key).toBe('key1');
expect(result[0].scope).toBe(ApiKeyScope.READ);

expect(result[1].key).toBe('key2');
expect(result[1].scope).toBe(ApiKeyScope.WRITE);

expect(result[2].key).toBe('key3');
expect(result[2].scope).toBe(ApiKeyScope.ADMIN);

expect(result[3].key).toBe('key4');
expect(result[3].scope).toBe(ApiKeyScope.READ);
});

it('should handle spaces in the input', () => {
const result = parseApiKeys(' key1 , key2:write ');
expect(result).toHaveLength(2);
expect(result[0].key).toBe('key1');
expect(result[1].key).toBe('key2');
expect(result[1].scope).toBe(ApiKeyScope.WRITE);
});

it('should handle malformed lists (empty entries)', () => {
const result = parseApiKeys('key1,,key2,,,key3:write');
expect(result).toHaveLength(3);
expect(result.map(r => r.key)).toEqual(['key1', 'key2', 'key3']);
});
Comment on lines +131 to +135

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test currently only asserts the parsed keys but does not verify that their scopes are correctly parsed or defaulted. Asserting the full object structure (including the scopes) makes the test more robust and ensures that the scope parsing logic works as expected when handling malformed lists with empty entries.

  it('should handle malformed lists (empty entries)', () => {
    const result = parseApiKeys('key1,,key2,,,key3:write');
    expect(result).toHaveLength(3);
    expect(result.map(r => ({ key: r.key, scope: r.scope }))).toEqual([
      { key: 'key1', scope: ApiKeyScope.READ },
      { key: 'key2', scope: ApiKeyScope.READ },
      { key: 'key3', scope: ApiKeyScope.WRITE }
    ]);
  });


it('should handle keys with multiple colons correctly', () => {
const result = parseApiKeys('complex:key:name:write');
expect(result).toHaveLength(1);
expect(result[0].key).toBe('complex');
expect(result[0].scope).toBe(ApiKeyScope.READ); // 'key:name:write' is invalid scope
});
});

describe('validateApiKey', () => {
it('should validate matching key', () => {
const validKeys = ['key1', 'key2', 'key3'];
Expand Down