|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const { |
| 4 | + mockReadPdfFile, |
| 5 | + mockRequestJson, |
| 6 | + mockUploadPresignedPdf, |
| 7 | + mockValidateHelpfulness, |
| 8 | + mockParseYesNo, |
| 9 | +} = vi.hoisted(() => ({ |
| 10 | + mockReadPdfFile: vi.fn(), |
| 11 | + mockRequestJson: vi.fn(), |
| 12 | + mockUploadPresignedPdf: vi.fn(), |
| 13 | + mockValidateHelpfulness: vi.fn(), |
| 14 | + mockParseYesNo: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('./utils.js', async () => { |
| 18 | + const actual = await vi.importActual<typeof import('./utils.js')>('./utils.js'); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + readPdfFile: mockReadPdfFile, |
| 22 | + requestJson: mockRequestJson, |
| 23 | + uploadPresignedPdf: mockUploadPresignedPdf, |
| 24 | + validateHelpfulness: mockValidateHelpfulness, |
| 25 | + parseYesNo: mockParseYesNo, |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +import { getRegistry } from '../../registry.js'; |
| 30 | +import './submit.js'; |
| 31 | +import './review.js'; |
| 32 | +import './feedback.js'; |
| 33 | + |
| 34 | +describe('paperreview submit command', () => { |
| 35 | + beforeEach(() => { |
| 36 | + mockReadPdfFile.mockReset(); |
| 37 | + mockRequestJson.mockReset(); |
| 38 | + mockUploadPresignedPdf.mockReset(); |
| 39 | + mockValidateHelpfulness.mockReset(); |
| 40 | + mockParseYesNo.mockReset(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('supports dry run without any remote request', async () => { |
| 44 | + const cmd = getRegistry().get('paperreview/submit'); |
| 45 | + expect(cmd?.func).toBeTypeOf('function'); |
| 46 | + |
| 47 | + mockReadPdfFile.mockResolvedValue({ |
| 48 | + buffer: Buffer.from('%PDF'), |
| 49 | + fileName: 'paper.pdf', |
| 50 | + resolvedPath: '/tmp/paper.pdf', |
| 51 | + sizeBytes: 4096, |
| 52 | + }); |
| 53 | + |
| 54 | + const result = await cmd!.func!(null as any, { |
| 55 | + pdf: './paper.pdf', |
| 56 | + email: 'wang2629651228@gmail.com', |
| 57 | + venue: 'RAL', |
| 58 | + 'dry-run': true, |
| 59 | + 'prepare-only': false, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(mockRequestJson).not.toHaveBeenCalled(); |
| 63 | + expect(result).toMatchObject({ |
| 64 | + status: 'dry-run', |
| 65 | + file: 'paper.pdf', |
| 66 | + email: 'wang2629651228@gmail.com', |
| 67 | + venue: 'RAL', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('requests an upload URL, uploads the PDF, and confirms the submission', async () => { |
| 72 | + const cmd = getRegistry().get('paperreview/submit'); |
| 73 | + expect(cmd?.func).toBeTypeOf('function'); |
| 74 | + |
| 75 | + mockReadPdfFile.mockResolvedValue({ |
| 76 | + buffer: Buffer.from('%PDF'), |
| 77 | + fileName: 'paper.pdf', |
| 78 | + resolvedPath: '/tmp/paper.pdf', |
| 79 | + sizeBytes: 4096, |
| 80 | + }); |
| 81 | + mockRequestJson |
| 82 | + .mockResolvedValueOnce({ |
| 83 | + response: { ok: true, status: 200 } as Response, |
| 84 | + payload: { |
| 85 | + success: true, |
| 86 | + presigned_url: 'https://upload.example.com', |
| 87 | + presigned_fields: { key: 'uploads/paper.pdf' }, |
| 88 | + s3_key: 'uploads/paper.pdf', |
| 89 | + }, |
| 90 | + }) |
| 91 | + .mockResolvedValueOnce({ |
| 92 | + response: { ok: true, status: 200 } as Response, |
| 93 | + payload: { |
| 94 | + success: true, |
| 95 | + token: 'tok_123', |
| 96 | + message: 'Submission accepted', |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const result = await cmd!.func!(null as any, { |
| 101 | + pdf: './paper.pdf', |
| 102 | + email: 'wang2629651228@gmail.com', |
| 103 | + venue: 'RAL', |
| 104 | + 'dry-run': false, |
| 105 | + 'prepare-only': false, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(mockRequestJson).toHaveBeenNthCalledWith(1, '/api/get-upload-url', expect.objectContaining({ |
| 109 | + method: 'POST', |
| 110 | + body: JSON.stringify({ |
| 111 | + filename: 'paper.pdf', |
| 112 | + venue: 'RAL', |
| 113 | + }), |
| 114 | + })); |
| 115 | + expect(mockUploadPresignedPdf).toHaveBeenCalledWith( |
| 116 | + 'https://upload.example.com', |
| 117 | + expect.objectContaining({ fileName: 'paper.pdf' }), |
| 118 | + expect.objectContaining({ s3_key: 'uploads/paper.pdf' }), |
| 119 | + ); |
| 120 | + expect(mockRequestJson).toHaveBeenNthCalledWith(2, '/api/confirm-upload', expect.objectContaining({ |
| 121 | + method: 'POST', |
| 122 | + body: expect.any(FormData), |
| 123 | + })); |
| 124 | + expect(result).toMatchObject({ |
| 125 | + status: 'submitted', |
| 126 | + token: 'tok_123', |
| 127 | + review_url: 'https://paperreview.ai/review?token=tok_123', |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('paperreview review command', () => { |
| 133 | + beforeEach(() => { |
| 134 | + mockRequestJson.mockReset(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('returns processing status when the review is not ready yet', async () => { |
| 138 | + const cmd = getRegistry().get('paperreview/review'); |
| 139 | + expect(cmd?.func).toBeTypeOf('function'); |
| 140 | + |
| 141 | + mockRequestJson.mockResolvedValue({ |
| 142 | + response: { status: 202 } as Response, |
| 143 | + payload: { detail: 'Review is still processing.' }, |
| 144 | + }); |
| 145 | + |
| 146 | + const result = await cmd!.func!(null as any, { token: 'tok_123' }); |
| 147 | + |
| 148 | + expect(result).toMatchObject({ |
| 149 | + status: 'processing', |
| 150 | + token: 'tok_123', |
| 151 | + review_url: 'https://paperreview.ai/review?token=tok_123', |
| 152 | + message: 'Review is still processing.', |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe('paperreview feedback command', () => { |
| 158 | + beforeEach(() => { |
| 159 | + mockRequestJson.mockReset(); |
| 160 | + mockValidateHelpfulness.mockReset(); |
| 161 | + mockParseYesNo.mockReset(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('normalizes feedback inputs and posts them to the API', async () => { |
| 165 | + const cmd = getRegistry().get('paperreview/feedback'); |
| 166 | + expect(cmd?.func).toBeTypeOf('function'); |
| 167 | + |
| 168 | + mockValidateHelpfulness.mockReturnValue(4); |
| 169 | + mockParseYesNo.mockReturnValueOnce(true).mockReturnValueOnce(false); |
| 170 | + mockRequestJson.mockResolvedValue({ |
| 171 | + response: { ok: true, status: 200 } as Response, |
| 172 | + payload: { message: 'Thanks for the feedback.' }, |
| 173 | + }); |
| 174 | + |
| 175 | + const result = await cmd!.func!(null as any, { |
| 176 | + token: 'tok_123', |
| 177 | + helpfulness: 4, |
| 178 | + 'critical-error': 'yes', |
| 179 | + 'actionable-suggestions': 'no', |
| 180 | + 'additional-comments': 'Helpful summary, but the contribution section needs more detail.', |
| 181 | + }); |
| 182 | + |
| 183 | + expect(mockRequestJson).toHaveBeenCalledWith('/api/feedback/tok_123', { |
| 184 | + method: 'POST', |
| 185 | + headers: { 'Content-Type': 'application/json' }, |
| 186 | + body: JSON.stringify({ |
| 187 | + helpfulness: 4, |
| 188 | + has_critical_error: true, |
| 189 | + has_actionable_suggestions: false, |
| 190 | + additional_comments: 'Helpful summary, but the contribution section needs more detail.', |
| 191 | + }), |
| 192 | + }); |
| 193 | + expect(result).toMatchObject({ |
| 194 | + status: 'submitted', |
| 195 | + token: 'tok_123', |
| 196 | + helpfulness: 4, |
| 197 | + critical_error: true, |
| 198 | + actionable_suggestions: false, |
| 199 | + message: 'Thanks for the feedback.', |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments