@@ -14,32 +14,19 @@ namespace {
1414// RAII class to redirect stdin for tests
1515class StdinRedirect {
1616public:
17- StdinRedirect (const std::string &input) {
18- // Create a temporary file with the input
19- std::filesystem::path tempPath =
20- std::filesystem::temp_directory_path () / " stdin.txt" ;
21- std::ofstream tempFile (tempPath);
22- tempFile << input;
23- tempFile.close ();
24-
25- // Redirect stdin
26- old_stdin = freopen (tempPath.c_str (), " r" , stdin);
17+ StdinRedirect (const std::string &input) : old_cin(std::cin.rdbuf()) {
18+ input_buffer << input;
19+ std::cin.rdbuf (input_buffer.rdbuf ());
2720 }
2821
2922 ~StdinRedirect () {
3023 // Restore stdin and clean up
31- if (old_stdin) {
32- freopen (old_stdin_path, " r" , stdin);
33- fclose (old_stdin);
34- }
35- std::filesystem::remove (std::filesystem::temp_directory_path () /
36- " stdin.txt" );
24+ std::cin.rdbuf (old_cin);
3725 }
3826
3927private:
40- FILE *old_stdin = nullptr ;
41- // This is platform-specific, but works for this test case
42- const char *old_stdin_path = " /dev/tty" ;
28+ std::streambuf *old_cin;
29+ std::stringstream input_buffer;
4330};
4431} // namespace
4532
@@ -68,15 +55,17 @@ piper_synthesizer *ProcessTest::synth = nullptr;
6855TEST_F (ProcessTest, ProcessInputStreamText) {
6956 piper::RunConfig runConfig;
7057 runConfig.outputType = piper::OUTPUT_STDOUT ;
71- runConfig.speakerId = 0 ;
58+
59+ piper_synthesize_options options = piper_default_synthesize_options (synth);
60+ options.speaker_id = 0 ;
7261
7362 StdinRedirect redirect (" This is a test." );
7463
7564 // Redirect cout to check output
7665 std::stringstream cout_buffer;
7766 std::streambuf *old_cout = std::cout.rdbuf (cout_buffer.rdbuf ());
7867
79- processInputStream (runConfig, synth, nullptr );
68+ processInputStream (runConfig, synth, &options );
8069
8170 std::cout.rdbuf (old_cout); // Restore cout
8271
@@ -86,18 +75,50 @@ TEST_F(ProcessTest, ProcessInputStreamText) {
8675 ASSERT_EQ (audio_data.substr (0 , 4 ), " RIFF" );
8776}
8877
78+ TEST_F (ProcessTest, ProcessInputStreamFileOutput) {
79+ piper::RunConfig runConfig;
80+ auto outputPath = std::filesystem::temp_directory_path () / " test.wav" ;
81+ runConfig.outputPath = outputPath;
82+ runConfig.outputType = piper::OUTPUT_FILE ;
83+
84+ piper_synthesize_options options = piper_default_synthesize_options (synth);
85+ options.speaker_id = 0 ;
86+
87+ StdinRedirect redirect (" This is a test for file output." );
88+
89+ // Redirect cout to capture the output path
90+ std::stringstream cout_buffer;
91+ std::streambuf* old_cout = std::cout.rdbuf (cout_buffer.rdbuf ());
92+
93+ processInputStream (runConfig, synth, &options);
94+
95+ std::cout.rdbuf (old_cout); // Restore cout
96+
97+ // Verify the output file was created and has content
98+ ASSERT_TRUE (std::filesystem::exists (outputPath));
99+ std::ifstream audio_file (outputPath, std::ios::binary | std::ios::ate);
100+ ASSERT_TRUE (audio_file.is_open ());
101+ std::streamsize size = audio_file.tellg ();
102+ ASSERT_GT (size, 44 ); // Should have a 44-byte header plus some audio data
103+
104+ // Clean up the created file
105+ std::filesystem::remove (outputPath);
106+ }
107+
89108TEST_F (ProcessTest, ProcessInputStreamJson) {
90109 piper::RunConfig runConfig;
91110 runConfig.outputType = piper::OUTPUT_STDOUT ;
92111 runConfig.jsonInput = true ;
93- runConfig.speakerId = 0 ;
112+
113+ piper_synthesize_options options = piper_default_synthesize_options (synth);
114+ options.speaker_id = 0 ;
94115
95116 StdinRedirect redirect (" {\" text\" : \" This is a JSON test.\" }" );
96117
97118 std::stringstream cout_buffer;
98119 std::streambuf *old_cout = std::cout.rdbuf (cout_buffer.rdbuf ());
99120
100- processInputStream (runConfig, synth, nullptr );
121+ processInputStream (runConfig, synth, &options );
101122
102123 std::cout.rdbuf (old_cout);
103124
0 commit comments