|
1 | 1 | const {initFishData} = require('@ml/utils/fishData'); |
2 | 2 | import {setState, getState, resetState} from '@ml/oceans/state'; |
3 | | -import {ClassType, Modes} from '@ml/oceans/constants'; |
| 3 | +import {ClassType, Modes, AppMode} from '@ml/oceans/constants'; |
4 | 4 | import {init, predictFish} from '@ml/oceans/models/predict'; |
5 | 5 | import SimpleTrainer from '@ml/utils/SimpleTrainer'; |
| 6 | +import {TrashOceanObject} from '@ml/oceans/OceanObject'; |
6 | 7 |
|
7 | | -describe('Model quality test', () => { |
| 8 | +describe('Predict test', () => { |
8 | 9 | beforeAll(() => { |
9 | 10 | initFishData(); |
| 11 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; |
10 | 12 | }); |
11 | 13 |
|
12 | 14 | beforeEach(() => { |
| 15 | + const trainer = new SimpleTrainer(); |
| 16 | + trainer.predict = jest.fn(async example => { |
| 17 | + if (example instanceof TrashOceanObject) { |
| 18 | + return {predictedClassId: 1, confidenceByClassId: {0: 0, 1: 1}}; |
| 19 | + } else { |
| 20 | + return {predictedClassId: 0, confidenceByClassId: {0: 1, 1: 0}}; |
| 21 | + } |
| 22 | + }); |
| 23 | + |
13 | 24 | resetState(); |
14 | 25 | setState({ |
15 | | - trainer: new SimpleTrainer() |
| 26 | + trainer |
16 | 27 | }); |
17 | 28 | }); |
18 | 29 |
|
19 | | - jest.useFakeTimers(); |
20 | | - |
21 | 30 | test('init state without intermediate loading', () => { |
22 | | - init(); |
23 | | - // There's a setTimeout in the predict init. Ideally this would be |
24 | | - // refactored to use promises if possible but until then, wait 100 ms. |
25 | | - setTimeout(async () => { |
| 31 | + return init().then(() => { |
26 | 32 | const state = getState(); |
27 | 33 | expect(state).toBeTruthy(); |
28 | | - expect(state.mode).toBe(Modes.Predicting); |
| 34 | + expect(state.currentMode).toBe(Modes.Predicting); |
29 | 35 | expect(state.fishData).toBeTruthy(); |
30 | 36 | expect(state.fishData.length).toBeGreaterThan(0); |
31 | | - }, 100); |
| 37 | + }); |
32 | 38 | }); |
33 | 39 |
|
34 | | - test('fish gets prediction on predict', async () => { |
| 40 | + test('init state to intermediate loading', async () => { |
| 41 | + setState({appMode: AppMode.FishShort}); |
35 | 42 | init(); |
36 | | - // There's a setTimeout in the predict init. Ideally this would be |
37 | | - // refactored to use promises if possible but until then, wait 100 ms. |
38 | | - setTimeout(async () => { |
39 | | - const state = getState(); |
40 | | - await predictFish(state, 0); |
41 | | - expect(state.trainer.predict).toBeCalled(); |
42 | | - expect(state.fishData[0].result).toBeInstanceOf(Number); |
43 | | - }, 100); |
| 43 | + |
| 44 | + const state = getState(); |
| 45 | + expect(state).toBeTruthy(); |
| 46 | + expect(state.currentMode).toBe(Modes.IntermediateLoading); |
| 47 | + }); |
| 48 | + |
| 49 | + it('fish gets prediction on predict', async () => { |
| 50 | + setState({appMode: AppMode.FishVTrash}); |
| 51 | + let state = getState(); |
| 52 | + return init().then(() => { |
| 53 | + state = getState(); |
| 54 | + return predictFish(state, 0).then(() => { |
| 55 | + expect( |
| 56 | + state.fishData[0].result.predictedClassId |
| 57 | + ).toBeGreaterThanOrEqual(0); |
| 58 | + }); |
| 59 | + }); |
44 | 60 | }); |
45 | 61 | }); |
0 commit comments