import { upsertPromptFeedback, clearPromptFeedback } from './promptFeedback';
import { axiosInstance } from '../../axiosApi';

jest.mock('../../axiosApi', () => ({
  axiosInstance: {
    post: jest.fn(),
    delete: jest.fn(),
  },
}));

describe('promptFeedback optimistic helpers', () => {
  const post = axiosInstance.post as jest.Mock;
  const del = axiosInstance.delete as jest.Mock;

  beforeEach(() => {
    post.mockReset();
    del.mockReset();
  });

  it('posts upsert payload', async () => {
    post.mockResolvedValue({ data: { rating: 'up' } });
    await upsertPromptFeedback(42, { rating: 'up' });
    expect(post).toHaveBeenCalledWith('prompt_feedback', {
      prompt_id: 42,
      rating: 'up',
      reason: undefined,
      comment: undefined,
    });
  });

  it('deletes vote by prompt_id', async () => {
    del.mockResolvedValue({});
    await clearPromptFeedback(42);
    expect(del).toHaveBeenCalledWith('prompt_feedback', {
      params: { prompt_id: 42 },
    });
  });

  it('surfaces request failures for rollback callers', async () => {
    post.mockRejectedValue(new Error('network'));
    await expect(upsertPromptFeedback(1, { rating: 'down' })).rejects.toThrow(
      'network',
    );
  });
});
