|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 2 | import { assert, it } from "@effect/vitest"; |
| 3 | +import { GUEST_MESSAGE_MAX_LENGTH } from "@t3tools/contracts"; |
3 | 4 | import * as Effect from "effect/Effect"; |
4 | 5 | import { HttpServerRequest } from "effect/http"; |
5 | 6 |
|
6 | 7 | import { readSubmission } from "./http.ts"; |
7 | 8 |
|
| 9 | +const submit = (form: FormData) => |
| 10 | + readSubmission.pipe( |
| 11 | + Effect.provideService( |
| 12 | + HttpServerRequest.HttpServerRequest, |
| 13 | + HttpServerRequest.fromWeb( |
| 14 | + new Request("http://localhost/api/guest/messages?share=share-1", { |
| 15 | + method: "POST", |
| 16 | + body: form, |
| 17 | + }), |
| 18 | + ), |
| 19 | + ), |
| 20 | + ); |
| 21 | + |
8 | 22 | it.effect("keeps a JSON file a guest attaches as a file, not a form field", () => |
9 | 23 | Effect.gen(function* () { |
10 | 24 | const form = new FormData(); |
11 | 25 | form.append("shareId", "share-1"); |
12 | 26 | form.append("text", ""); |
13 | 27 | form.append("file", new File(['{"a":1}'], "data.json", { type: "application/json" })); |
14 | | - const request = HttpServerRequest.fromWeb( |
15 | | - new Request("http://localhost/api/guest/messages?share=share-1", { |
16 | | - method: "POST", |
17 | | - body: form, |
18 | | - }), |
19 | | - ); |
20 | | - const submission = yield* readSubmission.pipe( |
21 | | - Effect.provideService(HttpServerRequest.HttpServerRequest, request), |
22 | | - ); |
| 28 | + const submission = yield* submit(form); |
23 | 29 | assert.deepEqual( |
24 | 30 | submission?.uploads.map((upload) => [upload.name, upload.contentType]), |
25 | 31 | [["data.json", "application/json"]], |
26 | 32 | ); |
27 | 33 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
28 | 34 | ); |
| 35 | + |
| 36 | +it.effect("counts a line break in a multipart message as one character", () => |
| 37 | + Effect.gen(function* () { |
| 38 | + const text = "a\n".repeat(GUEST_MESSAGE_MAX_LENGTH / 2 - 1) + "ab"; |
| 39 | + const form = new FormData(); |
| 40 | + form.append("shareId", "share-1"); |
| 41 | + form.append("text", text); |
| 42 | + const submission = yield* submit(form); |
| 43 | + assert.strictEqual(submission?.fields.text, text); |
| 44 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 45 | +); |
0 commit comments