(input: string | string[], outputOrExpectedResult: string | ExpectedResult)
| 155 | }); |
| 156 | |
| 157 | function runTest(input: string | string[], outputOrExpectedResult: string | ExpectedResult) { |
| 158 | let expected: ExpectedResult; |
| 159 | |
| 160 | if (typeof outputOrExpectedResult === 'string') { |
| 161 | expected = { output: outputOrExpectedResult }; |
| 162 | } else { |
| 163 | expected = outputOrExpectedResult; |
| 164 | } |
| 165 | |
| 166 | const callbacks = { |
| 167 | onArtifactOpen: vi.fn<ArtifactCallback>((data) => { |
| 168 | expect(data).toMatchSnapshot('onArtifactOpen'); |
| 169 | }), |
| 170 | onArtifactClose: vi.fn<ArtifactCallback>((data) => { |
| 171 | expect(data).toMatchSnapshot('onArtifactClose'); |
| 172 | }), |
| 173 | onActionOpen: vi.fn<ActionCallback>((data) => { |
| 174 | expect(data).toMatchSnapshot('onActionOpen'); |
| 175 | }), |
| 176 | onActionClose: vi.fn<ActionCallback>((data) => { |
| 177 | expect(data).toMatchSnapshot('onActionClose'); |
| 178 | }), |
| 179 | }; |
| 180 | |
| 181 | const parser = new StreamingMessageParser({ |
| 182 | artifactElement: () => '', |
| 183 | callbacks, |
| 184 | }); |
| 185 | |
| 186 | let message = ''; |
| 187 | |
| 188 | let result = ''; |
| 189 | |
| 190 | const chunks = Array.isArray(input) ? input : input.split(''); |
| 191 | |
| 192 | for (const chunk of chunks) { |
| 193 | message += chunk; |
| 194 | |
| 195 | result += parser.parse('message_1', message); |
| 196 | } |
| 197 | |
| 198 | for (const name in expected.callbacks) { |
| 199 | const callbackName = name; |
| 200 | |
| 201 | expect(callbacks[callbackName as keyof typeof callbacks]).toHaveBeenCalledTimes( |
| 202 | expected.callbacks[callbackName as keyof typeof expected.callbacks] ?? 0, |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | expect(result).toEqual(expected.output); |
| 207 | } |
no test coverage detected