|
1011 | 1011 | expect($array)->toHaveKey('timestamp'); |
1012 | 1012 | } |
1013 | 1013 | }); |
| 1014 | + |
| 1015 | + it('step finish event has default zero usage when fake response has no explicit usage', function (): void { |
| 1016 | + Prism::fake([ |
| 1017 | + TextResponseFake::make()->withText('Test'), |
| 1018 | + ]); |
| 1019 | + |
| 1020 | + $events = iterator_to_array( |
| 1021 | + Prism::text() |
| 1022 | + ->using('openai', 'gpt-4') |
| 1023 | + ->withPrompt('Test') |
| 1024 | + ->asStream() |
| 1025 | + ); |
| 1026 | + |
| 1027 | + $stepFinishEvents = array_values(array_filter( |
| 1028 | + $events, |
| 1029 | + fn (StreamEvent $e): bool => $e instanceof StepFinishEvent |
| 1030 | + )); |
| 1031 | + |
| 1032 | + expect($stepFinishEvents)->not->toBeEmpty(); |
| 1033 | + expect($stepFinishEvents[0]->usage)->not->toBeNull(); |
| 1034 | + expect($stepFinishEvents[0]->usage->promptTokens)->toBe(0); |
| 1035 | + expect($stepFinishEvents[0]->usage->completionTokens)->toBe(0); |
| 1036 | + }); |
| 1037 | + |
| 1038 | + it('step finish event contains usage when fake response has usage', function (): void { |
| 1039 | + Prism::fake([ |
| 1040 | + TextResponseFake::make() |
| 1041 | + ->withText('Test') |
| 1042 | + ->withUsage(new Usage(100, 50)), |
| 1043 | + ]); |
| 1044 | + |
| 1045 | + $events = iterator_to_array( |
| 1046 | + Prism::text() |
| 1047 | + ->using('openai', 'gpt-4') |
| 1048 | + ->withPrompt('Test') |
| 1049 | + ->asStream() |
| 1050 | + ); |
| 1051 | + |
| 1052 | + $stepFinishEvents = array_values(array_filter( |
| 1053 | + $events, |
| 1054 | + fn (StreamEvent $e): bool => $e instanceof StepFinishEvent |
| 1055 | + )); |
| 1056 | + |
| 1057 | + expect($stepFinishEvents)->not->toBeEmpty(); |
| 1058 | + expect($stepFinishEvents[0]->usage)->not->toBeNull(); |
| 1059 | + expect($stepFinishEvents[0]->usage->promptTokens)->toBe(100); |
| 1060 | + expect($stepFinishEvents[0]->usage->completionTokens)->toBe(50); |
| 1061 | + }); |
| 1062 | + |
| 1063 | + it('step finish event toArray includes usage when set', function (): void { |
| 1064 | + Prism::fake([ |
| 1065 | + TextResponseFake::make() |
| 1066 | + ->withText('Test') |
| 1067 | + ->withUsage(new Usage(20, 10)), |
| 1068 | + ]); |
| 1069 | + |
| 1070 | + $events = iterator_to_array( |
| 1071 | + Prism::text() |
| 1072 | + ->using('openai', 'gpt-4') |
| 1073 | + ->withPrompt('Test') |
| 1074 | + ->asStream() |
| 1075 | + ); |
| 1076 | + |
| 1077 | + $stepFinishEvents = array_values(array_filter( |
| 1078 | + $events, |
| 1079 | + fn (StreamEvent $e): bool => $e instanceof StepFinishEvent |
| 1080 | + )); |
| 1081 | + |
| 1082 | + expect($stepFinishEvents)->not->toBeEmpty(); |
| 1083 | + $array = $stepFinishEvents[0]->toArray(); |
| 1084 | + expect($array)->toHaveKey('usage'); |
| 1085 | + expect($array['usage'])->not->toBeNull(); |
| 1086 | + expect($array['usage']['prompt_tokens'])->toBe(20); |
| 1087 | + expect($array['usage']['completion_tokens'])->toBe(10); |
| 1088 | + }); |
| 1089 | + |
| 1090 | + it('step finish event toArray has null usage when event is constructed without usage', function (): void { |
| 1091 | + $event = new StepFinishEvent(id: 'test-id', timestamp: 1234567890); |
| 1092 | + $array = $event->toArray(); |
| 1093 | + expect($array)->toHaveKey('usage'); |
| 1094 | + expect($array['usage'])->toBeNull(); |
| 1095 | + }); |
1014 | 1096 | }); |
0 commit comments