@durable-streams/server-conformance-tests 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,7 +59,7 @@ async function fetchSSE(url, opts = {}) {
59
59
  */
60
60
  function parseSSEEvents(sseText) {
61
61
  const events = [];
62
- const normalized = sseText.replace(/\r\n/g, `\n`);
62
+ const normalized = sseText.replace(/\r\n/g, `\n`).replace(/\r/g, `\n`);
63
63
  const eventBlocks = normalized.split(`\n\n`).filter((block) => block.trim());
64
64
  for (const block of eventBlocks) {
65
65
  const lines = block.split(`\n`);
@@ -301,7 +301,7 @@ function runConformanceTests(options) {
301
301
  method: `PUT`,
302
302
  headers: { "Content-Type": `text/plain` }
303
303
  });
304
- expect([200, 204]).toContain(secondResponse.status);
304
+ expect(secondResponse.status).toBe(200);
305
305
  });
306
306
  test(`should return 409 on PUT with different config`, async () => {
307
307
  const streamPath = `/v1/stream/config-conflict-test-${Date.now()}`;
@@ -326,7 +326,7 @@ function runConformanceTests(options) {
326
326
  headers: { "Content-Type": `text/plain` },
327
327
  body: `hello world`
328
328
  });
329
- expect([200, 204]).toContain(response.status);
329
+ expect(response.status).toBe(204);
330
330
  expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeDefined();
331
331
  });
332
332
  test(`should return 404 on POST to non-existent stream`, async () => {
@@ -509,7 +509,7 @@ function runConformanceTests(options) {
509
509
  },
510
510
  body: `second`
511
511
  });
512
- expect([200, 204]).toContain(response.status);
512
+ expect(response.status).toBe(204);
513
513
  });
514
514
  test(`should reject duplicate seq values`, async () => {
515
515
  const streamPath = `/v1/stream/seq-duplicate-test-${Date.now()}`;
@@ -536,6 +536,140 @@ function runConformanceTests(options) {
536
536
  expect(response.status).toBe(409);
537
537
  });
538
538
  });
539
+ describe(`Browser Security Headers`, () => {
540
+ test(`should include X-Content-Type-Options: nosniff on GET responses`, async () => {
541
+ const streamPath = `/v1/stream/security-get-nosniff-${Date.now()}`;
542
+ await fetch(`${getBaseUrl()}${streamPath}`, {
543
+ method: `PUT`,
544
+ headers: { "Content-Type": `text/plain` },
545
+ body: `test data`
546
+ });
547
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, { method: `GET` });
548
+ expect(response.status).toBe(200);
549
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
550
+ });
551
+ test(`should include X-Content-Type-Options: nosniff on PUT responses`, async () => {
552
+ const streamPath = `/v1/stream/security-put-nosniff-${Date.now()}`;
553
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, {
554
+ method: `PUT`,
555
+ headers: { "Content-Type": `text/plain` }
556
+ });
557
+ expect(response.status).toBe(201);
558
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
559
+ });
560
+ test(`should include X-Content-Type-Options: nosniff on POST responses`, async () => {
561
+ const streamPath = `/v1/stream/security-post-nosniff-${Date.now()}`;
562
+ await fetch(`${getBaseUrl()}${streamPath}`, {
563
+ method: `PUT`,
564
+ headers: { "Content-Type": `text/plain` }
565
+ });
566
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, {
567
+ method: `POST`,
568
+ headers: { "Content-Type": `text/plain` },
569
+ body: `data`
570
+ });
571
+ expect([200, 204]).toContain(response.status);
572
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
573
+ });
574
+ test(`should include X-Content-Type-Options: nosniff on HEAD responses`, async () => {
575
+ const streamPath = `/v1/stream/security-head-nosniff-${Date.now()}`;
576
+ await fetch(`${getBaseUrl()}${streamPath}`, {
577
+ method: `PUT`,
578
+ headers: { "Content-Type": `text/plain` }
579
+ });
580
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, { method: `HEAD` });
581
+ expect(response.status).toBe(200);
582
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
583
+ });
584
+ test(`should include Cross-Origin-Resource-Policy header on GET responses`, async () => {
585
+ const streamPath = `/v1/stream/security-corp-get-${Date.now()}`;
586
+ await fetch(`${getBaseUrl()}${streamPath}`, {
587
+ method: `PUT`,
588
+ headers: { "Content-Type": `application/octet-stream` },
589
+ body: new Uint8Array([
590
+ 1,
591
+ 2,
592
+ 3,
593
+ 4
594
+ ])
595
+ });
596
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, { method: `GET` });
597
+ expect(response.status).toBe(200);
598
+ const corp = response.headers.get(`cross-origin-resource-policy`);
599
+ expect(corp).toBeDefined();
600
+ expect([
601
+ `cross-origin`,
602
+ `same-origin`,
603
+ `same-site`
604
+ ]).toContain(corp);
605
+ });
606
+ test(`should include Cache-Control: no-store on HEAD responses`, async () => {
607
+ const streamPath = `/v1/stream/security-head-cache-${Date.now()}`;
608
+ await fetch(`${getBaseUrl()}${streamPath}`, {
609
+ method: `PUT`,
610
+ headers: { "Content-Type": `text/plain` }
611
+ });
612
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, { method: `HEAD` });
613
+ expect(response.status).toBe(200);
614
+ const cacheControl = response.headers.get(`cache-control`);
615
+ expect(cacheControl).toBeDefined();
616
+ expect(cacheControl).toContain(`no-store`);
617
+ });
618
+ test(`should include X-Content-Type-Options: nosniff on SSE responses`, async () => {
619
+ const streamPath = `/v1/stream/security-sse-nosniff-${Date.now()}`;
620
+ await fetch(`${getBaseUrl()}${streamPath}`, {
621
+ method: `PUT`,
622
+ headers: { "Content-Type": `application/json` },
623
+ body: JSON.stringify({ test: `data` })
624
+ });
625
+ const headResponse = await fetch(`${getBaseUrl()}${streamPath}`, { method: `HEAD` });
626
+ const offset = headResponse.headers.get(STREAM_OFFSET_HEADER) ?? `-1`;
627
+ const controller = new AbortController();
628
+ const timeoutId = setTimeout(() => controller.abort(), 500);
629
+ try {
630
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=${offset}&live=sse`, {
631
+ method: `GET`,
632
+ signal: controller.signal
633
+ });
634
+ expect(response.status).toBe(200);
635
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
636
+ } catch (e) {
637
+ if (!(e instanceof Error && e.name === `AbortError`)) throw e;
638
+ } finally {
639
+ clearTimeout(timeoutId);
640
+ }
641
+ });
642
+ test(`should include X-Content-Type-Options: nosniff on long-poll responses`, async () => {
643
+ const streamPath = `/v1/stream/security-longpoll-nosniff-${Date.now()}`;
644
+ await fetch(`${getBaseUrl()}${streamPath}`, {
645
+ method: `PUT`,
646
+ headers: { "Content-Type": `text/plain` },
647
+ body: `initial data`
648
+ });
649
+ const headResponse = await fetch(`${getBaseUrl()}${streamPath}`, { method: `HEAD` });
650
+ const offset = headResponse.headers.get(STREAM_OFFSET_HEADER) ?? `-1`;
651
+ const controller = new AbortController();
652
+ const timeoutId = setTimeout(() => controller.abort(), 500);
653
+ try {
654
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=${offset}&live=long-poll`, {
655
+ method: `GET`,
656
+ signal: controller.signal
657
+ });
658
+ expect([200, 204]).toContain(response.status);
659
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
660
+ } catch (e) {
661
+ if (!(e instanceof Error && e.name === `AbortError`)) throw e;
662
+ } finally {
663
+ clearTimeout(timeoutId);
664
+ }
665
+ });
666
+ test(`should include security headers on error responses`, async () => {
667
+ const streamPath = `/v1/stream/security-error-headers-${Date.now()}`;
668
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, { method: `GET` });
669
+ expect(response.status).toBe(404);
670
+ expect(response.headers.get(`x-content-type-options`)).toBe(`nosniff`);
671
+ });
672
+ });
539
673
  describe(`TTL and Expiry Validation`, () => {
540
674
  test(`should reject both TTL and Expires-At (400)`, async () => {
541
675
  const streamPath = `/v1/stream/ttl-expires-conflict-test-${Date.now()}`;
@@ -606,7 +740,7 @@ function runConformanceTests(options) {
606
740
  headers: { "Content-Type": `TEXT/PLAIN` },
607
741
  body: `test`
608
742
  });
609
- expect([200, 204]).toContain(response.status);
743
+ expect(response.status).toBe(204);
610
744
  });
611
745
  test(`should allow idempotent create with different case content-type`, async () => {
612
746
  const streamPath = `/v1/stream/case-idempotent-test-${Date.now()}`;
@@ -619,7 +753,7 @@ function runConformanceTests(options) {
619
753
  method: `PUT`,
620
754
  headers: { "Content-Type": `APPLICATION/JSON` }
621
755
  });
622
- expect([200, 204]).toContain(response2.status);
756
+ expect(response2.status).toBe(200);
623
757
  });
624
758
  test(`should accept headers with different casing`, async () => {
625
759
  const streamPath = `/v1/stream/case-header-test-${Date.now()}`;
@@ -635,7 +769,7 @@ function runConformanceTests(options) {
635
769
  },
636
770
  body: `test`
637
771
  });
638
- expect([200, 204]).toContain(response.status);
772
+ expect(response.status).toBe(204);
639
773
  });
640
774
  });
641
775
  describe(`Content-Type Validation`, () => {
@@ -663,7 +797,7 @@ function runConformanceTests(options) {
663
797
  headers: { "Content-Type": `application/json` },
664
798
  body: `{"test": true}`
665
799
  });
666
- expect([200, 204]).toContain(response.status);
800
+ expect(response.status).toBe(204);
667
801
  });
