/**
* Unit tests for check_job_status tool
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { checkJobStatusTool, handleCheckJobStatus } from '../../../src/tools/check-job-status.js';
// Mock the http module
vi.mock('../../../src/utils/http.js', () => ({
httpGet: vi.fn(),
buildApiUrl: vi.fn((endpoint) => `https://api.ircamamplify.io/v1${endpoint}`),
}));
// Mock auth
vi.mock('../../../src/utils/auth.js', () => ({
getApiKey: vi.fn(() => 'test-api-key'),
hasApiKey: vi.fn(() => true),
createAuthHeaders: vi.fn(() => ({
Authorization: 'Bearer test-api-key',
'Content-Type': 'application/json',
})),
API_KEY_ENV_VAR: 'IRCAM_AMPLIFY_API_KEY',
}));
import { httpGet } from '../../../src/utils/http.js';
describe('check_job_status tool', () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
describe('tool definition', () => {
it('should have correct name', () => {
expect(checkJobStatusTool.name).toBe('check_job_status');
});
it('should have description about job status', () => {
expect(checkJobStatusTool.description).toBeTruthy();
expect(checkJobStatusTool.description).toContain('status');
expect(checkJobStatusTool.description).toContain('job');
});
it('should require job_id parameter', () => {
expect(checkJobStatusTool.inputSchema).toBeDefined();
expect(checkJobStatusTool.inputSchema.required).toContain('job_id');
});
});
describe('handleCheckJobStatus', () => {
it('should return pending status', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: 'job-123',
status: 'pending',
progress: 0,
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
const result = await handleCheckJobStatus({
job_id: 'job-123',
});
expect(result.status).toBe('pending');
expect(result.progress).toBe(0);
});
it('should return processing status with progress', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: 'job-123',
status: 'processing',
progress: 45,
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
const result = await handleCheckJobStatus({
job_id: 'job-123',
});
expect(result.status).toBe('processing');
expect(result.progress).toBe(45);
});
it('should return completed status with stem results', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: 'job-123',
status: 'completed',
progress: 100,
result: {
stems: {
vocals: { url: 'https://cdn.example.com/vocals.wav' },
drums: { url: 'https://cdn.example.com/drums.wav' },
bass: { url: 'https://cdn.example.com/bass.wav' },
other: { url: 'https://cdn.example.com/other.wav' },
},
},
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
const result = await handleCheckJobStatus({
job_id: 'job-123',
});
expect(result.status).toBe('completed');
expect(result.result).toEqual({
vocals_url: 'https://cdn.example.com/vocals.wav',
drums_url: 'https://cdn.example.com/drums.wav',
bass_url: 'https://cdn.example.com/bass.wav',
other_url: 'https://cdn.example.com/other.wav',
});
});
it('should return failed status with error message', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: 'job-123',
status: 'failed',
error: 'Audio file is corrupted',
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
const result = await handleCheckJobStatus({
job_id: 'job-123',
});
expect(result.status).toBe('failed');
expect(result.error).toBe('Audio file is corrupted');
});
it('should throw error for empty job_id', async () => {
await expect(
handleCheckJobStatus({ job_id: '' })
).rejects.toMatchObject({
code: 'JOB_NOT_FOUND',
});
});
it('should throw error for invalid job_id format', async () => {
await expect(
handleCheckJobStatus({ job_id: 'invalid job id!' })
).rejects.toMatchObject({
code: 'JOB_NOT_FOUND',
});
});
it('should throw error when job not found', async () => {
const mockError = {
ok: false,
status: 404,
error: {
code: 'JOB_NOT_FOUND',
message: 'Job not found',
suggestion: 'Check the job ID',
},
};
vi.mocked(httpGet).mockResolvedValue(mockError);
await expect(
handleCheckJobStatus({ job_id: 'nonexistent-job' })
).rejects.toMatchObject({
code: 'JOB_NOT_FOUND',
});
});
it('should call API for job status', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: 'job-abc-123',
status: 'pending',
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
await handleCheckJobStatus({
job_id: 'job-abc-123',
});
expect(httpGet).toHaveBeenCalled();
});
it('should handle UUID job IDs', async () => {
const mockResponse = {
ok: true,
status: 200,
data: {
job_id: '550e8400-e29b-41d4-a716-446655440000',
status: 'processing',
progress: 75,
},
};
vi.mocked(httpGet).mockResolvedValue(mockResponse);
const result = await handleCheckJobStatus({
job_id: '550e8400-e29b-41d4-a716-446655440000',
});
expect(result.status).toBe('processing');
expect(result.progress).toBe(75);
});
});
});