test: rewrite test_cli.c to use AppState and dispatch function
Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
104
test_cli.c
104
test_cli.c
@@ -1,56 +1,89 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include <stdatomic.h>
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "dispatcher.h"
|
||||||
|
|
||||||
|
// Global state for the dispatch function
|
||||||
|
static AppState g_state;
|
||||||
|
|
||||||
|
static void test_dispatch(Action action) {
|
||||||
|
g_state = reducer(g_state, action);
|
||||||
|
}
|
||||||
|
|
||||||
// Minimal test: just ensure parsing doesn't crash
|
|
||||||
static void test_cli_parse(void) {
|
static void test_cli_parse(void) {
|
||||||
printf("Test CLI parse... ");
|
printf("Test CLI parse... ");
|
||||||
|
|
||||||
// Create a minimal engine (no JACK needed for parsing)
|
// Initialize global state
|
||||||
Engine engine;
|
memset(&g_state, 0, sizeof(AppState));
|
||||||
memset(&engine, 0, sizeof(engine));
|
|
||||||
engine.sample_rate = 48000;
|
|
||||||
engine.control_channel = 0;
|
|
||||||
engine.quantize_mode = QUANTIZE_OFF;
|
|
||||||
engine.quantize_threshold = 0;
|
|
||||||
engine.queued_triggers = NULL;
|
|
||||||
|
|
||||||
// Initialize command queue
|
// Initialize clips
|
||||||
command_queue_init(&engine.command_queue);
|
|
||||||
|
|
||||||
// Initialize atomic state mirrors
|
|
||||||
atomic_store(&engine.quantize_mode_atomic, (int)QUANTIZE_OFF);
|
|
||||||
atomic_store(&engine.quantize_threshold_atomic, 0);
|
|
||||||
|
|
||||||
// Allocate and initialize transport
|
|
||||||
engine.transport = (Transport *)calloc(1, sizeof(Transport));
|
|
||||||
assert(engine.transport != NULL);
|
|
||||||
transport_init(engine.transport, 48000);
|
|
||||||
|
|
||||||
engine.transport->state = TRANSPORT_STOPPED;
|
|
||||||
engine.transport->clock_count = 0;
|
|
||||||
engine.transport->beat_position = 0;
|
|
||||||
engine.transport->bar_position = 0;
|
|
||||||
engine.transport->sample_position = 0;
|
|
||||||
for (int i = 0; i < MAX_CLIPS; i++) {
|
for (int i = 0; i < MAX_CLIPS; i++) {
|
||||||
engine.clips[i].state = CLIP_EMPTY;
|
g_state.clips[i].state = CLIP_EMPTY;
|
||||||
engine.clips[i].buffer = NULL; // not needed for parsing
|
g_state.clips[i].buffer = NULL; // not needed for parsing
|
||||||
engine.clips[i].buffer_size = 0;
|
g_state.clips[i].buffer_size = 0;
|
||||||
engine.clips[i].write_position = 0;
|
g_state.clips[i].write_position = 0;
|
||||||
engine.clips[i].read_position = 0;
|
g_state.clips[i].read_position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize transport
|
||||||
|
g_state.transport_state = TRANSPORT_STOPPED;
|
||||||
|
g_state.clock_source = CLOCK_SOURCE_INTERNAL;
|
||||||
|
g_state.bpm = 120.0;
|
||||||
|
g_state.samples_per_beat = (48000 * 60.0) / 120.0;
|
||||||
|
g_state.clock_count = 0;
|
||||||
|
g_state.beat_position = 0;
|
||||||
|
g_state.bar_position = 0;
|
||||||
|
g_state.sample_position = 0;
|
||||||
|
g_state.sample_accumulator = 0.0;
|
||||||
|
|
||||||
|
// Initialize quantize
|
||||||
|
g_state.quantize_mode = QUANTIZE_OFF;
|
||||||
|
g_state.quantize_threshold = 0;
|
||||||
|
|
||||||
|
// Initialize undo
|
||||||
|
g_state.undo.undo_index = 0;
|
||||||
|
g_state.undo.redo_index = 0;
|
||||||
|
g_state.undo.count = 0;
|
||||||
|
for (int i = 0; i < MAX_UNDO_HISTORY; i++) {
|
||||||
|
g_state.undo.prev_clip_indices[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// JACK info
|
||||||
|
g_state.sample_rate = 48000;
|
||||||
|
g_state.running = true;
|
||||||
|
|
||||||
|
// Create engine with dispatch function
|
||||||
|
Engine engine;
|
||||||
|
memset(&engine, 0, sizeof(Engine));
|
||||||
|
engine.dispatch = test_dispatch;
|
||||||
|
engine.sample_rate = 48000;
|
||||||
|
|
||||||
// Test valid commands
|
// Test valid commands
|
||||||
assert(cli_process_line(&engine, "help") == 1);
|
assert(cli_process_line(&engine, "help") == 1);
|
||||||
assert(cli_process_line(&engine, "trigger clip 0") == 1);
|
assert(cli_process_line(&engine, "trigger clip 0") == 1);
|
||||||
|
assert(g_state.clips[0].state == CLIP_RECORDING);
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "trigger scene 1") == 1);
|
assert(cli_process_line(&engine, "trigger scene 1") == 1);
|
||||||
|
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
||||||
|
assert(g_state.clips[CLIP_INDEX(1, ch)].state == CLIP_RECORDING);
|
||||||
|
}
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "reset clip 2") == 1);
|
assert(cli_process_line(&engine, "reset clip 2") == 1);
|
||||||
|
assert(g_state.clips[2].state == CLIP_EMPTY);
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "reset transport") == 1);
|
assert(cli_process_line(&engine, "reset transport") == 1);
|
||||||
|
assert(g_state.transport_state == TRANSPORT_STOPPED);
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "quantize beat") == 1);
|
assert(cli_process_line(&engine, "quantize beat") == 1);
|
||||||
|
assert(g_state.quantize_mode == QUANTIZE_BEAT);
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "threshold 1000") == 1);
|
assert(cli_process_line(&engine, "threshold 1000") == 1);
|
||||||
|
assert(g_state.quantize_threshold == 1000);
|
||||||
|
|
||||||
assert(cli_process_line(&engine, "quit") == 0);
|
assert(cli_process_line(&engine, "quit") == 0);
|
||||||
|
|
||||||
// Test invalid commands (should not crash)
|
// Test invalid commands (should not crash)
|
||||||
@@ -62,10 +95,9 @@ static void test_cli_parse(void) {
|
|||||||
assert(cli_process_line(&engine, "threshold") == 1);
|
assert(cli_process_line(&engine, "threshold") == 1);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
if (engine.transport) {
|
for (int i = 0; i < MAX_CLIPS; i++) {
|
||||||
transport_cleanup(engine.transport);
|
free(g_state.clips[i].buffer);
|
||||||
free(engine.transport);
|
g_state.clips[i].buffer = NULL;
|
||||||
engine.transport = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("PASSED\n");
|
printf("PASSED\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user