Login
1 branch 0 tags
Ben (T14/NixOS) Minor Makefile cleanup 7080bfb 28 days ago 85 Commits
moon / tests / test_podcast_rss.c
#include <string.h>

#include "podcast_xml.h"
#include "test_harness.h"
#include "test_helpers.h"

static bool test_rss_extracts_audio_episodes(void) {
	ASSERT_TRUE(test_prepare_storage());
	ASSERT_TRUE(
	    test_copy_fixture_to_storage("rss/basic.rss", ".tests/rss/basic.rss"));

	podcast_episode_t episodes[8];
	memset(episodes, 0, sizeof(episodes));

	int count = podcast_parse_rss("/.tests/rss/basic.rss", episodes, 8);
	ASSERT_EQ_INT(2, count);

	ASSERT_EQ_STR("Episode One", episodes[0].title);
	ASSERT_EQ_STR("guid-ep-1", episodes[0].guid);
	ASSERT_EQ_STR("https://cdn.example.com/ep1.mp3", episodes[0].enclosure_url);
	ASSERT_EQ_STR("Mon, 01 Jan 2024 10:00:00 GMT", episodes[0].pub_date);

	ASSERT_EQ_STR("Episode Two", episodes[1].title);
	ASSERT_EQ_STR("Episode Two", episodes[1].guid);
	ASSERT_EQ_STR("https://cdn.example.com/ep2.ogg", episodes[1].enclosure_url);
	ASSERT_EQ_STR("Tue, 02 Jan 2024 10:00:00 GMT", episodes[1].pub_date);
	return true;
}

static bool test_rss_respects_max_episodes(void) {
	ASSERT_TRUE(test_prepare_storage());
	ASSERT_TRUE(
	    test_copy_fixture_to_storage("rss/basic.rss", ".tests/rss/basic.rss"));

	podcast_episode_t episodes[1];
	memset(episodes, 0, sizeof(episodes));

	int count = podcast_parse_rss("/.tests/rss/basic.rss", episodes, 1);
	ASSERT_EQ_INT(1, count);
	ASSERT_EQ_STR("Episode One", episodes[0].title);
	return true;
}

static bool test_rss_mixed_enclosures_only_audio_kept(void) {
	ASSERT_TRUE(test_prepare_storage());
	ASSERT_TRUE(test_copy_fixture_to_storage("rss/mixed_enclosures.rss",
	                                        ".tests/rss/mixed_enclosures.rss"));

	podcast_episode_t episodes[4];
	memset(episodes, 0, sizeof(episodes));

	int count = podcast_parse_rss("/.tests/rss/mixed_enclosures.rss", episodes, 4);
	ASSERT_EQ_INT(2, count);
	ASSERT_EQ_STR("Audio Episode", episodes[0].title);
	ASSERT_EQ_STR("Another Audio", episodes[1].title);
	return true;
}

static bool test_rss_missing_file_returns_minus_one(void) {
	ASSERT_TRUE(test_prepare_storage());

	podcast_episode_t episodes[2];
	memset(episodes, 0, sizeof(episodes));
	ASSERT_EQ_INT(-1,
	              podcast_parse_rss("/.tests/rss/missing.rss", episodes, 2));
	return true;
}

static bool test_rss_malformed_returns_zero(void) {
	ASSERT_TRUE(test_prepare_storage());
	ASSERT_TRUE(test_copy_fixture_to_storage("rss/malformed.rss",
	                                        ".tests/rss/malformed.rss"));

	podcast_episode_t episodes[2];
	memset(episodes, 0, sizeof(episodes));
	ASSERT_EQ_INT(0, podcast_parse_rss("/.tests/rss/malformed.rss", episodes, 2));
	return true;
}

void register_podcast_rss_tests(void) {
	test_register("rss_extracts_audio_episodes", test_rss_extracts_audio_episodes);
	test_register("rss_respects_max_episodes", test_rss_respects_max_episodes);
	test_register("rss_mixed_enclosures_only_audio_kept",
	              test_rss_mixed_enclosures_only_audio_kept);
	test_register("rss_missing_file_returns_minus_one",
	              test_rss_missing_file_returns_minus_one);
	test_register("rss_malformed_returns_zero", test_rss_malformed_returns_zero);
}