668
802
  test(`should return stream content-type on GET`, async () => {
669
803
  const streamPath = `/v1/stream/content-type-get-test-${Date.now()}`;
@@ -745,6 +879,199 @@ function runConformanceTests(options) {
745
879
  expect(text1).toBe(text2);
746
880
  expect(text1).toBe(`hello world`);
747
881
  });
882
+ test(`should accept offset=now as sentinel for current tail position`, async () => {
883
+ const streamPath = `/v1/stream/offset-now-sentinel-test-${Date.now()}`;
884
+ await fetch(`${getBaseUrl()}${streamPath}`, {
885
+ method: `PUT`,
886
+ headers: { "Content-Type": `text/plain` },
887
+ body: `historical data`
888
+ });
889
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
890
+ expect(response.status).toBe(200);
891
+ const text = await response.text();
892
+ expect(text).toBe(``);
893
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
894
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeDefined();
895
+ });
896
+ test(`should return correct tail offset for offset=now`, async () => {
897
+ const streamPath = `/v1/stream/offset-now-tail-test-${Date.now()}`;
898
+ await fetch(`${getBaseUrl()}${streamPath}`, {
899
+ method: `PUT`,
900
+ headers: { "Content-Type": `text/plain` },
901
+ body: `initial data`
902
+ });
903
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`, { method: `GET` });
904
+ const tailOffset = readResponse.headers.get(STREAM_OFFSET_HEADER);
905
+ expect(tailOffset).toBeDefined();
906
+ const nowResponse = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
907
+ expect(nowResponse.headers.get(STREAM_OFFSET_HEADER)).toBe(tailOffset);
908
+ });
909
+ test(`should be able to resume from offset=now result`, async () => {
910
+ const streamPath = `/v1/stream/offset-now-resume-test-${Date.now()}`;
911
+ await fetch(`${getBaseUrl()}${streamPath}`, {
912
+ method: `PUT`,
913
+ headers: { "Content-Type": `text/plain` },
914
+ body: `old data`
915
+ });
916
+ const nowResponse = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
917
+ const nowOffset = nowResponse.headers.get(STREAM_OFFSET_HEADER);
918
+ expect(nowOffset).toBeDefined();
919
+ await fetch(`${getBaseUrl()}${streamPath}`, {
920
+ method: `POST`,
921
+ headers: { "Content-Type": `text/plain` },
922
+ body: `new data`
923
+ });
924
+ const resumeResponse = await fetch(`${getBaseUrl()}${streamPath}?offset=${nowOffset}`, { method: `GET` });
925
+ const resumeText = await resumeResponse.text();
926
+ expect(resumeText).toBe(`new data`);
927
+ });
928
+ test(`should work with offset=now on empty stream`, async () => {
929
+ const streamPath = `/v1/stream/offset-now-empty-test-${Date.now()}`;
930
+ await fetch(`${getBaseUrl()}${streamPath}`, {
931
+ method: `PUT`,
932
+ headers: { "Content-Type": `text/plain` }
933
+ });
934
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
935
+ expect(response.status).toBe(200);
936
+ const text = await response.text();
937
+ expect(text).toBe(``);
938
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
939
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeDefined();
940
+ });
941
+ test(`should return empty JSON array for offset=now on JSON streams`, async () => {
942
+ const streamPath = `/v1/stream/offset-now-json-body-test-${Date.now()}`;
943
+ await fetch(`${getBaseUrl()}${streamPath}`, {
944
+ method: `PUT`,
945
+ headers: { "Content-Type": `application/json` },
946
+ body: `[{"event": "historical"}]`
947
+ });
948
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
949
+ expect(response.status).toBe(200);
950
+ expect(response.headers.get(`content-type`)).toBe(`application/json`);
951
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
952
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeDefined();
953
+ const body = await response.text();
954
+ expect(body).toBe(`[]`);
955
+ });
956
+ test(`should return empty body for offset=now on non-JSON streams`, async () => {
957
+ const streamPath = `/v1/stream/offset-now-text-body-test-${Date.now()}`;
958
+ await fetch(`${getBaseUrl()}${streamPath}`, {
959
+ method: `PUT`,
960
+ headers: { "Content-Type": `text/plain` },
961
+ body: `historical data`
962
+ });
963
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
964
+ expect(response.status).toBe(200);
965
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
966
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeDefined();
967
+ const body = await response.text();
968
+ expect(body).toBe(``);
969
+ });
970
+ test(`should support offset=now with long-poll mode (waits for data)`, async () => {
971
+ const streamPath = `/v1/stream/offset-now-longpoll-test-${Date.now()}`;
972
+ await fetch(`${getBaseUrl()}${streamPath}`, {
973
+ method: `PUT`,
974
+ headers: { "Content-Type": `text/plain` },
975
+ body: `existing data`
976
+ });
977
+ const readRes = await fetch(`${getBaseUrl()}${streamPath}`);
978
+ const tailOffset = readRes.headers.get(STREAM_OFFSET_HEADER);
979
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now&live=long-poll`, { method: `GET` });
980
+ expect(response.status).toBe(204);
981
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
982
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBe(tailOffset);
983
+ });
984
+ test(`should receive data with offset=now long-poll when appended`, async () => {
985
+ const streamPath = `/v1/stream/offset-now-longpoll-data-test-${Date.now()}`;
986
+ await fetch(`${getBaseUrl()}${streamPath}`, {
987
+ method: `PUT`,
988
+ headers: { "Content-Type": `text/plain` },
989
+ body: `historical`
990
+ });
991
+ const longPollPromise = fetch(`${getBaseUrl()}${streamPath}?offset=now&live=long-poll`, { method: `GET` });
992
+ await new Promise((r) => setTimeout(r, 100));
993
+ await fetch(`${getBaseUrl()}${streamPath}`, {
994
+ method: `POST`,
995
+ headers: { "Content-Type": `text/plain` },
996
+ body: `new data`
997
+ });
998
+ const response = await longPollPromise;
999
+ expect(response.status).toBe(200);
1000
+ const text = await response.text();
1001
+ expect(text).toBe(`new data`);
1002
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
1003
+ });
1004
+ test(`should support offset=now with SSE mode`, async () => {
1005
+ const streamPath = `/v1/stream/offset-now-sse-test-${Date.now()}`;
1006
+ await fetch(`${getBaseUrl()}${streamPath}`, {
1007
+ method: `PUT`,
1008
+ headers: { "Content-Type": `text/plain` },
1009
+ body: `existing data`
1010
+ });
1011
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`, { method: `GET` });
1012
+ const tailOffset = readResponse.headers.get(STREAM_OFFSET_HEADER);
1013
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=now&live=sse`, { untilContent: `"upToDate"` });
1014
+ expect(response.status).toBe(200);
1015
+ const controlMatch = received.match(/event: control\s*\n\s*data: ({[^}]+})/);
1016
+ expect(controlMatch).toBeDefined();
1017
+ if (controlMatch && controlMatch[1]) {
1018
+ const controlData = JSON.parse(controlMatch[1]);
1019
+ expect(controlData[`upToDate`]).toBe(true);
1020
+ expect(controlData[`streamNextOffset`]).toBe(tailOffset);
1021
+ }
1022
+ });
1023
+ test(`should return 404 for offset=now on non-existent stream`, async () => {
1024
+ const streamPath = `/v1/stream/offset-now-404-test-${Date.now()}`;
1025
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now`, { method: `GET` });
1026
+ expect(response.status).toBe(404);
1027
+ });
1028
+ test(`should return 404 for offset=now with long-poll on non-existent stream`, async () => {
1029
+ const streamPath = `/v1/stream/offset-now-longpoll-404-test-${Date.now()}`;
1030
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now&live=long-poll`, { method: `GET` });
1031
+ expect(response.status).toBe(404);
1032
+ });
1033
+ test(`should return 404 for offset=now with SSE on non-existent stream`, async () => {
1034
+ const streamPath = `/v1/stream/offset-now-sse-404-test-${Date.now()}`;
1035
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now&live=sse`, { method: `GET` });
1036
+ expect(response.status).toBe(404);
1037
+ });
1038
+ test(`should support offset=now with long-poll on empty stream`, async () => {
1039
+ const streamPath = `/v1/stream/offset-now-empty-longpoll-test-${Date.now()}`;
1040
+ await fetch(`${getBaseUrl()}${streamPath}`, {
1041
+ method: `PUT`,
1042
+ headers: { "Content-Type": `text/plain` }
1043
+ });
1044
+ const response = await fetch(`${getBaseUrl()}${streamPath}?offset=now&live=long-poll`, { method: `GET` });
1045
+ expect(response.status).toBe(204);
1046
+ expect(response.headers.get(STREAM_UP_TO_DATE_HEADER)).toBe(`true`);
1047
+ const offset = response.headers.get(STREAM_OFFSET_HEADER);
1048
+ expect(offset).toBeDefined();
1049
+ await fetch(`${getBaseUrl()}${streamPath}`, {
1050
+ method: `POST`,
1051
+ headers: { "Content-Type": `text/plain` },
1052
+ body: `first data`
1053
+ });
1054
+ const resumeResponse = await fetch(`${getBaseUrl()}${streamPath}?offset=${offset}`, { method: `GET` });
1055
+ expect(resumeResponse.status).toBe(200);
1056
+ const resumeText = await resumeResponse.text();
1057
+ expect(resumeText).toBe(`first data`);
1058
+ });
1059
+ test(`should support offset=now with SSE on empty stream`, async () => {
1060
+ const streamPath = `/v1/stream/offset-now-empty-sse-test-${Date.now()}`;
1061
+ await fetch(`${getBaseUrl()}${streamPath}`, {
1062
+ method: `PUT`,
1063
+ headers: { "Content-Type": `text/plain` }
1064
+ });
1065
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=now&live=sse`, { untilContent: `"upToDate"` });
1066
+ expect(response.status).toBe(200);
1067
+ const controlMatch = received.match(/event: control\s*\n\s*data: ({[^}]+})/);
1068
+ expect(controlMatch).toBeDefined();
1069
+ if (controlMatch && controlMatch[1]) {
1070
+ const controlData = JSON.parse(controlMatch[1]);
1071
+ expect(controlData[`upToDate`]).toBe(true);
1072
+ expect(controlData[`streamNextOffset`]).toBeDefined();
1073
+ }
1074
+ });
748
1075
  test(`should reject malformed offset (contains comma)`, async () => {
749
1076
  const streamPath = `/v1/stream/offset-comma-test-${Date.now()}`;
750
1077
  await fetch(`${getBaseUrl()}${streamPath}`, {
@@ -955,7 +1282,8 @@ function runConformanceTests(options) {
955
1282
  expect(response.status).toBe(201);
956
1283
  const location = response.headers.get(`location`);
957
1284
  expect(location).toBeDefined();
958
- expect(location).toBe(`${getBaseUrl()}${streamPath}`);
1285
+ expect(location.endsWith(streamPath)).toBe(true);
1286
+ expect(() => new URL(location)).not.toThrow();
959
1287
  });
960
1288
  test(`should reject missing Content-Type on POST`, async () => {
961
1289
  const streamPath = `/v1/stream/missing-ct-post-test-${Date.now()}`;
@@ -1159,7 +1487,7 @@ function runConformanceTests(options) {
1159
1487
  "Stream-TTL": `3600`
1160
1488
  }
1161
1489
  });
1162
- expect([200, 204]).toContain(response2.status);
1490
+ expect(response2.status).toBe(200);
1163
1491
  });
1164
1492
  test(`should reject idempotent PUT with different TTL`, async () => {
1165
1493
  const streamPath = `/v1/stream/ttl-conflict-test-${Date.now()}`;
@@ -1263,7 +1591,7 @@ function runConformanceTests(options) {
1263
1591
  headers: { "Content-Type": `text/plain` },
1264
1592
  body: `appended data`
1265
1593
  });
1266
- expect([200, 204]).toContain(postBefore.status);
1594
+ expect(postBefore.status).toBe(204);
1267
1595
  await sleep(1500);
1268
1596
  const postAfter = await fetch(`${getBaseUrl()}${streamPath}`, {
1269
1597
  method: `POST`,
@@ -1323,7 +1651,7 @@ function runConformanceTests(options) {
1323
1651
  headers: { "Content-Type": `text/plain` },
1324
1652
  body: `appended data`
1325
1653
  });
1326
- expect([200, 204]).toContain(postBefore.status);
1654
+ expect(postBefore.status).toBe(204);
1327
1655
  await sleep(1500);
1328
1656
  const postAfter = await fetch(`${getBaseUrl()}${streamPath}`, {
1329
1657
  method: `POST`,
@@ -1746,6 +2074,88 @@ function runConformanceTests(options) {
1746
2074
  expect(received).toContain(`data: line2`);
1747
2075
  expect(received).toContain(`data: line3`);
1748
2076
  });
2077
+ test(`should prevent CRLF injection in payloads - embedded event boundaries become literal data`, async () => {
2078
+ const streamPath = `/v1/stream/sse-crlf-injection-test-${Date.now()}`;
2079
+ const maliciousPayload = `safe content\r\n\r\nevent: control\r\ndata: {"injected":true}\r\n\r\nmore safe content`;
2080
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2081
+ method: `PUT`,
2082
+ headers: { "Content-Type": `text/plain` },
2083
+ body: maliciousPayload
2084
+ });
2085
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=-1&live=sse`, { untilContent: `event: control` });
2086
+ expect(response.status).toBe(200);
2087
+ const events = parseSSEEvents(received);
2088
+ const dataEvents = events.filter((e) => e.type === `data`);
2089
+ const controlEvents = events.filter((e) => e.type === `control`);
2090
+ expect(dataEvents.length).toBe(1);
2091
+ expect(controlEvents.length).toBe(1);
2092
+ const dataContent = dataEvents[0].data;
2093
+ expect(dataContent).toContain(`event: control`);
2094
+ expect(dataContent).toContain(`data: {"injected":true}`);
2095
+ const controlContent = JSON.parse(controlEvents[0].data);
2096
+ expect(controlContent.injected).toBeUndefined();
2097
+ expect(controlContent.streamNextOffset).toBeDefined();
2098
+ });
2099
+ test(`should prevent CRLF injection - LF-only attack vectors`, async () => {
2100
+ const streamPath = `/v1/stream/sse-lf-injection-test-${Date.now()}`;
2101
+ const maliciousPayload = `start\n\nevent: data\ndata: fake-event\n\nend`;
2102
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2103
+ method: `PUT`,
2104
+ headers: { "Content-Type": `text/plain` },
2105
+ body: maliciousPayload
2106
+ });
2107
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=-1&live=sse`, { untilContent: `event: control` });
2108
+ expect(response.status).toBe(200);
2109
+ const events = parseSSEEvents(received);
2110
+ const dataEvents = events.filter((e) => e.type === `data`);
2111
+ expect(dataEvents.length).toBe(1);
2112
+ const dataContent = dataEvents[0].data;
2113
+ expect(dataContent).toContain(`event: data`);
2114
+ expect(dataContent).toContain(`data: fake-event`);
2115
+ });
2116
+ test(`should prevent CRLF injection - carriage return only attack vectors`, async () => {
2117
+ const streamPath = `/v1/stream/sse-cr-injection-test-${Date.now()}`;
2118
+ const maliciousPayload = `start\r\revent: control\rdata: {"cr_injected":true}\r\rend`;
2119
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2120
+ method: `PUT`,
2121
+ headers: { "Content-Type": `text/plain` },
2122
+ body: maliciousPayload
2123
+ });
2124
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=-1&live=sse`, { untilContent: `event: control` });
2125
+ expect(response.status).toBe(200);
2126
+ const events = parseSSEEvents(received);
2127
+ const controlEvents = events.filter((e) => e.type === `control`);
2128
+ expect(controlEvents.length).toBe(1);
2129
+ const controlContent = JSON.parse(controlEvents[0].data);
2130
+ expect(controlContent.cr_injected).toBeUndefined();
2131
+ expect(controlContent.streamNextOffset).toBeDefined();
2132
+ });
2133
+ test(`should handle JSON payloads with embedded newlines safely`, async () => {
2134
+ const streamPath = `/v1/stream/sse-json-newline-test-${Date.now()}`;
2135
+ const jsonPayload = JSON.stringify({
2136
+ message: `line1\nline2\nline3`,
2137
+ attack: `try\r\n\r\nevent: control\r\ndata: {"bad":true}`
2138
+ });
2139
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2140
+ method: `PUT`,
2141
+ headers: { "Content-Type": `application/json` },
2142
+ body: jsonPayload
2143
+ });
2144
+ const { response, received } = await fetchSSE(`${getBaseUrl()}${streamPath}?offset=-1&live=sse`, { untilContent: `event: control` });
2145
+ expect(response.status).toBe(200);
2146
+ const events = parseSSEEvents(received);
2147
+ const dataEvents = events.filter((e) => e.type === `data`);
2148
+ const controlEvents = events.filter((e) => e.type === `control`);
2149
+ expect(dataEvents.length).toBe(1);
2150
+ expect(controlEvents.length).toBe(1);
2151
+ const parsedData = JSON.parse(dataEvents[0].data);
2152
+ expect(Array.isArray(parsedData)).toBe(true);
2153
+ expect(parsedData[0].message).toBe(`line1\nline2\nline3`);
2154
+ expect(parsedData[0].attack).toContain(`event: control`);
2155
+ const controlContent = JSON.parse(controlEvents[0].data);
2156
+ expect(controlContent.bad).toBeUndefined();
2157
+ expect(controlContent.streamNextOffset).toBeDefined();
2158
+ });
1749
2159
  test(`should generate unique, monotonically increasing offsets in SSE mode`, async () => {
1750
2160
  const streamPath = `/v1/stream/sse-monotonic-offset-test-${Date.now()}`;
1751
2161
  await fetch(`${getBaseUrl()}${streamPath}`, {
@@ -2131,7 +2541,7 @@ function runConformanceTests(options) {
2131
2541
  headers: { "Content-Type": `application/octet-stream` },
2132
2542
  body: chunk
2133
2543
  });
2134
- expect([200, 204]).toContain(response.status);
2544
+ expect(response.status).toBe(204);
2135
2545
  }
2136
2546
  const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
2137
2547
  const expected = new Uint8Array(totalLength);
@@ -2234,7 +2644,7 @@ function runConformanceTests(options) {
2234
2644
  headers: { "Content-Type": `application/octet-stream` },
2235
2645
  body: op.data
2236
2646
  });
2237
- expect([200, 204]).toContain(response.status);
2647
+ expect(response.status).toBe(204);
2238
2648
  appendedData.push(...Array.from(op.data));
2239
2649
  const offset = response.headers.get(STREAM_OFFSET_HEADER);
2240
2650
  if (offset) savedOffsets.push(offset);
@@ -2301,7 +2711,7 @@ function runConformanceTests(options) {
2301
2711
  return true;
2302
2712
  }
2303
2713
  ), { numRuns: 25 });
2304
- });
2714
+ }, 15e3);
2305
2715
  test(`read-your-writes: data is immediately visible after append`, async () => {
2306
2716
  await fc.assert(fc.asyncProperty(fc.uint8Array({
2307
2717
  minLength: 1,
@@ -2317,7 +2727,7 @@ function runConformanceTests(options) {
2317
2727
  headers: { "Content-Type": `application/octet-stream` },
2318
2728
  body: data
2319
2729
  });
2320
- expect([200, 204]).toContain(appendResponse.status);
2730
+ expect(appendResponse.status).toBe(204);
2321
2731
  const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
2322
2732
  expect(readResponse.status).toBe(200);
2323
2733
  const buffer = await readResponse.arrayBuffer();
@@ -2429,7 +2839,7 @@ function runConformanceTests(options) {
2429
2839
  },
2430
2840
  body: `data-${seq}`
2431
2841
  });
2432
- expect([200, 204]).toContain(response.status);
2842
+ expect(response.status).toBe(204);
2433
2843
  }
2434
2844
  return true;
2435
2845
  }
@@ -2453,7 +2863,7 @@ function runConformanceTests(options) {
2453
2863
  },
2454
2864
  body: `first`
2455
2865
  });
2456
- expect([200, 204]).toContain(response1.status);
2866
+ expect(response1.status).toBe(204);
2457
2867
  const response2 = await fetch(`${getBaseUrl()}${streamPath}`, {
2458
2868
  method: `POST`,
2459
2869
  headers: {
@@ -2468,6 +2878,1171 @@ function runConformanceTests(options) {
2468
2878
  ), { numRuns: 25 });
2469
2879
  });
2470
2880
  });
2881
+ describe(`Concurrent Writer Stress Tests`, () => {
2882
+ test(`concurrent writers with sequence numbers - server handles gracefully`, async () => {
2883
+ const streamPath = `/v1/stream/concurrent-seq-${Date.now()}-${Math.random().toString(36).slice(2)}`;
2884
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2885
+ method: `PUT`,
2886
+ headers: { "Content-Type": `text/plain` }
2887
+ });
2888
+ const numWriters = 5;
2889
+ const seqValue = `seq-001`;
2890
+ const writePromises = Array.from({ length: numWriters }, (_, i) => fetch(`${getBaseUrl()}${streamPath}`, {
2891
+ method: `POST`,
2892
+ headers: {
2893
+ "Content-Type": `text/plain`,
2894
+ [STREAM_SEQ_HEADER]: seqValue
2895
+ },
2896
+ body: `writer-${i}`
2897
+ }));
2898
+ const responses = await Promise.all(writePromises);
2899
+ const statuses = responses.map((r) => r.status);
2900
+ for (const status of statuses) expect([
2901
+ 200,
2902
+ 204,
2903
+ 409
2904
+ ]).toContain(status);
2905
+ const successes = statuses.filter((s) => s === 200 || s === 204);
2906
+ expect(successes.length).toBeGreaterThanOrEqual(1);
2907
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
2908
+ const content = await readResponse.text();
2909
+ const matchingWriters = Array.from({ length: numWriters }, (_, i) => content.includes(`writer-${i}`)).filter(Boolean);
2910
+ expect(matchingWriters.length).toBeGreaterThanOrEqual(1);
2911
+ });
2912
+ test(`concurrent writers racing with incrementing seq values`, async () => {
2913
+ await fc.assert(fc.asyncProperty(fc.integer({
2914
+ min: 3,
2915
+ max: 8
2916
+ }), async (numWriters) => {
2917
+ const streamPath = `/v1/stream/concurrent-race-${Date.now()}-${Math.random().toString(36).slice(2)}`;
2918
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2919
+ method: `PUT`,
2920
+ headers: { "Content-Type": `text/plain` }
2921
+ });
2922
+ const writePromises = Array.from({ length: numWriters }, (_, i) => fetch(`${getBaseUrl()}${streamPath}`, {
2923
+ method: `POST`,
2924
+ headers: {
2925
+ "Content-Type": `text/plain`,
2926
+ [STREAM_SEQ_HEADER]: String(i).padStart(4, `0`)
2927
+ },
2928
+ body: `data-${i}`
2929
+ }));
2930
+ const responses = await Promise.all(writePromises);
2931
+ const successIndices = [];
2932
+ for (let i = 0; i < responses.length; i++) {
2933
+ expect([
2934
+ 200,
2935
+ 204,
2936
+ 409
2937
+ ]).toContain(responses[i].status);
2938
+ if (responses[i].status === 200 || responses[i].status === 204) successIndices.push(i);
2939
+ }
2940
+ expect(successIndices.length).toBeGreaterThanOrEqual(1);
2941
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
2942
+ const content = await readResponse.text();
2943
+ for (const i of successIndices) expect(content).toContain(`data-${i}`);
2944
+ return true;
2945
+ }), { numRuns: 10 });
2946
+ });
2947
+ test(`concurrent appends without seq - all data is persisted`, async () => {
2948
+ const streamPath = `/v1/stream/concurrent-no-seq-${Date.now()}-${Math.random().toString(36).slice(2)}`;
2949
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2950
+ method: `PUT`,
2951
+ headers: { "Content-Type": `text/plain` }
2952
+ });
2953
+ const numWriters = 10;
2954
+ const writePromises = Array.from({ length: numWriters }, (_, i) => fetch(`${getBaseUrl()}${streamPath}`, {
2955
+ method: `POST`,
2956
+ headers: { "Content-Type": `text/plain` },
2957
+ body: `concurrent-${i}`
2958
+ }));
2959
+ const responses = await Promise.all(writePromises);
2960
+ for (const response of responses) expect([200, 204]).toContain(response.status);
2961
+ const offsets = responses.map((r) => r.headers.get(STREAM_OFFSET_HEADER));
2962
+ for (const offset of offsets) expect(offset).not.toBeNull();
2963
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
2964
+ const content = await readResponse.text();
2965
+ for (let i = 0; i < numWriters; i++) expect(content).toContain(`concurrent-${i}`);
2966
+ });
2967
+ test(`mixed readers and writers - readers see consistent state`, async () => {
2968
+ const streamPath = `/v1/stream/concurrent-rw-${Date.now()}-${Math.random().toString(36).slice(2)}`;
2969
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2970
+ method: `PUT`,
2971
+ headers: { "Content-Type": `text/plain` }
2972
+ });
2973
+ await fetch(`${getBaseUrl()}${streamPath}`, {
2974
+ method: `POST`,
2975
+ headers: { "Content-Type": `text/plain` },
2976
+ body: `initial`
2977
+ });
2978
+ const numOps = 20;
2979
+ const operations = Array.from({ length: numOps }, (_, i) => {
2980
+ if (i % 2 === 0) return fetch(`${getBaseUrl()}${streamPath}`, {
2981
+ method: `POST`,
2982
+ headers: { "Content-Type": `text/plain` },
2983
+ body: `write-${i}`
2984
+ });
2985
+ else return fetch(`${getBaseUrl()}${streamPath}`);
2986
+ });
2987
+ const responses = await Promise.all(operations);
2988
+ responses.forEach((response, i) => {
2989
+ if (i % 2 === 0) expect([200, 204]).toContain(response.status);
2990
+ else expect(response.status).toBe(200);
2991
+ });
2992
+ const finalRead = await fetch(`${getBaseUrl()}${streamPath}`);
2993
+ const content = await finalRead.text();
2994
+ expect(content).toContain(`initial`);
2995
+ for (let i = 0; i < numOps; i += 2) expect(content).toContain(`write-${i}`);
2996
+ });
2997
+ });
2998
+ describe(`State Hash Verification`, () => {
2999
+ /**
3000
+ * Simple hash function for content verification.
3001
+ * Uses FNV-1a algorithm for deterministic hashing.
3002
+ */
3003
+ function hashContent(data) {
3004
+ let hash = 2166136261;
3005
+ for (const byte of data) {
3006
+ hash ^= byte;
3007
+ hash = Math.imul(hash, 16777619);
3008
+ hash = hash >>> 0;
3009
+ }
3010
+ return hash.toString(16).padStart(8, `0`);
3011
+ }
3012
+ test(`replay produces identical content hash`, async () => {
3013
+ await fc.assert(fc.asyncProperty(
3014
+ // Generate a sequence of appends
3015
+ fc.array(fc.uint8Array({
3016
+ minLength: 1,
3017
+ maxLength: 100
3018
+ }), {
3019
+ minLength: 1,
3020
+ maxLength: 10
3021
+ }),
3022
+ async (chunks) => {
3023
+ const streamPath1 = `/v1/stream/hash-verify-1-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3024
+ await fetch(`${getBaseUrl()}${streamPath1}`, {
3025
+ method: `PUT`,
3026
+ headers: { "Content-Type": `application/octet-stream` }
3027
+ });
3028
+ for (const chunk of chunks) await fetch(`${getBaseUrl()}${streamPath1}`, {
3029
+ method: `POST`,
3030
+ headers: { "Content-Type": `application/octet-stream` },
3031
+ body: chunk
3032
+ });
3033
+ const response1 = await fetch(`${getBaseUrl()}${streamPath1}`);
3034
+ const data1 = new Uint8Array(await response1.arrayBuffer());
3035
+ const hash1 = hashContent(data1);
3036
+ const streamPath2 = `/v1/stream/hash-verify-2-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3037
+ await fetch(`${getBaseUrl()}${streamPath2}`, {
3038
+ method: `PUT`,
3039
+ headers: { "Content-Type": `application/octet-stream` }
3040
+ });
3041
+ for (const chunk of chunks) await fetch(`${getBaseUrl()}${streamPath2}`, {
3042
+ method: `POST`,
3043
+ headers: { "Content-Type": `application/octet-stream` },
3044
+ body: chunk
3045
+ });
3046
+ const response2 = await fetch(`${getBaseUrl()}${streamPath2}`);
3047
+ const data2 = new Uint8Array(await response2.arrayBuffer());
3048
+ const hash2 = hashContent(data2);
3049
+ expect(hash1).toBe(hash2);
3050
+ expect(data1.length).toBe(data2.length);
3051
+ return true;
3052
+ }
3053
+ ), { numRuns: 15 });
3054
+ }, 15e3);
3055
+ test(`content hash changes with each append`, async () => {
3056
+ const streamPath = `/v1/stream/hash-changes-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3057
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3058
+ method: `PUT`,
3059
+ headers: { "Content-Type": `application/octet-stream` }
3060
+ });
3061
+ const hashes = [];
3062
+ for (let i = 0; i < 5; i++) {
3063
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3064
+ method: `POST`,
3065
+ headers: { "Content-Type": `application/octet-stream` },
3066
+ body: new Uint8Array([
3067
+ i,
3068
+ i + 1,
3069
+ i + 2
3070
+ ])
3071
+ });
3072
+ const response = await fetch(`${getBaseUrl()}${streamPath}`);
3073
+ const data = new Uint8Array(await response.arrayBuffer());
3074
+ hashes.push(hashContent(data));
3075
+ }
3076
+ const uniqueHashes = new Set(hashes);
3077
+ expect(uniqueHashes.size).toBe(5);
3078
+ });
3079
+ test(`empty stream has consistent hash`, async () => {
3080
+ const streamPath1 = `/v1/stream/empty-hash-1-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3081
+ const streamPath2 = `/v1/stream/empty-hash-2-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3082
+ await fetch(`${getBaseUrl()}${streamPath1}`, {
3083
+ method: `PUT`,
3084
+ headers: { "Content-Type": `application/octet-stream` }
3085
+ });
3086
+ await fetch(`${getBaseUrl()}${streamPath2}`, {
3087
+ method: `PUT`,
3088
+ headers: { "Content-Type": `application/octet-stream` }
3089
+ });
3090
+ const response1 = await fetch(`${getBaseUrl()}${streamPath1}`);
3091
+ const response2 = await fetch(`${getBaseUrl()}${streamPath2}`);
3092
+ const data1 = new Uint8Array(await response1.arrayBuffer());
3093
+ const data2 = new Uint8Array(await response2.arrayBuffer());
3094
+ expect(data1.length).toBe(0);
3095
+ expect(data2.length).toBe(0);
3096
+ expect(hashContent(data1)).toBe(hashContent(data2));
3097
+ });
3098
+ test(`deterministic ordering - same data in same order produces same hash`, async () => {
3099
+ await fc.assert(fc.asyncProperty(fc.array(fc.uint8Array({
3100
+ minLength: 1,
3101
+ maxLength: 50
3102
+ }), {
3103
+ minLength: 2,
3104
+ maxLength: 5
3105
+ }), async (chunks) => {
3106
+ const hashes = [];
3107
+ for (let run = 0; run < 2; run++) {
3108
+ const streamPath = `/v1/stream/order-hash-${run}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
3109
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3110
+ method: `PUT`,
3111
+ headers: { "Content-Type": `application/octet-stream` }
3112
+ });
3113
+ for (const chunk of chunks) await fetch(`${getBaseUrl()}${streamPath}`, {
3114
+ method: `POST`,
3115
+ headers: { "Content-Type": `application/octet-stream` },
3116
+ body: chunk
3117
+ });
3118
+ const response = await fetch(`${getBaseUrl()}${streamPath}`);
3119
+ const data = new Uint8Array(await response.arrayBuffer());
3120
+ hashes.push(hashContent(data));
3121
+ }
3122
+ expect(hashes[0]).toBe(hashes[1]);
3123
+ return true;
3124
+ }), { numRuns: 10 });
3125
+ });
3126
+ });
3127
+ });
3128
+ describe(`Idempotent Producer Operations`, () => {
3129
+ const PRODUCER_ID_HEADER = `Producer-Id`;
3130
+ const PRODUCER_EPOCH_HEADER = `Producer-Epoch`;
3131
+ const PRODUCER_SEQ_HEADER = `Producer-Seq`;
3132
+ const PRODUCER_EXPECTED_SEQ_HEADER = `Producer-Expected-Seq`;
3133
+ const PRODUCER_RECEIVED_SEQ_HEADER = `Producer-Received-Seq`;
3134
+ test(`should accept first append with producer headers (epoch=0, seq=0)`, async () => {
3135
+ const streamPath = `/v1/stream/producer-basic-${Date.now()}`;
3136
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3137
+ method: `PUT`,
3138
+ headers: { "Content-Type": `text/plain` }
3139
+ });
3140
+ const response = await fetch(`${getBaseUrl()}${streamPath}`, {
3141
+ method: `POST`,
3142
+ headers: {
3143
+ "Content-Type": `text/plain`,
3144
+ [PRODUCER_ID_HEADER]: `test-producer`,
3145
+ [PRODUCER_EPOCH_HEADER]: `0`,
3146
+ [PRODUCER_SEQ_HEADER]: `0`
3147
+ },
3148
+ body: `hello`
3149
+ });
3150
+ expect(response.status).toBe(200);
3151
+ expect(response.headers.get(STREAM_OFFSET_HEADER)).toBeTruthy();
3152
+ expect(response.headers.get(PRODUCER_EPOCH_HEADER)).toBe(`0`);
3153
+ });
3154
+ test(`should accept sequential producer sequences`, async () => {
3155
+ const streamPath = `/v1/stream/producer-seq-${Date.now()}`;
3156
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3157
+ method: `PUT`,
3158
+ headers: { "Content-Type": `text/plain` }
3159
+ });
3160
+ const r0 = await fetch(`${getBaseUrl()}${streamPath}`, {
3161
+ method: `POST`,
3162
+ headers: {
3163
+ "Content-Type": `text/plain`,
3164
+ [PRODUCER_ID_HEADER]: `test-producer`,
3165
+ [PRODUCER_EPOCH_HEADER]: `0`,
3166
+ [PRODUCER_SEQ_HEADER]: `0`
3167
+ },
3168
+ body: `msg0`
3169
+ });
3170
+ expect(r0.status).toBe(200);
3171
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3172
+ method: `POST`,
3173
+ headers: {
3174
+ "Content-Type": `text/plain`,
3175
+ [PRODUCER_ID_HEADER]: `test-producer`,
3176
+ [PRODUCER_EPOCH_HEADER]: `0`,
3177
+ [PRODUCER_SEQ_HEADER]: `1`
3178
+ },
3179
+ body: `msg1`
3180
+ });
3181
+ expect(r1.status).toBe(200);
3182
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3183
+ method: `POST`,
3184
+ headers: {
3185
+ "Content-Type": `text/plain`,
3186
+ [PRODUCER_ID_HEADER]: `test-producer`,
3187
+ [PRODUCER_EPOCH_HEADER]: `0`,
3188
+ [PRODUCER_SEQ_HEADER]: `2`
3189
+ },
3190
+ body: `msg2`
3191
+ });
3192
+ expect(r2.status).toBe(200);
3193
+ });
3194
+ test(`should return 204 for duplicate sequence (idempotent success)`, async () => {
3195
+ const streamPath = `/v1/stream/producer-dup-${Date.now()}`;
3196
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3197
+ method: `PUT`,
3198
+ headers: { "Content-Type": `text/plain` }
3199
+ });
3200
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3201
+ method: `POST`,
3202
+ headers: {
3203
+ "Content-Type": `text/plain`,
3204
+ [PRODUCER_ID_HEADER]: `test-producer`,
3205
+ [PRODUCER_EPOCH_HEADER]: `0`,
3206
+ [PRODUCER_SEQ_HEADER]: `0`
3207
+ },
3208
+ body: `hello`
3209
+ });
3210
+ expect(r1.status).toBe(200);
3211
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3212
+ method: `POST`,
3213
+ headers: {
3214
+ "Content-Type": `text/plain`,
3215
+ [PRODUCER_ID_HEADER]: `test-producer`,
3216
+ [PRODUCER_EPOCH_HEADER]: `0`,
3217
+ [PRODUCER_SEQ_HEADER]: `0`
3218
+ },
3219
+ body: `hello`
3220
+ });
3221
+ expect(r2.status).toBe(204);
3222
+ });
3223
+ test(`should accept epoch upgrade (new epoch starts at seq=0)`, async () => {
3224
+ const streamPath = `/v1/stream/producer-epoch-upgrade-${Date.now()}`;
3225
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3226
+ method: `PUT`,
3227
+ headers: { "Content-Type": `text/plain` }
3228
+ });
3229
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3230
+ method: `POST`,
3231
+ headers: {
3232
+ "Content-Type": `text/plain`,
3233
+ [PRODUCER_ID_HEADER]: `test-producer`,
3234
+ [PRODUCER_EPOCH_HEADER]: `0`,
3235
+ [PRODUCER_SEQ_HEADER]: `0`
3236
+ },
3237
+ body: `epoch0-msg0`
3238
+ });
3239
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3240
+ method: `POST`,
3241
+ headers: {
3242
+ "Content-Type": `text/plain`,
3243
+ [PRODUCER_ID_HEADER]: `test-producer`,
3244
+ [PRODUCER_EPOCH_HEADER]: `0`,
3245
+ [PRODUCER_SEQ_HEADER]: `1`
3246
+ },
3247
+ body: `epoch0-msg1`
3248
+ });
3249
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3250
+ method: `POST`,
3251
+ headers: {
3252
+ "Content-Type": `text/plain`,
3253
+ [PRODUCER_ID_HEADER]: `test-producer`,
3254
+ [PRODUCER_EPOCH_HEADER]: `1`,
3255
+ [PRODUCER_SEQ_HEADER]: `0`
3256
+ },
3257
+ body: `epoch1-msg0`
3258
+ });
3259
+ expect(r.status).toBe(200);
3260
+ expect(r.headers.get(PRODUCER_EPOCH_HEADER)).toBe(`1`);
3261
+ });
3262
+ test(`should reject stale epoch with 403 (zombie fencing)`, async () => {
3263
+ const streamPath = `/v1/stream/producer-stale-epoch-${Date.now()}`;
3264
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3265
+ method: `PUT`,
3266
+ headers: { "Content-Type": `text/plain` }
3267
+ });
3268
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3269
+ method: `POST`,
3270
+ headers: {
3271
+ "Content-Type": `text/plain`,
3272
+ [PRODUCER_ID_HEADER]: `test-producer`,
3273
+ [PRODUCER_EPOCH_HEADER]: `1`,
3274
+ [PRODUCER_SEQ_HEADER]: `0`
3275
+ },
3276
+ body: `msg`
3277
+ });
3278
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3279
+ method: `POST`,
3280
+ headers: {
3281
+ "Content-Type": `text/plain`,
3282
+ [PRODUCER_ID_HEADER]: `test-producer`,
3283
+ [PRODUCER_EPOCH_HEADER]: `0`,
3284
+ [PRODUCER_SEQ_HEADER]: `0`
3285
+ },
3286
+ body: `zombie`
3287
+ });
3288
+ expect(r.status).toBe(403);
3289
+ expect(r.headers.get(PRODUCER_EPOCH_HEADER)).toBe(`1`);
3290
+ });
3291
+ test(`should reject sequence gap with 409`, async () => {
3292
+ const streamPath = `/v1/stream/producer-seq-gap-${Date.now()}`;
3293
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3294
+ method: `PUT`,
3295
+ headers: { "Content-Type": `text/plain` }
3296
+ });
3297
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3298
+ method: `POST`,
3299
+ headers: {
3300
+ "Content-Type": `text/plain`,
3301
+ [PRODUCER_ID_HEADER]: `test-producer`,
3302
+ [PRODUCER_EPOCH_HEADER]: `0`,
3303
+ [PRODUCER_SEQ_HEADER]: `0`
3304
+ },
3305
+ body: `msg0`
3306
+ });
3307
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3308
+ method: `POST`,
3309
+ headers: {
3310
+ "Content-Type": `text/plain`,
3311
+ [PRODUCER_ID_HEADER]: `test-producer`,
3312
+ [PRODUCER_EPOCH_HEADER]: `0`,
3313
+ [PRODUCER_SEQ_HEADER]: `2`
3314
+ },
3315
+ body: `msg2`
3316
+ });
3317
+ expect(r.status).toBe(409);
3318
+ expect(r.headers.get(PRODUCER_EXPECTED_SEQ_HEADER)).toBe(`1`);
3319
+ expect(r.headers.get(PRODUCER_RECEIVED_SEQ_HEADER)).toBe(`2`);
3320
+ });
3321
+ test(`should reject epoch increase with seq != 0`, async () => {
3322
+ const streamPath = `/v1/stream/producer-epoch-bad-seq-${Date.now()}`;
3323
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3324
+ method: `PUT`,
3325
+ headers: { "Content-Type": `text/plain` }
3326
+ });
3327
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3328
+ method: `POST`,
3329
+ headers: {
3330
+ "Content-Type": `text/plain`,
3331
+ [PRODUCER_ID_HEADER]: `test-producer`,
3332
+ [PRODUCER_EPOCH_HEADER]: `0`,
3333
+ [PRODUCER_SEQ_HEADER]: `0`
3334
+ },
3335
+ body: `msg`
3336
+ });
3337
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3338
+ method: `POST`,
3339
+ headers: {
3340
+ "Content-Type": `text/plain`,
3341
+ [PRODUCER_ID_HEADER]: `test-producer`,
3342
+ [PRODUCER_EPOCH_HEADER]: `1`,
3343
+ [PRODUCER_SEQ_HEADER]: `5`
3344
+ },
3345
+ body: `bad`
3346
+ });
3347
+ expect(r.status).toBe(400);
3348
+ });
3349
+ test(`should require all producer headers together`, async () => {
3350
+ const streamPath = `/v1/stream/producer-partial-headers-${Date.now()}`;
3351
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3352
+ method: `PUT`,
3353
+ headers: { "Content-Type": `text/plain` }
3354
+ });
3355
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3356
+ method: `POST`,
3357
+ headers: {
3358
+ "Content-Type": `text/plain`,
3359
+ [PRODUCER_ID_HEADER]: `test-producer`
3360
+ },
3361
+ body: `msg`
3362
+ });
3363
+ expect(r1.status).toBe(400);
3364
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3365
+ method: `POST`,
3366
+ headers: {
3367
+ "Content-Type": `text/plain`,
3368
+ [PRODUCER_EPOCH_HEADER]: `0`
3369
+ },
3370
+ body: `msg`
3371
+ });
3372
+ expect(r2.status).toBe(400);
3373
+ const r3 = await fetch(`${getBaseUrl()}${streamPath}`, {
3374
+ method: `POST`,
3375
+ headers: {
3376
+ "Content-Type": `text/plain`,
3377
+ [PRODUCER_ID_HEADER]: `test-producer`,
3378
+ [PRODUCER_EPOCH_HEADER]: `0`
3379
+ },
3380
+ body: `msg`
3381
+ });
3382
+ expect(r3.status).toBe(400);
3383
+ });
3384
+ test(`should reject invalid integer formats in producer headers`, async () => {
3385
+ const streamPath = `/v1/stream/producer-invalid-format-${Date.now()}`;
3386
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3387
+ method: `PUT`,
3388
+ headers: { "Content-Type": `text/plain` }
3389
+ });
3390
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3391
+ method: `POST`,
3392
+ headers: {
3393
+ "Content-Type": `text/plain`,
3394
+ [PRODUCER_ID_HEADER]: `test-producer`,
3395
+ [PRODUCER_EPOCH_HEADER]: `0`,
3396
+ [PRODUCER_SEQ_HEADER]: `1abc`
3397
+ },
3398
+ body: `msg`
3399
+ });
3400
+ expect(r1.status).toBe(400);
3401
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3402
+ method: `POST`,
3403
+ headers: {
3404
+ "Content-Type": `text/plain`,
3405
+ [PRODUCER_ID_HEADER]: `test-producer`,
3406
+ [PRODUCER_EPOCH_HEADER]: `0xyz`,
3407
+ [PRODUCER_SEQ_HEADER]: `0`
3408
+ },
3409
+ body: `msg`
3410
+ });
3411
+ expect(r2.status).toBe(400);
3412
+ const r3 = await fetch(`${getBaseUrl()}${streamPath}`, {
3413
+ method: `POST`,
3414
+ headers: {
3415
+ "Content-Type": `text/plain`,
3416
+ [PRODUCER_ID_HEADER]: `test-producer`,
3417
+ [PRODUCER_EPOCH_HEADER]: `1e3`,
3418
+ [PRODUCER_SEQ_HEADER]: `0`
3419
+ },
3420
+ body: `msg`
3421
+ });
3422
+ expect(r3.status).toBe(400);
3423
+ const r4 = await fetch(`${getBaseUrl()}${streamPath}`, {
3424
+ method: `POST`,
3425
+ headers: {
3426
+ "Content-Type": `text/plain`,
3427
+ [PRODUCER_ID_HEADER]: `test-producer`,
3428
+ [PRODUCER_EPOCH_HEADER]: `-1`,
3429
+ [PRODUCER_SEQ_HEADER]: `0`
3430
+ },
3431
+ body: `msg`
3432
+ });
3433
+ expect(r4.status).toBe(400);
3434
+ const r5 = await fetch(`${getBaseUrl()}${streamPath}`, {
3435
+ method: `POST`,
3436
+ headers: {
3437
+ "Content-Type": `text/plain`,
3438
+ [PRODUCER_ID_HEADER]: `test-producer`,
3439
+ [PRODUCER_EPOCH_HEADER]: `0`,
3440
+ [PRODUCER_SEQ_HEADER]: `0`
3441
+ },
3442
+ body: `msg`
3443
+ });
3444
+ expect(r5.status).toBe(200);
3445
+ });
3446
+ test(`multiple producers should have independent state`, async () => {
3447
+ const streamPath = `/v1/stream/producer-multi-${Date.now()}`;
3448
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3449
+ method: `PUT`,
3450
+ headers: { "Content-Type": `text/plain` }
3451
+ });
3452
+ const rA0 = await fetch(`${getBaseUrl()}${streamPath}`, {
3453
+ method: `POST`,
3454
+ headers: {
3455
+ "Content-Type": `text/plain`,
3456
+ [PRODUCER_ID_HEADER]: `producer-A`,
3457
+ [PRODUCER_EPOCH_HEADER]: `0`,
3458
+ [PRODUCER_SEQ_HEADER]: `0`
3459
+ },
3460
+ body: `A0`
3461
+ });
3462
+ expect(rA0.status).toBe(200);
3463
+ const rB0 = await fetch(`${getBaseUrl()}${streamPath}`, {
3464
+ method: `POST`,
3465
+ headers: {
3466
+ "Content-Type": `text/plain`,
3467
+ [PRODUCER_ID_HEADER]: `producer-B`,
3468
+ [PRODUCER_EPOCH_HEADER]: `0`,
3469
+ [PRODUCER_SEQ_HEADER]: `0`
3470
+ },
3471
+ body: `B0`
3472
+ });
3473
+ expect(rB0.status).toBe(200);
3474
+ const rA1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3475
+ method: `POST`,
3476
+ headers: {
3477
+ "Content-Type": `text/plain`,
3478
+ [PRODUCER_ID_HEADER]: `producer-A`,
3479
+ [PRODUCER_EPOCH_HEADER]: `0`,
3480
+ [PRODUCER_SEQ_HEADER]: `1`
3481
+ },
3482
+ body: `A1`
3483
+ });
3484
+ expect(rA1.status).toBe(200);
3485
+ const rB1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3486
+ method: `POST`,
3487
+ headers: {
3488
+ "Content-Type": `text/plain`,
3489
+ [PRODUCER_ID_HEADER]: `producer-B`,
3490
+ [PRODUCER_EPOCH_HEADER]: `0`,
3491
+ [PRODUCER_SEQ_HEADER]: `1`
3492
+ },
3493
+ body: `B1`
3494
+ });
3495
+ expect(rB1.status).toBe(200);
3496
+ });
3497
+ test(`duplicate of seq=0 should not corrupt state`, async () => {
3498
+ const streamPath = `/v1/stream/producer-dup-seq0-${Date.now()}`;
3499
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3500
+ method: `PUT`,
3501
+ headers: { "Content-Type": `text/plain` }
3502
+ });
3503
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3504
+ method: `POST`,
3505
+ headers: {
3506
+ "Content-Type": `text/plain`,
3507
+ [PRODUCER_ID_HEADER]: `test-producer`,
3508
+ [PRODUCER_EPOCH_HEADER]: `0`,
3509
+ [PRODUCER_SEQ_HEADER]: `0`
3510
+ },
3511
+ body: `first`
3512
+ });
3513
+ expect(r1.status).toBe(200);
3514
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3515
+ method: `POST`,
3516
+ headers: {
3517
+ "Content-Type": `text/plain`,
3518
+ [PRODUCER_ID_HEADER]: `test-producer`,
3519
+ [PRODUCER_EPOCH_HEADER]: `0`,
3520
+ [PRODUCER_SEQ_HEADER]: `0`
3521
+ },
3522
+ body: `first`
3523
+ });
3524
+ expect(r2.status).toBe(204);
3525
+ const r3 = await fetch(`${getBaseUrl()}${streamPath}`, {
3526
+ method: `POST`,
3527
+ headers: {
3528
+ "Content-Type": `text/plain`,
3529
+ [PRODUCER_ID_HEADER]: `test-producer`,
3530
+ [PRODUCER_EPOCH_HEADER]: `0`,
3531
+ [PRODUCER_SEQ_HEADER]: `1`
3532
+ },
3533
+ body: `second`
3534
+ });
3535
+ expect(r3.status).toBe(200);
3536
+ });
3537
+ test(`duplicate response should return highest accepted seq, not request seq`, async () => {
3538
+ const streamPath = `/v1/stream/producer-dup-highest-seq-${Date.now()}`;
3539
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3540
+ method: `PUT`,
3541
+ headers: { "Content-Type": `text/plain` }
3542
+ });
3543
+ for (let i = 0; i < 3; i++) {
3544
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3545
+ method: `POST`,
3546
+ headers: {
3547
+ "Content-Type": `text/plain`,
3548
+ [PRODUCER_ID_HEADER]: `test-producer`,
3549
+ [PRODUCER_EPOCH_HEADER]: `0`,
3550
+ [PRODUCER_SEQ_HEADER]: `${i}`
3551
+ },
3552
+ body: `msg-${i}`
3553
+ });
3554
+ expect(r.status).toBe(200);
3555
+ expect(r.headers.get(PRODUCER_SEQ_HEADER)).toBe(`${i}`);
3556
+ }
3557
+ const dupResponse = await fetch(`${getBaseUrl()}${streamPath}`, {
3558
+ method: `POST`,
3559
+ headers: {
3560
+ "Content-Type": `text/plain`,
3561
+ [PRODUCER_ID_HEADER]: `test-producer`,
3562
+ [PRODUCER_EPOCH_HEADER]: `0`,
3563
+ [PRODUCER_SEQ_HEADER]: `1`
3564
+ },
3565
+ body: `msg-1`
3566
+ });
3567
+ expect(dupResponse.status).toBe(204);
3568
+ expect(dupResponse.headers.get(PRODUCER_SEQ_HEADER)).toBe(`2`);
3569
+ });
3570
+ test(`split-brain fencing scenario`, async () => {
3571
+ const streamPath = `/v1/stream/producer-split-brain-${Date.now()}`;
3572
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3573
+ method: `PUT`,
3574
+ headers: { "Content-Type": `text/plain` }
3575
+ });
3576
+ const rA0 = await fetch(`${getBaseUrl()}${streamPath}`, {
3577
+ method: `POST`,
3578
+ headers: {
3579
+ "Content-Type": `text/plain`,
3580
+ [PRODUCER_ID_HEADER]: `shared-producer`,
3581
+ [PRODUCER_EPOCH_HEADER]: `0`,
3582
+ [PRODUCER_SEQ_HEADER]: `0`
3583
+ },
3584
+ body: `A0`
3585
+ });
3586
+ expect(rA0.status).toBe(200);
3587
+ const rB0 = await fetch(`${getBaseUrl()}${streamPath}`, {
3588
+ method: `POST`,
3589
+ headers: {
3590
+ "Content-Type": `text/plain`,
3591
+ [PRODUCER_ID_HEADER]: `shared-producer`,
3592
+ [PRODUCER_EPOCH_HEADER]: `1`,
3593
+ [PRODUCER_SEQ_HEADER]: `0`
3594
+ },
3595
+ body: `B0`
3596
+ });
3597
+ expect(rB0.status).toBe(200);
3598
+ const rA1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3599
+ method: `POST`,
3600
+ headers: {
3601
+ "Content-Type": `text/plain`,
3602
+ [PRODUCER_ID_HEADER]: `shared-producer`,
3603
+ [PRODUCER_EPOCH_HEADER]: `0`,
3604
+ [PRODUCER_SEQ_HEADER]: `1`
3605
+ },
3606
+ body: `A1`
3607
+ });
3608
+ expect(rA1.status).toBe(403);
3609
+ expect(rA1.headers.get(PRODUCER_EPOCH_HEADER)).toBe(`1`);
3610
+ });
3611
+ test(`epoch rollback should be rejected`, async () => {
3612
+ const streamPath = `/v1/stream/producer-epoch-rollback-${Date.now()}`;
3613
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3614
+ method: `PUT`,
3615
+ headers: { "Content-Type": `text/plain` }
3616
+ });
3617
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3618
+ method: `POST`,
3619
+ headers: {
3620
+ "Content-Type": `text/plain`,
3621
+ [PRODUCER_ID_HEADER]: `test-producer`,
3622
+ [PRODUCER_EPOCH_HEADER]: `2`,
3623
+ [PRODUCER_SEQ_HEADER]: `0`
3624
+ },
3625
+ body: `msg`
3626
+ });
3627
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3628
+ method: `POST`,
3629
+ headers: {
3630
+ "Content-Type": `text/plain`,
3631
+ [PRODUCER_ID_HEADER]: `test-producer`,
3632
+ [PRODUCER_EPOCH_HEADER]: `1`,
3633
+ [PRODUCER_SEQ_HEADER]: `0`
3634
+ },
3635
+ body: `rollback`
3636
+ });
3637
+ expect(r.status).toBe(403);
3638
+ });
3639
+ test(`producer headers work with Stream-Seq header`, async () => {
3640
+ const streamPath = `/v1/stream/producer-with-stream-seq-${Date.now()}`;
3641
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3642
+ method: `PUT`,
3643
+ headers: { "Content-Type": `text/plain` }
3644
+ });
3645
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3646
+ method: `POST`,
3647
+ headers: {
3648
+ "Content-Type": `text/plain`,
3649
+ [PRODUCER_ID_HEADER]: `test-producer`,
3650
+ [PRODUCER_EPOCH_HEADER]: `0`,
3651
+ [PRODUCER_SEQ_HEADER]: `0`,
3652
+ [STREAM_SEQ_HEADER]: `app-seq-001`
3653
+ },
3654
+ body: `msg`
3655
+ });
3656
+ expect(r.status).toBe(200);
3657
+ });
3658
+ test(`producer duplicate should return 204 even with Stream-Seq header`, async () => {
3659
+ const streamPath = `/v1/stream/producer-dedupe-before-stream-seq-${Date.now()}`;
3660
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3661
+ method: `PUT`,
3662
+ headers: { "Content-Type": `text/plain` }
3663
+ });
3664
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3665
+ method: `POST`,
3666
+ headers: {
3667
+ "Content-Type": `text/plain`,
3668
+ [PRODUCER_ID_HEADER]: `test-producer`,
3669
+ [PRODUCER_EPOCH_HEADER]: `0`,
3670
+ [PRODUCER_SEQ_HEADER]: `0`,
3671
+ [STREAM_SEQ_HEADER]: `app-seq-001`
3672
+ },
3673
+ body: `msg`
3674
+ });
3675
+ expect(r1.status).toBe(200);
3676
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3677
+ method: `POST`,
3678
+ headers: {
3679
+ "Content-Type": `text/plain`,
3680
+ [PRODUCER_ID_HEADER]: `test-producer`,
3681
+ [PRODUCER_EPOCH_HEADER]: `0`,
3682
+ [PRODUCER_SEQ_HEADER]: `0`,
3683
+ [STREAM_SEQ_HEADER]: `app-seq-001`
3684
+ },
3685
+ body: `msg`
3686
+ });
3687
+ expect(r2.status).toBe(204);
3688
+ });
3689
+ test(`should store and read back data correctly`, async () => {
3690
+ const streamPath = `/v1/stream/producer-readback-${Date.now()}`;
3691
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3692
+ method: `PUT`,
3693
+ headers: { "Content-Type": `text/plain` }
3694
+ });
3695
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3696
+ method: `POST`,
3697
+ headers: {
3698
+ "Content-Type": `text/plain`,
3699
+ [PRODUCER_ID_HEADER]: `test-producer`,
3700
+ [PRODUCER_EPOCH_HEADER]: `0`,
3701
+ [PRODUCER_SEQ_HEADER]: `0`
3702
+ },
3703
+ body: `hello world`
3704
+ });
3705
+ expect(r.status).toBe(200);
3706
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3707
+ expect(readResponse.status).toBe(200);
3708
+ const content = await readResponse.text();
3709
+ expect(content).toBe(`hello world`);
3710
+ });
3711
+ test(`should preserve order of sequential producer writes`, async () => {
3712
+ const streamPath = `/v1/stream/producer-order-${Date.now()}`;
3713
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3714
+ method: `PUT`,
3715
+ headers: { "Content-Type": `text/plain` }
3716
+ });
3717
+ for (let i = 0; i < 5; i++) {
3718
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3719
+ method: `POST`,
3720
+ headers: {
3721
+ "Content-Type": `text/plain`,
3722
+ [PRODUCER_ID_HEADER]: `test-producer`,
3723
+ [PRODUCER_EPOCH_HEADER]: `0`,
3724
+ [PRODUCER_SEQ_HEADER]: `${i}`
3725
+ },
3726
+ body: `msg-${i}`
3727
+ });
3728
+ expect(r.status).toBe(200);
3729
+ }
3730
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3731
+ const content = await readResponse.text();
3732
+ expect(content).toBe(`msg-0msg-1msg-2msg-3msg-4`);
3733
+ });
3734
+ test(`duplicate should not corrupt or duplicate data`, async () => {
3735
+ const streamPath = `/v1/stream/producer-dup-integrity-${Date.now()}`;
3736
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3737
+ method: `PUT`,
3738
+ headers: { "Content-Type": `text/plain` }
3739
+ });
3740
+ const r1 = await fetch(`${getBaseUrl()}${streamPath}`, {
3741
+ method: `POST`,
3742
+ headers: {
3743
+ "Content-Type": `text/plain`,
3744
+ [PRODUCER_ID_HEADER]: `test-producer`,
3745
+ [PRODUCER_EPOCH_HEADER]: `0`,
3746
+ [PRODUCER_SEQ_HEADER]: `0`
3747
+ },
3748
+ body: `first`
3749
+ });
3750
+ expect(r1.status).toBe(200);
3751
+ const r2 = await fetch(`${getBaseUrl()}${streamPath}`, {
3752
+ method: `POST`,
3753
+ headers: {
3754
+ "Content-Type": `text/plain`,
3755
+ [PRODUCER_ID_HEADER]: `test-producer`,
3756
+ [PRODUCER_EPOCH_HEADER]: `0`,
3757
+ [PRODUCER_SEQ_HEADER]: `0`
3758
+ },
3759
+ body: `first`
3760
+ });
3761
+ expect(r2.status).toBe(204);
3762
+ const r3 = await fetch(`${getBaseUrl()}${streamPath}`, {
3763
+ method: `POST`,
3764
+ headers: {
3765
+ "Content-Type": `text/plain`,
3766
+ [PRODUCER_ID_HEADER]: `test-producer`,
3767
+ [PRODUCER_EPOCH_HEADER]: `0`,
3768
+ [PRODUCER_SEQ_HEADER]: `1`
3769
+ },
3770
+ body: `second`
3771
+ });
3772
+ expect(r3.status).toBe(200);
3773
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3774
+ const content = await readResponse.text();
3775
+ expect(content).toBe(`firstsecond`);
3776
+ });
3777
+ test(`multiple producers should interleave correctly`, async () => {
3778
+ const streamPath = `/v1/stream/producer-interleave-${Date.now()}`;
3779
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3780
+ method: `PUT`,
3781
+ headers: { "Content-Type": `text/plain` }
3782
+ });
3783
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3784
+ method: `POST`,
3785
+ headers: {
3786
+ "Content-Type": `text/plain`,
3787
+ [PRODUCER_ID_HEADER]: `producer-A`,
3788
+ [PRODUCER_EPOCH_HEADER]: `0`,
3789
+ [PRODUCER_SEQ_HEADER]: `0`
3790
+ },
3791
+ body: `A0`
3792
+ });
3793
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3794
+ method: `POST`,
3795
+ headers: {
3796
+ "Content-Type": `text/plain`,
3797
+ [PRODUCER_ID_HEADER]: `producer-B`,
3798
+ [PRODUCER_EPOCH_HEADER]: `0`,
3799
+ [PRODUCER_SEQ_HEADER]: `0`
3800
+ },
3801
+ body: `B0`
3802
+ });
3803
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3804
+ method: `POST`,
3805
+ headers: {
3806
+ "Content-Type": `text/plain`,
3807
+ [PRODUCER_ID_HEADER]: `producer-A`,
3808
+ [PRODUCER_EPOCH_HEADER]: `0`,
3809
+ [PRODUCER_SEQ_HEADER]: `1`
3810
+ },
3811
+ body: `A1`
3812
+ });
3813
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3814
+ method: `POST`,
3815
+ headers: {
3816
+ "Content-Type": `text/plain`,
3817
+ [PRODUCER_ID_HEADER]: `producer-B`,
3818
+ [PRODUCER_EPOCH_HEADER]: `0`,
3819
+ [PRODUCER_SEQ_HEADER]: `1`
3820
+ },
3821
+ body: `B1`
3822
+ });
3823
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3824
+ const content = await readResponse.text();
3825
+ expect(content).toBe(`A0B0A1B1`);
3826
+ });
3827
+ test(`should store and read back JSON object correctly`, async () => {
3828
+ const streamPath = `/v1/stream/producer-json-obj-${Date.now()}`;
3829
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3830
+ method: `PUT`,
3831
+ headers: { "Content-Type": `application/json` }
3832
+ });
3833
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3834
+ method: `POST`,
3835
+ headers: {
3836
+ "Content-Type": `application/json`,
3837
+ [PRODUCER_ID_HEADER]: `test-producer`,
3838
+ [PRODUCER_EPOCH_HEADER]: `0`,
3839
+ [PRODUCER_SEQ_HEADER]: `0`
3840
+ },
3841
+ body: JSON.stringify({
3842
+ event: `test`,
3843
+ value: 42
3844
+ })
3845
+ });
3846
+ expect(r.status).toBe(200);
3847
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3848
+ const data = await readResponse.json();
3849
+ expect(data).toEqual([{
3850
+ event: `test`,
3851
+ value: 42
3852
+ }]);
3853
+ });
3854
+ test(`should preserve order of JSON appends with producer`, async () => {
3855
+ const streamPath = `/v1/stream/producer-json-order-${Date.now()}`;
3856
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3857
+ method: `PUT`,
3858
+ headers: { "Content-Type": `application/json` }
3859
+ });
3860
+ for (let i = 0; i < 5; i++) {
3861
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3862
+ method: `POST`,
3863
+ headers: {
3864
+ "Content-Type": `application/json`,
3865
+ [PRODUCER_ID_HEADER]: `test-producer`,
3866
+ [PRODUCER_EPOCH_HEADER]: `0`,
3867
+ [PRODUCER_SEQ_HEADER]: `${i}`
3868
+ },
3869
+ body: JSON.stringify({
3870
+ seq: i,
3871
+ data: `msg-${i}`
3872
+ })
3873
+ });
3874
+ expect(r.status).toBe(200);
3875
+ }
3876
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3877
+ const data = await readResponse.json();
3878
+ expect(data).toEqual([
3879
+ {
3880
+ seq: 0,
3881
+ data: `msg-0`
3882
+ },
3883
+ {
3884
+ seq: 1,
3885
+ data: `msg-1`
3886
+ },
3887
+ {
3888
+ seq: 2,
3889
+ data: `msg-2`
3890
+ },
3891
+ {
3892
+ seq: 3,
3893
+ data: `msg-3`
3894
+ },
3895
+ {
3896
+ seq: 4,
3897
+ data: `msg-4`
3898
+ }
3899
+ ]);
3900
+ });
3901
+ test(`JSON duplicate should not corrupt data`, async () => {
3902
+ const streamPath = `/v1/stream/producer-json-dup-${Date.now()}`;
3903
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3904
+ method: `PUT`,
3905
+ headers: { "Content-Type": `application/json` }
3906
+ });
3907
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3908
+ method: `POST`,
3909
+ headers: {
3910
+ "Content-Type": `application/json`,
3911
+ [PRODUCER_ID_HEADER]: `test-producer`,
3912
+ [PRODUCER_EPOCH_HEADER]: `0`,
3913
+ [PRODUCER_SEQ_HEADER]: `0`
3914
+ },
3915
+ body: JSON.stringify({ id: 1 })
3916
+ });
3917
+ const dup = await fetch(`${getBaseUrl()}${streamPath}`, {
3918
+ method: `POST`,
3919
+ headers: {
3920
+ "Content-Type": `application/json`,
3921
+ [PRODUCER_ID_HEADER]: `test-producer`,
3922
+ [PRODUCER_EPOCH_HEADER]: `0`,
3923
+ [PRODUCER_SEQ_HEADER]: `0`
3924
+ },
3925
+ body: JSON.stringify({ id: 1 })
3926
+ });
3927
+ expect(dup.status).toBe(204);
3928
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3929
+ method: `POST`,
3930
+ headers: {
3931
+ "Content-Type": `application/json`,
3932
+ [PRODUCER_ID_HEADER]: `test-producer`,
3933
+ [PRODUCER_EPOCH_HEADER]: `0`,
3934
+ [PRODUCER_SEQ_HEADER]: `1`
3935
+ },
3936
+ body: JSON.stringify({ id: 2 })
3937
+ });
3938
+ const readResponse = await fetch(`${getBaseUrl()}${streamPath}`);
3939
+ const data = await readResponse.json();
3940
+ expect(data).toEqual([{ id: 1 }, { id: 2 }]);
3941
+ });
3942
+ test(`should reject invalid JSON with producer headers`, async () => {
3943
+ const streamPath = `/v1/stream/producer-json-invalid-${Date.now()}`;
3944
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3945
+ method: `PUT`,
3946
+ headers: { "Content-Type": `application/json` }
3947
+ });
3948
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3949
+ method: `POST`,
3950
+ headers: {
3951
+ "Content-Type": `application/json`,
3952
+ [PRODUCER_ID_HEADER]: `test-producer`,
3953
+ [PRODUCER_EPOCH_HEADER]: `0`,
3954
+ [PRODUCER_SEQ_HEADER]: `0`
3955
+ },
3956
+ body: `{ invalid json }`
3957
+ });
3958
+ expect(r.status).toBe(400);
3959
+ });
3960
+ test(`should reject empty JSON array with producer headers`, async () => {
3961
+ const streamPath = `/v1/stream/producer-json-empty-${Date.now()}`;
3962
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3963
+ method: `PUT`,
3964
+ headers: { "Content-Type": `application/json` }
3965
+ });
3966
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3967
+ method: `POST`,
3968
+ headers: {
3969
+ "Content-Type": `application/json`,
3970
+ [PRODUCER_ID_HEADER]: `test-producer`,
3971
+ [PRODUCER_EPOCH_HEADER]: `0`,
3972
+ [PRODUCER_SEQ_HEADER]: `0`
3973
+ },
3974
+ body: `[]`
3975
+ });
3976
+ expect(r.status).toBe(400);
3977
+ });
3978
+ test(`should return 404 for non-existent stream`, async () => {
3979
+ const streamPath = `/v1/stream/producer-404-${Date.now()}`;
3980
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3981
+ method: `POST`,
3982
+ headers: {
3983
+ "Content-Type": `text/plain`,
3984
+ [PRODUCER_ID_HEADER]: `test-producer`,
3985
+ [PRODUCER_EPOCH_HEADER]: `0`,
3986
+ [PRODUCER_SEQ_HEADER]: `0`
3987
+ },
3988
+ body: `data`
3989
+ });
3990
+ expect(r.status).toBe(404);
3991
+ });
3992
+ test(`should return 409 for content-type mismatch`, async () => {
3993
+ const streamPath = `/v1/stream/producer-ct-mismatch-${Date.now()}`;
3994
+ await fetch(`${getBaseUrl()}${streamPath}`, {
3995
+ method: `PUT`,
3996
+ headers: { "Content-Type": `text/plain` }
3997
+ });
3998
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
3999
+ method: `POST`,
4000
+ headers: {
4001
+ "Content-Type": `application/json`,
4002
+ [PRODUCER_ID_HEADER]: `test-producer`,
4003
+ [PRODUCER_EPOCH_HEADER]: `0`,
4004
+ [PRODUCER_SEQ_HEADER]: `0`
4005
+ },
4006
+ body: JSON.stringify({ data: `test` })
4007
+ });
4008
+ expect(r.status).toBe(409);
4009
+ });
4010
+ test(`should return 400 for empty body`, async () => {
4011
+ const streamPath = `/v1/stream/producer-empty-body-${Date.now()}`;
4012
+ await fetch(`${getBaseUrl()}${streamPath}`, {
4013
+ method: `PUT`,
4014
+ headers: { "Content-Type": `text/plain` }
4015
+ });
4016
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
4017
+ method: `POST`,
4018
+ headers: {
4019
+ "Content-Type": `text/plain`,
4020
+ [PRODUCER_ID_HEADER]: `test-producer`,
4021
+ [PRODUCER_EPOCH_HEADER]: `0`,
4022
+ [PRODUCER_SEQ_HEADER]: `0`
4023
+ },
4024
+ body: ``
4025
+ });
4026
+ expect(r.status).toBe(400);
4027
+ });
4028
+ test(`should reject empty Producer-Id`, async () => {
4029
+ const streamPath = `/v1/stream/producer-empty-id-${Date.now()}`;
4030
+ await fetch(`${getBaseUrl()}${streamPath}`, {
4031
+ method: `PUT`,
4032
+ headers: { "Content-Type": `text/plain` }
4033
+ });
4034
+ const r = await fetch(`${getBaseUrl()}${streamPath}`, {
4035
+ method: `POST`,
4036
+ headers: {
4037
+ "Content-Type": `text/plain`,
4038
+ [PRODUCER_ID_HEADER]: ``,
4039
+ [PRODUCER_EPOCH_HEADER]: `0`,
4040
+ [PRODUCER_SEQ_HEADER]: `0`
4041
+ },
4042
+ body: `data`
4043
+ });
4044
+ expect(r.status).toBe(400);
4045
+ });
2471
4046
  });
2472
4047
  }
2473
4048