feat: add client tests, status FIFO, and evaluation docs
This commit is contained in:
committed by
Loic Coenen (aider)
parent
998406616a
commit
791744beeb
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