# Unit tests for the Posterior Handler

## Overview

Two different test cases are provided in the `test_posterior.cpp` to test the basic functionality of the posterior handler:

1. History length: 35, Threshold: 100, Suppression: 0 ms
2. History length: 35, Threshold: 100, Suppression: 100 ms

Further the following configuration is used: `category_count = 4` (silence, unknown, yes, no)

test procedure:
- The test does interact directly with the posterior handler itsef, mocking the model outputs and system time of the MCU
- Therfore the test should be completely decoupled from the rest of the MicroKWS application and environment.
- After filling up the first complete window with raw silence samples (255, 0, 0, 0) 25 unknown samples (0, 255, 0, 0) will be generated. Finally a mixed-class model output (16, 32, 144, 64) is simulated.
- As the behavior during the first 25 interation of the posterior handler depends on the initialization values of the moving averate buffer, which have not been specified explicitly, the beginning of the test is less strict than later.
- The `trigger_count` variable verifies that the number of triggers generated by the handler does match the expected value. For the first test case (Suppression: 0 ms) continuous triggers (here: 35 after a complete window) are expected while for test case 2 (Suppression: 100 ms) the number of triggers depends on the threshold.
- In addtition to the triggers, the detected classes are also verified for the described raw data sequence.

## Setup

### On-device testing

To run these unit test a target binary need to be compiled and flashed to a real MCU device. The result of the test can be inspected via the ESP-IDF serial monitor (`idf.py monitor`).

*How to run the MicroKWS tests on the ESP32-C3 MCU?*

1. Go to the `test` directory of the actual MicroKWS project (e.g. `3_run/prj_tuned/test/` **not** `3_run/student/posterior/test`).
2. Setup an ESP-IDF project as usual: `idf.py set-target esp32c3`
3. Compile the test app: `idf.py build`
4. Flash the app to the device: `idf.py flash`
5. Use the IDF monitor to check the test outputs: `idf.py monitor`

### Testing on Linux host

Currently NOT supported.

## Troubleshooting

If you have the feeling that the test cases do not match the description of the algorithm in the lab manual and the C docstrings/comments, please reach out to the lab team ASAP. We tried to add tolerances to some checks to give some degree of freedom for your implementations.
