Skip to content
11 changes: 5 additions & 6 deletions packages/cli/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';

export function getApiKey(): string {
// 1. Check environment variable
Expand All @@ -23,7 +23,6 @@ export function getApiKey(): string {
console.error('Warning: could not read config file:', configPath);
}

// 3. Neither found, exit with error message
console.error('No API key found. Set JULES_API_KEY or run: jules_cli setup');
process.exit(1);
// 3. Neither found, throw error
throw new Error('No API key found. Set JULES_API_KEY or run: jules_cli setup');
}
66 changes: 66 additions & 0 deletions packages/cli/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { JulesClient } from '../src/client';

Choose a reason for hiding this comment

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

high

Your PR description mentions using node --experimental-strip-types, which requires explicit file extensions in import paths. To ensure the tests can run in that environment, you should add the .ts extension to this import.

Suggested change
import { JulesClient } from '../src/client';
import { JulesClient } from '../src/client.ts';

Copy link
Owner Author

Choose a reason for hiding this comment

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

We cannot add .ts extensions here — this was already tried and reverted in commits 77197d0 and 18b1090 because tsc errors with TS5097 ('An import path can only end with a .ts extension when allowImportingTsExtensions is enabled'). The project compiles with tsc without allowImportingTsExtensions, so .ts extensions in imports are not valid. The test runner used is node:test via tsc-compiled output, not --experimental-strip-types directly.

import assert from 'node:assert';
import { test } from 'node:test';
import fs from 'node:fs';
import os from 'node:os';

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');
});
Comment on lines +1 to +22

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
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.


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

const configPath = os.homedir() + '/.config/jules/config.json';

Choose a reason for hiding this comment

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

medium

For cross-platform compatibility, it's better to construct file paths using path.join() instead of string concatenation. This will ensure the tests run correctly on different operating systems (e.g., Windows) by using the correct path separator. You'll need to add import path from 'node:path'; at the top of the file.

Suggested change
const configPath = os.homedir() + '/.config/jules/config.json';
const configPath = path.join(os.homedir(), '.config', 'jules', 'config.json');

Copy link
Owner Author

Choose a reason for hiding this comment

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

@jules Please fix both this and the comment on line 57 (id:2934907534) together in one commit:

  1. Add import path from 'node:path'; to the imports in packages/cli/test/client.test.ts
  2. Line 31: replace string concatenation with path.join(os.homedir(), '.config', 'jules', 'config.json')
  3. Line 57: replace path.includes('.config/jules/config.json') with an exact match: path === path.join(os.homedir(), '.config', 'jules', 'config.json')

Copy link
Contributor

Choose a reason for hiding this comment

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

I've implemented the requested path handling improvements in packages/cli/test/client.test.ts. Specifically, I added the node:path import, used path.join() for the configuration file path, and updated the existsSync mock to use an exact path match for better robustness. All tests, including the new edge cases, are passing correctly.


t.mock.method(fs, 'existsSync', (path: string) => {
if (path === configPath) return true;
return false;
});

t.mock.method(fs, 'readFileSync', (path: string) => {
if (path === configPath) {
return JSON.stringify({ apiKey: 'config-api-key' });
}
throw new Error('File not found');
});

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

test('JulesClient constructor throws error if no API key is found anywhere', (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', (path: string) => {
if (path.includes('.config/jules/config.json')) return false;

Choose a reason for hiding this comment

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

medium

Using path.includes() for this check is brittle and might lead to false positives. It's more robust to perform an exact match against the full path. Constructing the path with path.join() also ensures cross-platform compatibility. Note that this requires importing the path module.

Suggested change
if (path.includes('.config/jules/config.json')) return false;
if (path === path.join(os.homedir(), '.config', 'jules', 'config.json')) return false;

return true; // allow other existsSync calls to proceed normally if any
});

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