Skip to content
2 changes: 1 addition & 1 deletion packages/cli/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getApiKey } from './auth';
import { getApiKey } from './auth.ts';

const BASE_URL = 'https://jules.googleapis.com/v1alpha';

Expand Down
20 changes: 20 additions & 0 deletions packages/cli/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { JulesClient } from '../src/client.ts';
import assert from 'node:assert';
import { test } from 'node:test';

test('JulesClient constructor uses provided API key', () => {
const apiKey = 'test-api-key';
const client = new JulesClient(apiKey);
assert.strictEqual((client as any).apiKey, apiKey);
});

test('JulesClient constructor uses API key from environment if not provided', () => {
const originalEnvKey = process.env.JULES_API_KEY;
process.env.JULES_API_KEY = 'env-api-key';
try {
const client = new JulesClient();
assert.strictEqual((client as any).apiKey, 'env-api-key');
} finally {
process.env.JULES_API_KEY = originalEnvKey;
}
});
Comment thread
GreyC marked this conversation as resolved.
Outdated
Comment thread
GreyC marked this conversation as resolved.
Comment on lines +1 to +22
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 current tests cover providing the API key directly and via an environment variable, which is great. However, to ensure full coverage of the JulesClient constructor's behavior, I recommend adding tests for two other important scenarios:

  • When the API key is sourced from the configuration file.
  • When no API key is available from any source, which should result in an error being thrown.

This will make the test suite more robust and prevent future regressions related to API key handling.

import { JulesClient } from '../src/client';
import assert from 'node:assert';
import { test } from 'node:test';
import * as fs from 'fs';

test('JulesClient constructor uses provided API key', () => {
  const apiKey = 'test-api-key';
  const client = new JulesClient(apiKey);
  assert.strictEqual((client as any).apiKey, apiKey);
});

test('JulesClient constructor uses API key from environment if not provided', (t) => {
  const originalEnvKey = process.env.JULES_API_KEY;
  process.env.JULES_API_KEY = 'env-api-key';
  t.after(() => {
    process.env.JULES_API_KEY = originalEnvKey;
  });

  const client = new JulesClient();
  assert.strictEqual((client as any).apiKey, 'env-api-key');
});

test('JulesClient constructor uses API key from config file', (t) => {
  const originalEnvKey = process.env.JULES_API_KEY;
  delete process.env.JULES_API_KEY;
  t.after(() => {
    process.env.JULES_API_KEY = originalEnvKey;
  });

  t.mock.method(fs, 'existsSync', () => true);
  t.mock.method(fs, 'readFileSync', () => JSON.stringify({ apiKey: 'config-api-key' }));

  const client = new JulesClient();
  assert.strictEqual((client as any).apiKey, 'config-api-key');
});

test('JulesClient constructor throws when no API key is found', (t) => {
  const originalEnvKey = process.env.JULES_API_KEY;
  delete process.env.JULES_API_KEY;
  t.after(() => {
    process.env.JULES_API_KEY = originalEnvKey;
  });

  t.mock.method(fs, 'existsSync', () => false);

  assert.throws(
    () => {
      new JulesClient();
    },
    {
      message: 'No API key found. Set JULES_API_KEY or run: jules_cli setup',
    }
  );
});

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@jules Please add the two missing test cases suggested here:

  1. API key sourced from the config file (~/.config/jules/config.json) — mock the fs.readFileSync or the config reading logic to return a known key, and assert the constructor picks it up.
  2. Error thrown when no API key is available from any source — ensure no env var, no config file, no explicit arg, and assert that constructing JulesClient throws an error.

Do not add any temp directories. Only modify packages/cli/test/client.test.ts.

Loading