openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
119 lines (99 loc) • 4.62 kB
JavaScript
import { test, describe, expect, vi } from 'vitest';
import { parseCommands, getCommand } from './command.mjs';
// Tests for parseCommands
test('returns same input when no command present', () => {
const input = 'This is a test';
const result = parseCommands(input);
expect(result).toEqual({ commandLine: input });
});
// Test processing command with a numeric value
test('processes command with number correctly', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const input = ':v12 remaining text';
const result = parseCommands(input);
expect(result).toEqual({ vectorSearch: 12, commandLine: 'remaining text' });
expect(logSpy).toHaveBeenCalledWith('[!] Command: vectorSearch activated with value: 12');
logSpy.mockRestore();
});
// Test processing command without a numeric value
test('processes command without number correctly', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const input = ':v remaining text';
const result = parseCommands(input);
expect(result).toEqual({ vectorSearch: 0, commandLine: 'remaining text' });
expect(logSpy).toHaveBeenCalledWith('[!] Command: vectorSearch activated');
logSpy.mockRestore();
});
// Test processing multiple commands consecutively
test('processes multiple commands consecutively', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const input = ':v12 :v34 remaining text';
const result = parseCommands(input);
expect(result).toEqual({ vectorSearch: 34, commandLine: 'remaining text' });
expect(logSpy).toHaveBeenNthCalledWith(1, '[!] Command: vectorSearch activated with value: 12');
expect(logSpy).toHaveBeenNthCalledWith(2, '[!] Command: vectorSearch activated with value: 34');
logSpy.mockRestore();
});
// Tests for getCommand
describe('getCommand', () => {
test('returns defaultValue when the command is defined as null', () => {
const commands = { test: null };
const defaultValue = 'default';
expect(getCommand(commands, 'test', defaultValue)).toBe(defaultValue);
});
test('returns numeric value when the command is defined with a number', () => {
const commands = { test: 42 };
expect(getCommand(commands, 'test', 'default')).toBe(42);
});
test('returns null when the command is missing', () => {
const commands = {};
// Since the implementation returns null if the command key is not present
expect(getCommand(commands, 'missing', 'default')).toBeNull();
});
test('returns "undefined" when the command value is the string "undefined"', () => {
const commands = { test: "undefined" };
// The implementation does not convert the string, so it returns "undefined"
expect(getCommand(commands, 'test', 'default')).toBe("undefined");
});
test('returns string value when the command is a non-"undefined" string', () => {
const commands = { test: "foobar" };
expect(getCommand(commands, 'test', 'default')).toBe("foobar");
});
test('returns 0 when the command value is 0', () => {
const commands = { test: 0 };
expect(getCommand(commands, 'test', 'default')).toBe(0);
});
// Additional edge case tests
test('returns boolean true when the command value is true', () => {
const commands = { test: true };
expect(getCommand(commands, 'test', 'default')).toBe(true);
});
test('returns boolean false when the command value is false', () => {
const commands = { test: false };
expect(getCommand(commands, 'test', 'default')).toBe(false);
});
test('returns empty string when the command value is an empty string', () => {
const commands = { test: "" };
expect(getCommand(commands, 'test', 'default')).toBe("");
});
test('returns object when the command value is an object', () => {
const obj = { key: 'value' };
const commands = { test: obj };
expect(getCommand(commands, 'test', 'default')).toEqual(obj);
});
test('returns array when the command value is an array', () => {
const arr = [1, 2, 3];
const commands = { test: arr };
expect(getCommand(commands, 'test', 'default')).toEqual(arr);
});
test('returns Date object when the command value is a Date', () => {
const date = new Date('2025-03-20T00:00:00Z');
const commands = { test: date };
expect(getCommand(commands, 'test', 'default')).toBe(date);
});
test('returns NaN when the command value is NaN', () => {
const commands = { test: Number.NaN };
const result = getCommand(commands, 'test', 'default');
expect(Number.isNaN(result)).toBe(true);
});
});