LCOV - code coverage report
Current view: top level - tests/unit - test_media_index.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 54 54
Test Date: 2026-04-20 19:54:22 Functions: 100.0 % 8 8

            Line data    Source code
       1              : /**
       2              :  * @file test_media_index.c
       3              :  * @brief Unit tests for media_index_put / media_index_get (FEAT-07).
       4              :  */
       5              : 
       6              : #include "test_helpers.h"
       7              : #include "infrastructure/media_index.h"
       8              : 
       9              : #include <stdlib.h>
      10              : #include <string.h>
      11              : #include <unistd.h>
      12              : 
      13              : /* ---- helpers ---- */
      14              : 
      15            6 : static void set_test_home(const char *home) {
      16            6 :     setenv("HOME", home, 1);
      17            6 : }
      18              : 
      19              : /* ---- tests ---- */
      20              : 
      21              : /** Basic round-trip: put then get returns the path. */
      22            1 : static void test_media_index_basic(void) {
      23            1 :     set_test_home("/tmp/tg-cli-midx-test1");
      24              : 
      25            1 :     int rc = media_index_put(12345678LL, "/tmp/photo-12345678.jpg");
      26            1 :     ASSERT(rc == 0, "media_index_put: must succeed");
      27              : 
      28            1 :     char path[512] = {0};
      29            1 :     int found = media_index_get(12345678LL, path, sizeof(path));
      30            1 :     ASSERT(found == 1, "media_index_get: must find the entry");
      31            1 :     ASSERT(strcmp(path, "/tmp/photo-12345678.jpg") == 0,
      32              :            "media_index_get: path must match");
      33              : }
      34              : 
      35              : /** Looking up an id that was never stored returns 0. */
      36            1 : static void test_media_index_miss(void) {
      37            1 :     set_test_home("/tmp/tg-cli-midx-test2");
      38              : 
      39            1 :     char path[512] = {0};
      40            1 :     int found = media_index_get(999999999LL, path, sizeof(path));
      41            1 :     ASSERT(found == 0, "media_index_get: miss returns 0");
      42            1 :     ASSERT(path[0] == '\0', "media_index_get: output buffer empty on miss");
      43              : }
      44              : 
      45              : /** Overwriting an entry updates the stored path. */
      46            1 : static void test_media_index_overwrite(void) {
      47            1 :     set_test_home("/tmp/tg-cli-midx-test3");
      48              : 
      49            1 :     media_index_put(42LL, "/old/path.jpg");
      50              : 
      51            1 :     int rc = media_index_put(42LL, "/new/path.jpg");
      52            1 :     ASSERT(rc == 0, "overwrite must succeed");
      53              : 
      54            1 :     char path[512] = {0};
      55            1 :     int found = media_index_get(42LL, path, sizeof(path));
      56            1 :     ASSERT(found == 1, "entry found after overwrite");
      57            1 :     ASSERT(strcmp(path, "/new/path.jpg") == 0, "overwritten path returned");
      58              : }
      59              : 
      60              : /** Multiple entries coexist and are individually addressable. */
      61            1 : static void test_media_index_multiple(void) {
      62            1 :     set_test_home("/tmp/tg-cli-midx-test4");
      63              : 
      64            1 :     media_index_put(1LL, "/cache/photo-1.jpg");
      65            1 :     media_index_put(2LL, "/cache/photo-2.jpg");
      66            1 :     media_index_put(3LL, "/cache/doc-3.pdf");
      67              : 
      68            1 :     char p1[512]={0}, p2[512]={0}, p3[512]={0};
      69            1 :     ASSERT(media_index_get(1LL, p1, sizeof(p1)) == 1, "id=1 found");
      70            1 :     ASSERT(media_index_get(2LL, p2, sizeof(p2)) == 1, "id=2 found");
      71            1 :     ASSERT(media_index_get(3LL, p3, sizeof(p3)) == 1, "id=3 found");
      72            1 :     ASSERT(strcmp(p1, "/cache/photo-1.jpg") == 0, "path 1 correct");
      73            1 :     ASSERT(strcmp(p2, "/cache/photo-2.jpg") == 0, "path 2 correct");
      74            1 :     ASSERT(strcmp(p3, "/cache/doc-3.pdf")   == 0, "path 3 correct");
      75              : 
      76            1 :     char pm[512] = {0};
      77            1 :     ASSERT(media_index_get(99LL, pm, sizeof(pm)) == 0, "non-existent id=99 miss");
      78              : }
      79              : 
      80              : /** Null path to media_index_put is rejected. */
      81            1 : static void test_media_index_null_path(void) {
      82            1 :     set_test_home("/tmp/tg-cli-midx-test5");
      83            1 :     ASSERT(media_index_put(1LL, NULL) == -1, "null path rejected");
      84              : }
      85              : 
      86              : /** media_index_get with null output buffer returns -1. */
      87            1 : static void test_media_index_null_out(void) {
      88            1 :     set_test_home("/tmp/tg-cli-midx-test6");
      89            1 :     ASSERT(media_index_get(1LL, NULL, 0) == -1, "null out_path rejected");
      90              : }
      91              : 
      92            1 : void run_media_index_tests(void) {
      93            1 :     RUN_TEST(test_media_index_basic);
      94            1 :     RUN_TEST(test_media_index_miss);
      95            1 :     RUN_TEST(test_media_index_overwrite);
      96            1 :     RUN_TEST(test_media_index_multiple);
      97            1 :     RUN_TEST(test_media_index_null_path);
      98            1 :     RUN_TEST(test_media_index_null_out);
      99            1 : }
        

Generated by: LCOV version 2.0-1