comparison mrjunejune/test/inference_bridge_test.c @ 260:1f9877b637e9

Add Copilot-powered cyberpunk JRPG chat Integrate the production JRPG chat with Seobeo streaming, Deita persistence, and a Bazel-managed Copilot SDK and LiteLLM inference stack. Co-authored-by: Copilot <[email protected]>
author MrJuneJune <mrjunejune@users.noreply.github.com>
date Wed, 05 Aug 2026 09:19:41 -0700
parents
children b401627fc49e
comparison
equal deleted inserted replaced
259:667156fcd3e3 260:1f9877b637e9
1 #include "mrjunejune/inference_bridge.h"
2
3 #include <assert.h>
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <time.h>
8
9 typedef struct {
10 pthread_mutex_t mutex;
11 pthread_cond_t condition;
12 int accepted;
13 int deltas;
14 int completed;
15 int usage;
16 int done;
17 int closed;
18 char delta[128];
19 int64 input_tokens;
20 int64 output_tokens;
21 } Test_State;
22
23 static void Handle_Event(
24 const Inference_Event *p_event,
25 void *p_user_data)
26 {
27 Test_State *p_state = p_user_data;
28 pthread_mutex_lock(&p_state->mutex);
29 if (strcmp(p_event->type, "turn.accepted") == 0)
30 p_state->accepted++;
31 else if (strcmp(p_event->type, "assistant.delta") == 0)
32 {
33 p_state->deltas++;
34 snprintf(p_state->delta, sizeof(p_state->delta), "%s", p_event->delta);
35 }
36 else if (strcmp(p_event->type, "assistant.completed") == 0)
37 p_state->completed++;
38 else if (strcmp(p_event->type, "assistant.usage") == 0)
39 {
40 p_state->usage++;
41 p_state->input_tokens = p_event->input_tokens;
42 p_state->output_tokens = p_event->output_tokens;
43 }
44 else if (strcmp(p_event->type, "turn.done") == 0 &&
45 strncmp(p_event->request_id, "request-", 8) == 0)
46 {
47 p_state->done++;
48 }
49 else if (strcmp(p_event->type, "bridge.closed") == 0)
50 p_state->closed++;
51 pthread_cond_broadcast(&p_state->condition);
52 pthread_mutex_unlock(&p_state->mutex);
53 }
54
55 int main(int argc, char **argv)
56 {
57 assert(argc == 2);
58 Test_State state = {0};
59 assert(pthread_mutex_init(&state.mutex, NULL) == 0);
60 assert(pthread_cond_init(&state.condition, NULL) == 0);
61
62 Inference_Bridge *p_bridge = Inference_Bridge_Create(
63 argv[1], argv[1], Handle_Event, &state);
64 assert(p_bridge);
65 assert(Inference_Bridge_Start(p_bridge));
66 assert(Inference_Bridge_Is_Ready(p_bridge));
67 assert(Inference_Bridge_Start_Turn(
68 p_bridge, "request-1", "conversation-1", "hello"));
69
70 struct timespec deadline;
71 clock_gettime(CLOCK_REALTIME, &deadline);
72 deadline.tv_sec += 5;
73 pthread_mutex_lock(&state.mutex);
74 while (state.done == 0)
75 {
76 int result = pthread_cond_timedwait(
77 &state.condition, &state.mutex, &deadline);
78 assert(result == 0);
79 }
80 pthread_mutex_unlock(&state.mutex);
81
82 assert(state.accepted == 1);
83 assert(state.deltas == 1);
84 assert(state.completed == 1);
85 assert(state.usage == 1);
86 assert(strcmp(state.delta, "hello \"traveler\"\\path") == 0);
87 assert(state.input_tokens == 7);
88 assert(state.output_tokens == 3);
89
90 char large_prompt[32 * 1024 + 1];
91 memset(large_prompt, 0x01, sizeof(large_prompt) - 1);
92 large_prompt[sizeof(large_prompt) - 1] = '\0';
93 assert(Inference_Bridge_Start_Turn(
94 p_bridge,
95 "request-large",
96 "conversation-1",
97 large_prompt));
98 clock_gettime(CLOCK_REALTIME, &deadline);
99 deadline.tv_sec += 5;
100 pthread_mutex_lock(&state.mutex);
101 while (state.done < 2)
102 {
103 int result = pthread_cond_timedwait(
104 &state.condition, &state.mutex, &deadline);
105 assert(result == 0);
106 }
107 pthread_mutex_unlock(&state.mutex);
108
109 Inference_Bridge_Destroy(p_bridge);
110 assert(state.closed == 1);
111 pthread_cond_destroy(&state.condition);
112 pthread_mutex_destroy(&state.mutex);
113 printf("Inference bridge tests passed\n");
114 return 0;
115 }