view mrjunejune/test/conversation_store_test.c @ 263:ee04e4e69fed

Add functional JRPG frame and tools Add full-screen background_2 apertures, functional frame chrome, card-driven details, dual-window tools, live conversion workflows, and bounded cleanup for generated downloads. Co-authored-by: Copilot <[email protected]>
author MrJuneJune <mrjunejune@users.noreply.github.com>
date Thu, 06 Aug 2026 11:31:30 -0700
parents 1f9877b637e9
children 04fee26ecce0
line wrap: on
line source

#include "mrjunejune/conversation_store.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
  char database_path[] = "/tmp/mrjunejune-conversations-XXXXXX";
  int fd = mkstemp(database_path);
  assert(fd >= 0);
  close(fd);

  Conversation_Store *p_store = Conversation_Store_Create(database_path);
  assert(p_store);

  char conversation_id[37];
  assert(Conversation_Store_Create_Conversation(
      p_store, "First quest", conversation_id) == CONVERSATION_STORE_OK);
  assert(strlen(conversation_id) == 36);

  Dowa_Arena *p_arena = Dowa_Arena_Create(16 * 1024);
  Conversation_Record record;
  assert(Conversation_Store_Get(
      p_store, conversation_id, &record, p_arena) == CONVERSATION_STORE_OK);
  assert(strcmp(record.id, conversation_id) == 0);
  assert(strcmp(record.title, "First quest") == 0);
  assert(Dowa_Array_Length(record.turns) == 0);
  Dowa_Arena_Free(p_arena);

  assert(Conversation_Store_Update_Title(
      p_store, conversation_id, "Renamed quest") == CONVERSATION_STORE_OK);
  assert(Conversation_Store_Begin_Turn(
      p_store,
      conversation_id,
      "request-1",
      "Hello Epi") == CONVERSATION_STORE_OK);
  assert(Conversation_Store_Begin_Turn(
      p_store,
      conversation_id,
      "request-2",
      "Overlapping") == CONVERSATION_STORE_CONFLICT);
  assert(Conversation_Store_Complete_Turn(
      p_store,
      conversation_id,
      "request-1",
      "Hello traveler",
      12,
      4) == CONVERSATION_STORE_OK);

  assert(Conversation_Store_Begin_Turn(
      p_store,
      conversation_id,
      "request-2",
      "Try again") == CONVERSATION_STORE_OK);
  assert(Conversation_Store_Fail_Turn(
      p_store,
      conversation_id,
      "request-2",
      "cancelled",
      TRUE) == CONVERSATION_STORE_OK);

  p_arena = Dowa_Arena_Create(32 * 1024);
  assert(Conversation_Store_Get(
      p_store, conversation_id, &record, p_arena) == CONVERSATION_STORE_OK);
  assert(strcmp(record.title, "Renamed quest") == 0);
  assert(Dowa_Array_Length(record.turns) == 4);
  assert(strcmp(record.turns[0].role, "user") == 0);
  assert(strcmp(record.turns[0].content, "Hello Epi") == 0);
  assert(strcmp(record.turns[1].role, "assistant") == 0);
  assert(strcmp(record.turns[1].content, "Hello traveler") == 0);
  assert(record.turns[1].input_tokens == 12);
  assert(record.turns[1].output_tokens == 4);
  assert(strcmp(record.turns[3].status, "aborted") == 0);
  Dowa_Arena_Free(p_arena);

  assert(Conversation_Store_Begin_Turn(
      p_store,
      conversation_id,
      "request-3",
      "Interrupted") == CONVERSATION_STORE_OK);
  Conversation_Store_Destroy(p_store);
  p_store = Conversation_Store_Create(database_path);
  assert(p_store);
  assert(Conversation_Store_Begin_Turn(
      p_store,
      conversation_id,
      "request-4",
      "Recovered") == CONVERSATION_STORE_OK);
  assert(Conversation_Store_Complete_Turn(
      p_store,
      conversation_id,
      "request-4",
      "Recovered answer",
      1,
      1) == CONVERSATION_STORE_OK);

  p_arena = Dowa_Arena_Create(32 * 1024);
  assert(Conversation_Store_Get(
      p_store, conversation_id, &record, p_arena) == CONVERSATION_STORE_OK);
  assert(Dowa_Array_Length(record.turns) == 8);
  assert(strcmp(record.turns[5].status, "failed") == 0);
  assert(strcmp(
      record.turns[5].error_message,
      "Interrupted by server restart") == 0);
  Dowa_Arena_Free(p_arena);

  assert(Conversation_Store_Delete(
      p_store, conversation_id) == CONVERSATION_STORE_OK);
  p_arena = Dowa_Arena_Create(4096);
  assert(Conversation_Store_Get(
      p_store,
      conversation_id,
      &record,
      p_arena) == CONVERSATION_STORE_NOT_FOUND);
  Dowa_Arena_Free(p_arena);

  Conversation_Store_Destroy(p_store);
  unlink(database_path);
  printf("Conversation store tests passed\n");
  return 0;
}