fix(cli): address PR comments for /note command (v3)

This commit is contained in:
Sehoon Shon
2026-04-21 22:15:39 -07:00
parent f727139c72
commit affe6856f1
3 changed files with 69 additions and 62 deletions
@@ -33,8 +33,10 @@ describe('noteCommand', () => {
});
});
it('should return info message when no args provided and file does not exist', async () => {
vi.mocked(fs.readFile).mockRejectedValue(new Error('File not found'));
it('should return info message when no args provided and file does not exist (ENOENT)', async () => {
const error = new Error('File not found') as NodeJS.ErrnoException;
error.code = 'ENOENT';
vi.mocked(fs.readFile).mockRejectedValue(error);
const result = await noteCommand.action!(mockContext, ' ');
@@ -45,13 +47,30 @@ describe('noteCommand', () => {
});
});
it('should append note to file when args are provided', async () => {
const note = 'this is a new note';
it('should return error message when readFile fails with other error', async () => {
vi.mocked(fs.readFile).mockRejectedValue(new Error('Permission denied'));
const result = await noteCommand.action!(mockContext, '');
expect(result).toEqual({
type: 'message',
messageType: 'error',
content: expect.stringContaining(
'Failed to read notes: Permission denied',
),
});
});
it('should append trimmed note to file when args are provided', async () => {
const note = ' this is a new note ';
vi.mocked(fs.appendFile).mockResolvedValue(undefined);
const result = await noteCommand.action!(mockContext, note);
expect(fs.appendFile).toHaveBeenCalledWith(notesPath, `${note}\n`);
expect(fs.appendFile).toHaveBeenCalledWith(
notesPath,
`this is a new note\n`,
);
expect(result).toEqual({
type: 'message',
messageType: 'info',
+13 -4
View File
@@ -6,6 +6,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { isNodeError } from '@google/gemini-cli-core';
import { CommandKind, type SlashCommand } from './types.js';
export const noteCommand: SlashCommand = {
@@ -24,17 +25,25 @@ export const noteCommand: SlashCommand = {
messageType: 'info',
content: `Current notes in ${notesPath}:\n\n${content}`,
};
} catch {
} catch (error) {
if (isNodeError(error) && error.code === 'ENOENT') {
return {
type: 'message',
messageType: 'info',
content: 'No notes found. Use "/note <text>" to add one.',
};
}
return {
type: 'message',
messageType: 'info',
content: 'No notes found. Use "/note <text>" to add one.',
messageType: 'error',
content: `Failed to read notes: ${error instanceof Error ? error.message : String(error)}`,
};
}
}
try {
await fs.appendFile(notesPath, `${args}\n`);
const trimmedNote = args.trim();
await fs.appendFile(notesPath, `${trimmedNote}\n`);
return {
type: 'message',
messageType: 'info',