|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | +import { runInference } from './run_inference'; |
| 3 | +import { Task } from './tasks'; |
| 4 | +import { HfInference, HfInferenceEndpoint } from '@huggingface/inference'; |
| 5 | +import { DocumentSnapshot } from 'firebase-admin/firestore'; |
| 6 | + |
| 7 | +const inference = new HfInference('test'); |
| 8 | + |
| 9 | +jest.mock('@huggingface/inference', () => ({ |
| 10 | + HfInference: jest.fn().mockImplementation(() => ({ |
| 11 | + fillMask: jest.fn(), |
| 12 | + summarization: jest.fn(), |
| 13 | + questionAnswering: jest.fn(), |
| 14 | + tableQuestionAnswering: jest.fn(), |
| 15 | + })), |
| 16 | + HfInferenceEndpoint: jest.fn().mockImplementation(() => ({ |
| 17 | + fillMask: jest.fn(), |
| 18 | + summarization: jest.fn(), |
| 19 | + questionAnswering: jest.fn(), |
| 20 | + tableQuestionAnswering: jest.fn(), |
| 21 | + })), |
| 22 | +})); |
| 23 | + |
| 24 | +describe(Task.fillMask, () => { |
| 25 | + test('should throw an error if inputs are not provided', async () => { |
| 26 | + const snapshot = { |
| 27 | + data: () => ({ |
| 28 | + inputs: undefined, |
| 29 | + }), |
| 30 | + } as any; |
| 31 | + |
| 32 | + await expect( |
| 33 | + runInference(snapshot, Task.fillMask, inference), |
| 34 | + ).rejects.toThrow(Error); |
| 35 | + }); |
| 36 | + |
| 37 | + test('should throw an error if inputs are not a string', async () => { |
| 38 | + const snapshot = { |
| 39 | + data: () => ({ |
| 40 | + inputs: 123, |
| 41 | + }), |
| 42 | + } as any; |
| 43 | + |
| 44 | + await expect( |
| 45 | + runInference(snapshot, Task.fillMask, inference), |
| 46 | + ).rejects.toThrow(Error); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe(Task.summarization, () => { |
| 51 | + test('should throw an error if inputs are not provided', async () => { |
| 52 | + const snapshot = { |
| 53 | + data: () => ({ |
| 54 | + inputs: undefined, |
| 55 | + }), |
| 56 | + } as any; |
| 57 | + |
| 58 | + await expect( |
| 59 | + runInference(snapshot, Task.summarization, inference), |
| 60 | + ).rejects.toThrow(Error); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should throw an error if inputs are not a string', async () => { |
| 64 | + const snapshot = { |
| 65 | + data: () => ({ |
| 66 | + inputs: 123, |
| 67 | + }), |
| 68 | + } as any; |
| 69 | + |
| 70 | + await expect( |
| 71 | + runInference(snapshot, Task.summarization, inference), |
| 72 | + ).rejects.toThrow(Error); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +// Testing questionAnswering task. |
| 77 | +describe(Task.questionAnswering, () => { |
| 78 | + let mockTask: Task; |
| 79 | + let inference: HfInference | HfInferenceEndpoint; |
| 80 | + |
| 81 | + beforeAll(() => { |
| 82 | + mockTask = Task.questionAnswering; |
| 83 | + inference = new HfInference(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should run without errors', async () => { |
| 87 | + const snapshot = { |
| 88 | + data: () => ({ |
| 89 | + question: 'Who are you?', |
| 90 | + context: 'You are a cat.', |
| 91 | + }), |
| 92 | + } as any; |
| 93 | + |
| 94 | + await expect( |
| 95 | + runInference(snapshot, mockTask, inference), |
| 96 | + ).resolves.not.toThrow(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw if `question` and `context` are not provided', async () => { |
| 100 | + const snapshot = { |
| 101 | + data: () => ({}), |
| 102 | + } as any; |
| 103 | + |
| 104 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should throw if `question` is not a string', async () => { |
| 108 | + const snapshot = { |
| 109 | + data: () => ({ |
| 110 | + question: 123, |
| 111 | + }), |
| 112 | + } as any; |
| 113 | + |
| 114 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should throw if `context` is not a string', async () => { |
| 118 | + const snapshot = { |
| 119 | + data: () => ({ |
| 120 | + context: 123, |
| 121 | + }), |
| 122 | + } as any; |
| 123 | + |
| 124 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should run with inference endpoint', async () => { |
| 128 | + const snapshot = { |
| 129 | + data: () => ({ |
| 130 | + question: 'Who are you?', |
| 131 | + context: 'You are a cat.', |
| 132 | + }), |
| 133 | + } as any; |
| 134 | + |
| 135 | + inference = new HfInferenceEndpoint('https://endpoint-url.com'); |
| 136 | + |
| 137 | + await expect( |
| 138 | + runInference(snapshot, mockTask, inference), |
| 139 | + ).resolves.not.toThrow(); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +// Testing tableQuestionAnswering task. |
| 144 | +describe(Task.tableQuestionAnswering, () => { |
| 145 | + let mockTask: Task; |
| 146 | + let inference: HfInference | HfInferenceEndpoint; |
| 147 | + const snapshot = { |
| 148 | + data: () => ({ |
| 149 | + inputs: { |
| 150 | + query: 'Who are you?', |
| 151 | + table: { |
| 152 | + name: ['John', 'Mary'], |
| 153 | + age: ['20', '30'], |
| 154 | + }, |
| 155 | + }, |
| 156 | + }), |
| 157 | + } as any; |
| 158 | + |
| 159 | + beforeAll(() => { |
| 160 | + mockTask = Task.tableQuestionAnswering; |
| 161 | + inference = new HfInference(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should run without errors', async () => { |
| 165 | + await expect( |
| 166 | + runInference(snapshot, mockTask, inference), |
| 167 | + ).resolves.not.toThrow(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should throw if `question` and `table` are not provided', async () => { |
| 171 | + const snapshot = { |
| 172 | + data: () => ({}), |
| 173 | + } as any; |
| 174 | + |
| 175 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should throw if `query` is not a string', async () => { |
| 179 | + const snapshot = { |
| 180 | + data: () => ({ |
| 181 | + inputs: { |
| 182 | + query: 123, |
| 183 | + table: { |
| 184 | + name: ['John', 'Mary'], |
| 185 | + age: ['20', '30'], |
| 186 | + }, |
| 187 | + }, |
| 188 | + }), |
| 189 | + } as any; |
| 190 | + |
| 191 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should throw if `table` is not valid', async () => { |
| 195 | + const snapshot = { |
| 196 | + data: () => ({ |
| 197 | + inputs: { |
| 198 | + query: '123', |
| 199 | + table: 123, |
| 200 | + }, |
| 201 | + }), |
| 202 | + } as any; |
| 203 | + |
| 204 | + await expect(runInference(snapshot, mockTask, inference)).rejects.toThrow(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should run with inference endpoint', async () => { |
| 208 | + inference = new HfInferenceEndpoint('https://endpoint-url.com'); |
| 209 | + |
| 210 | + await expect( |
| 211 | + runInference(snapshot, mockTask, inference), |
| 212 | + ).resolves.not.toThrow(); |
| 213 | + }); |
| 214 | +}); |
0 commit comments