Skip to content

🧪 [testing improvement] Add tests for parseApiKeys#19

Open
anusornc wants to merge 1 commit into
mainfrom
add-parse-api-keys-tests-3993730500057671196
Open

🧪 [testing improvement] Add tests for parseApiKeys#19
anusornc wants to merge 1 commit into
mainfrom
add-parse-api-keys-tests-3993730500057671196

Conversation

@anusornc

@anusornc anusornc commented Jun 9, 2026

Copy link
Copy Markdown
Owner

🎯 What: Added tests for the parseApiKeys function in src/protocol/validation.ts since it lacked test coverage.

📊 Coverage: Covered the following scenarios:

  • Empty, undefined, and whitespace-only inputs.
  • Valid single keys with and without explicitly defined scopes.
  • Single keys with invalid scopes, ensuring fallback logic defaults to the read scope.
  • Multiple keys separated by commas with mixed scopes.
  • Handling malformed inputs with extra spaces and missing entries.
  • Edge cases such as multiple colons in the key format.

Result: Improved test coverage and reliability for parsing and processing API keys, protecting the function against regressions.


PR created automatically by Jules for task 3993730500057671196 started by @anusornc

Co-authored-by: anusornc <11565019+anusornc@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces comprehensive unit tests for the 'parseApiKeys' function in 'test/validation.test.ts', covering various scenarios such as empty inputs, single and multiple keys with different scopes, malformed lists, and whitespace handling. The review feedback suggests adding a test case to verify how the function handles short keys (fewer than 4 characters) when generating key descriptions, and recommends strengthening the malformed list test by asserting the full object structure, including scopes, rather than just the keys.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread test/validation.test.ts
Comment on lines +83 to +90
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();
});

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');
});

Comment thread test/validation.test.ts
Comment on lines +131 to +135
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']);
});

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 }
    ]);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant