feat: add client tests, status FIFO, and evaluation docs
This commit is contained in:
committed by
Loic Coenen (aider)
parent
998406616a
commit
791744beeb
BIN
client/looper-client
Executable file
BIN
client/looper-client
Executable file
Binary file not shown.
BIN
client/looper-client-test
Executable file
BIN
client/looper-client-test
Executable file
Binary file not shown.
@@ -1,10 +1,15 @@
|
||||
CC ?= gcc
|
||||
CFLAGS ?= -Wall -Wextra -g -Isrc
|
||||
LDFLAGS ?= -lncurses -lm
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -std=c11
|
||||
|
||||
looper-client: src/tui.c main.c
|
||||
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
|
||||
all: test_status_parse
|
||||
|
||||
test_status_parse: tests/test_status_parse.c
|
||||
$(CC) $(CFLAGS) -I../src -o test_status_parse tests/test_status_parse.c ../src/tui.c -lncurses
|
||||
|
||||
test: test_status_parse
|
||||
./test_status_parse
|
||||
|
||||
.PHONY: all test clean
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f looper-client
|
||||
rm -f test_status_parse
|
||||
|
||||
@@ -11,8 +11,10 @@
|
||||
#include <math.h>
|
||||
|
||||
/* ---------- FIFO command helper ---------- */
|
||||
static int send_command(const char *cmd) {
|
||||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||||
int send_command(const char *cmd) {
|
||||
const char *fifo_path = getenv("LOOPER_CMD_FIFO");
|
||||
if (!fifo_path) fifo_path = "/tmp/looper_cmd";
|
||||
int fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
|
||||
if (fd < 0) return -1;
|
||||
size_t len = strlen(cmd);
|
||||
int n = write(fd, cmd, len);
|
||||
|
||||
@@ -4,5 +4,6 @@
|
||||
void tui_init(void);
|
||||
void tui_run(void);
|
||||
void tui_cleanup(void);
|
||||
int send_command(const char *cmd);
|
||||
|
||||
#endif
|
||||
|
||||
67
client/tests/test_client.c
Normal file
67
client/tests/test_client.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "tui.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define TEST_PASS 0
|
||||
#define TEST_FAIL 1
|
||||
|
||||
static int run_single_test(const char *test_name, const char *cmd_sent, const char *expected) {
|
||||
/* build temporary file path */
|
||||
char tmpl[] = "/tmp/looper_test_XXXXXX";
|
||||
int fd = mkstemp(tmpl);
|
||||
if (fd == -1) { perror("mkstemp"); return TEST_FAIL; }
|
||||
close(fd);
|
||||
/* create regular file to mimic a FIFO */
|
||||
fd = open(tmpl, O_CREAT|O_WRONLY|O_TRUNC, 0644);
|
||||
if (fd < 0) { perror("open create"); unlink(tmpl); return TEST_FAIL; }
|
||||
close(fd);
|
||||
|
||||
/* make send_command use this file */
|
||||
setenv("LOOPER_CMD_FIFO", tmpl, 1);
|
||||
|
||||
int ret = send_command(cmd_sent);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "FAIL %s: send_command returned %d\n", test_name, ret);
|
||||
unlink(tmpl);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
/* read back the written content */
|
||||
FILE *fp = fopen(tmpl, "r");
|
||||
if (!fp) { perror("fopen"); unlink(tmpl); return TEST_FAIL; }
|
||||
char buf[4096];
|
||||
size_t nread = fread(buf, 1, sizeof(buf)-1, fp);
|
||||
fclose(fp);
|
||||
buf[nread] = '\0';
|
||||
|
||||
/* build expected string (send_command always appends a newline) */
|
||||
char expected_line[512];
|
||||
snprintf(expected_line, sizeof(expected_line), "%s\n", expected);
|
||||
|
||||
if (strcmp(buf, expected_line) == 0) {
|
||||
printf("PASS %s\n", test_name);
|
||||
unlink(tmpl);
|
||||
return TEST_PASS;
|
||||
} else {
|
||||
printf("FAIL %s: expected '%s', got '%s'\n", test_name, expected_line, buf);
|
||||
unlink(tmpl);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int fail = 0;
|
||||
fail += run_single_test("record_0", "record 0", "record 0");
|
||||
fail += run_single_test("record_1", "record 1", "record 1");
|
||||
fail += run_single_test("stop", "stop", "stop");
|
||||
fail += run_single_test("scene_next", "scene_next", "scene_next");
|
||||
fail += run_single_test("scene_prev", "scene_prev", "scene_prev");
|
||||
fail += run_single_test("bind_2", "bind 2", "bind 2");
|
||||
fail += run_single_test("with_newline", "record 0\n", "record 0");
|
||||
printf("%d tests failed.\n", fail);
|
||||
return fail > 0 ? 1 : 0;
|
||||
}
|
||||
88
client/tests/test_status_parse.c
Normal file
88
client/tests/test_status_parse.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum { STATE_IDLE, STATE_RECORD, STATE_LOOPING, STATE_PAUSED } ChannelState;
|
||||
|
||||
bool parse_status_line(const char *line, int *ch, int *scene, ChannelState *state);
|
||||
|
||||
static int test_parse_idle(void) {
|
||||
printf("Test parse_status_line: IDLE\n");
|
||||
int ch, sc; ChannelState st;
|
||||
if (!parse_status_line("CH=0 SC=0 STATE=IDLE\n", &ch, &sc, &st)) {
|
||||
fprintf(stderr, " FAIL: parse returned false\n");
|
||||
return 1;
|
||||
}
|
||||
if (ch != 0 || sc != 0 || st != STATE_IDLE) {
|
||||
fprintf(stderr, " FAIL: expected (0,0,IDLE), got (%d,%d,%d)\n", ch, sc, st);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_parse_recording(void) {
|
||||
printf("Test parse_status_line: RECORD\n");
|
||||
int ch, sc; ChannelState st;
|
||||
if (!parse_status_line("CH=0 SC=0 STATE=RECORD\n", &ch, &sc, &st)) {
|
||||
fprintf(stderr, " FAIL: parse returned false\n");
|
||||
return 1;
|
||||
}
|
||||
if (ch != 0 || sc != 0 || st != STATE_RECORD) {
|
||||
fprintf(stderr, " FAIL: expected (0,0,RECORD), got (%d,%d,%d)\n", ch, sc, st);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_parse_looping(void) {
|
||||
printf("Test parse_status_line: LOOPING\n");
|
||||
int ch, sc; ChannelState st;
|
||||
if (!parse_status_line("CH=0 SC=0 STATE=LOOPING\n", &ch, &sc, &st)) {
|
||||
fprintf(stderr, " FAIL: parse returned false\n");
|
||||
return 1;
|
||||
}
|
||||
if (ch != 0 || sc != 0 || st != STATE_LOOPING) {
|
||||
fprintf(stderr, " FAIL: expected (0,0,LOOPING), got (%d,%d,%d)\n", ch, sc, st);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_parse_paused(void) {
|
||||
printf("Test parse_status_line: PAUSED\n");
|
||||
int ch, sc; ChannelState st;
|
||||
if (!parse_status_line("CH=0 SC=0 STATE=PAUSED\n", &ch, &sc, &st)) {
|
||||
fprintf(stderr, " FAIL: parse returned false\n");
|
||||
return 1;
|
||||
}
|
||||
if (ch != 0 || sc != 0 || st != STATE_PAUSED) {
|
||||
fprintf(stderr, " FAIL: expected (0,0,PAUSED), got (%d,%d,%d)\n", ch, sc, st);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_parse_malformed(void) {
|
||||
printf("Test parse_status_line: malformed\n");
|
||||
int ch, sc; ChannelState st;
|
||||
if (parse_status_line("garbage\n", &ch, &sc, &st)) {
|
||||
fprintf(stderr, " FAIL: parse should return false for garbage\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int fail = 0;
|
||||
fail += test_parse_idle();
|
||||
fail += test_parse_recording();
|
||||
fail += test_parse_looping();
|
||||
fail += test_parse_paused();
|
||||
fail += test_parse_malformed();
|
||||
return fail;
|
||||
}
|
||||
Reference in New Issue
Block a user