|
34 | 34 | ]
|
35 | 35 |
|
36 | 36 | structured_output << chunks[0]
|
37 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ message: "Line 1\n" }) |
| 37 | + expect(structured_output.read_buffered_property(:message)).to eq("Line 1\n") |
38 | 38 |
|
39 | 39 | structured_output << chunks[1]
|
40 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ message: "Line 2\n" }) |
| 40 | + expect(structured_output.read_buffered_property(:message)).to eq("Line 2\n") |
41 | 41 |
|
42 | 42 | structured_output << chunks[2]
|
43 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ message: "Line 3" }) |
| 43 | + expect(structured_output.read_buffered_property(:message)).to eq("Line 3") |
44 | 44 |
|
45 | 45 | structured_output << chunks[3]
|
46 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ bool: true }) |
| 46 | + expect(structured_output.read_buffered_property(:bool)).to eq(true) |
47 | 47 |
|
48 | 48 | # Waiting for number to be fully buffered.
|
49 | 49 | structured_output << chunks[4]
|
50 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ bool: true }) |
| 50 | + expect(structured_output.read_buffered_property(:bool)).to eq(true) |
| 51 | + expect(structured_output.read_buffered_property(:number)).to be_nil |
51 | 52 |
|
52 | 53 | structured_output << chunks[5]
|
53 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ bool: true, number: 42 }) |
| 54 | + expect(structured_output.read_buffered_property(:number)).to eq(42) |
54 | 55 |
|
55 | 56 | structured_output << chunks[6]
|
56 |
| - expect(structured_output.read_latest_buffered_chunk).to eq( |
57 |
| - { bool: true, number: 42, status: "o" }, |
58 |
| - ) |
| 57 | + expect(structured_output.read_buffered_property(:number)).to eq(42) |
| 58 | + expect(structured_output.read_buffered_property(:bool)).to eq(true) |
| 59 | + expect(structured_output.read_buffered_property(:status)).to eq("o") |
59 | 60 |
|
60 | 61 | structured_output << chunks[7]
|
61 |
| - expect(structured_output.read_latest_buffered_chunk).to eq( |
62 |
| - { bool: true, number: 42, status: "\"k\"" }, |
63 |
| - ) |
| 62 | + expect(structured_output.read_buffered_property(:status)).to eq("\"k\"") |
64 | 63 |
|
65 | 64 | # No partial string left to read.
|
66 |
| - expect(structured_output.read_latest_buffered_chunk).to eq({ bool: true, number: 42 }) |
| 65 | + expect(structured_output.read_buffered_property(:status)).to eq("") |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "dealing with non-JSON responses" do |
| 70 | + it "treat it as plain text once we determined it's invalid JSON" do |
| 71 | + chunks = [+"I'm not", +"a", +"JSON :)"] |
| 72 | + |
| 73 | + structured_output << chunks[0] |
| 74 | + expect(structured_output.read_buffered_property(nil)).to eq("I'm not") |
| 75 | + |
| 76 | + structured_output << chunks[1] |
| 77 | + expect(structured_output.read_buffered_property(nil)).to eq("a") |
| 78 | + |
| 79 | + structured_output << chunks[2] |
| 80 | + expect(structured_output.read_buffered_property(nil)).to eq("JSON :)") |
67 | 81 | end
|
68 | 82 | end
|
69 | 83 | end
|
0 commit comments