-
Notifications
You must be signed in to change notification settings - Fork 375
[specs] add sse tests #11239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iscai-msft
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
iscai-msft:specs/addSse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[specs] add sse tests #11239
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/http-specs" | ||
| --- | ||
|
|
||
| Add SSE tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/spec-api" | ||
| --- | ||
|
|
||
| Add `streamChunks` support to `MockBody` for chunked SSE streaming in mock responses |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/spector" | ||
| --- | ||
|
|
||
| Support chunked streaming via `streamChunks` in mock response body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import "@typespec/http"; | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
| import "@typespec/spector"; | ||
|
|
||
| using Http; | ||
| using Events; | ||
| using SSE; | ||
| using Spector; | ||
|
|
||
| @doc("Test of server-sent events (SSE) streaming.") | ||
| @scenarioService("/streaming/sse") | ||
| namespace Streaming.Sse; | ||
|
|
||
| @route("unnamed") | ||
| namespace Unnamed { | ||
| model Info { | ||
| desc: string; | ||
| } | ||
|
|
||
| @events | ||
| union UnnamedEvents { | ||
| @Events.contentType("application/json") | ||
| Info, | ||
| } | ||
|
|
||
| @scenario | ||
| @scenarioDoc(""" | ||
| SSE streaming with unnamed events. The server streams a sequence of unnamed | ||
| `message` events, each carrying a JSON `Info` payload, then closes the | ||
| connection. Since the union variant is unnamed, no `event:` field is emitted | ||
| and each event defaults to the `message` type. | ||
|
|
||
| Expected response body (content type `text/event-stream`): | ||
| ``` | ||
| data: {"desc": "one"} | ||
|
|
||
| data: {"desc": "two"} | ||
|
|
||
| data: {"desc": "three"} | ||
|
|
||
| ``` | ||
| """) | ||
| @route("receive") | ||
| op receive(): SSEStream<UnnamedEvents>; | ||
| } | ||
|
|
||
| @route("named") | ||
| namespace Named { | ||
| model ResponseCreated { | ||
| id: string; | ||
| } | ||
|
|
||
| model ResponseDelta { | ||
| delta: string; | ||
| } | ||
|
|
||
| @events | ||
| union ResponseEvents { | ||
| @Events.contentType("application/json") | ||
| responseCreated: ResponseCreated, | ||
|
|
||
| @Events.contentType("application/json") | ||
| responseDelta: ResponseDelta, | ||
|
|
||
| @Events.contentType("text/plain") | ||
| @terminalEvent | ||
| "[DONE]", | ||
| } | ||
|
|
||
| @scenario | ||
| @scenarioDoc(""" | ||
| SSE streaming with multiple named events and a terminal event, modeled after | ||
| an OpenAI-style streaming response. Each named union variant sets the SSE | ||
| `event:` field; the terminal `[DONE]` event signals the client to disconnect. | ||
|
|
||
| Expected response body (content type `text/event-stream`): | ||
| ``` | ||
| event: responseCreated | ||
| data: {"id": "resp_1"} | ||
|
|
||
| event: responseDelta | ||
| data: {"delta": "Hello"} | ||
|
|
||
| event: responseDelta | ||
| data: {"delta": " world"} | ||
|
|
||
| data: [DONE] | ||
|
|
||
| ``` | ||
| """) | ||
| @route("receive") | ||
| op receive(): SSEStream<ResponseEvents>; | ||
| } | ||
|
|
||
| @route("retrieve") | ||
| namespace Retrieve { | ||
| model RetrievalRequest { | ||
| query: string; | ||
| } | ||
|
|
||
| model PartialResult { | ||
| text: string; | ||
| } | ||
|
|
||
| model FinalResult { | ||
| references: string[]; | ||
| } | ||
|
|
||
| @events | ||
| union RetrievalEvents { | ||
| @Events.contentType("application/json") | ||
| partialResult: PartialResult, | ||
|
|
||
| @Events.contentType("application/json") | ||
| finalResult: FinalResult, | ||
|
|
||
| @Events.contentType("text/plain") | ||
| @terminalEvent | ||
| "[DONE]", | ||
| } | ||
|
|
||
| @scenario | ||
| @scenarioDoc(""" | ||
| A POST request with a JSON body whose response is an SSE stream, modeled | ||
| after a knowledge-retrieval service. The server streams `partialResult` | ||
| events as results become available, a final `finalResult` event, and a | ||
| terminal `[DONE]` event. | ||
|
|
||
| Expected request body (content type `application/json`): | ||
| ``` | ||
| {"query": "what is typespec?"} | ||
| ``` | ||
|
|
||
| Expected response body (content type `text/event-stream`): | ||
| ``` | ||
| event: partialResult | ||
| data: {"text": "partial one"} | ||
|
|
||
| event: partialResult | ||
| data: {"text": "partial two"} | ||
|
|
||
| event: finalResult | ||
| data: {"references": ["doc1", "doc2"]} | ||
|
|
||
| data: [DONE] | ||
|
|
||
| ``` | ||
| """) | ||
| @post | ||
| @route("stream") | ||
| op stream(@body request: RetrievalRequest): SSEStream<RetrievalEvents>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; | ||
|
|
||
| export const Scenarios: Record<string, ScenarioMockApi> = {}; | ||
|
|
||
| const unnamedStream = ['data: {"desc": "one"}', 'data: {"desc": "two"}', 'data: {"desc": "three"}'] | ||
| .map((event) => `${event}\n\n`) | ||
| .join(""); | ||
|
|
||
| Scenarios.Streaming_Sse_Unnamed_receive = passOnSuccess({ | ||
| uri: "/streaming/sse/unnamed/receive", | ||
| method: "get", | ||
| request: {}, | ||
| response: { | ||
| status: 200, | ||
| body: { | ||
| rawContent: Buffer.from(unnamedStream), | ||
| contentType: "text/event-stream", | ||
| }, | ||
| }, | ||
| kind: "MockApiDefinition", | ||
| }); | ||
|
|
||
| const namedChunks = [ | ||
| 'event: responseCreated\ndata: {"id": "resp_1"}\n\n', | ||
| 'event: responseDelta\ndata: {"delta": "Hello"}\n\n', | ||
| 'event: responseDelta\ndata: {"delta": " world"}\n\n', | ||
| "data: [DONE]\n\n", | ||
| ].map((event) => Buffer.from(event)); | ||
|
|
||
| Scenarios.Streaming_Sse_Named_receive = passOnSuccess({ | ||
| uri: "/streaming/sse/named/receive", | ||
| method: "get", | ||
| request: {}, | ||
| response: { | ||
| status: 200, | ||
| body: { | ||
| streamChunks: namedChunks, | ||
| contentType: "text/event-stream", | ||
| rawContent: Buffer.from(namedChunks.map((c) => c.toString()).join("")), | ||
| }, | ||
| }, | ||
| kind: "MockApiDefinition", | ||
| }); | ||
|
|
||
| const retrieveStream = [ | ||
| 'event: partialResult\ndata: {"text": "partial one"}', | ||
| 'event: partialResult\ndata: {"text": "partial two"}', | ||
| 'event: finalResult\ndata: {"references": ["doc1", "doc2"]}', | ||
| "data: [DONE]", | ||
| ] | ||
| .map((event) => `${event}\n\n`) | ||
| .join(""); | ||
|
|
||
| Scenarios.Streaming_Sse_Retrieve_stream = passOnSuccess({ | ||
| uri: "/streaming/sse/retrieve/stream", | ||
| method: "post", | ||
| request: { | ||
| body: { | ||
| rawContent: JSON.stringify({ query: "what is typespec?" }), | ||
| contentType: "application/json", | ||
| }, | ||
| }, | ||
| response: { | ||
| status: 200, | ||
| body: { | ||
| rawContent: Buffer.from(retrieveStream), | ||
|
iscai-msft marked this conversation as resolved.
|
||
| contentType: "text/event-stream", | ||
| }, | ||
| }, | ||
| kind: "MockApiDefinition", | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.