~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
59 lines · 2.1 KB Raw History
import assert from "node:assert/strict";
import { test } from "node:test";

import { safeParse } from "@atcute/lexicons/validations";

import { IngestBatchSchema, checkIngestBatch, namespaceOf } from "../src/ingest";

const segment = {
  idx: 6000,
  name: "seg_00000004mo.jss",
  sizeBytes: 268937693,
  checksum: "0a1b2c3d4e5f6071",
  eventCount: 3178197,
  blockCount: 818,
  minSeq: 21322223343,
  maxSeq: 21325402013,
  minWitnessedAt: 1785768533809776,
  maxWitnessedAt: 1785768618450665,
  collections: [
    { nsid: "app.bsky.feed.like", count: 1885670 },
    { nsid: "app.bsky.feed.post", count: 620964 },
  ],
} as const;

test("accepts a well-formed batch and preserves values", () => {
  const result = safeParse(IngestBatchSchema, { segments: [segment] });
  assert.ok(result.ok);
  assert.equal(result.value.segments.length, 1);
  assert.deepEqual(result.value.segments[0], segment);
  assert.equal(checkIngestBatch(result.value), null);
});

test("rejects an empty batch", () => {
  assert.equal(safeParse(IngestBatchSchema, { segments: [] }).ok, false);
});

test("rejects non-integer counts", () => {
  const bad = { ...segment, collections: [{ nsid: "app.bsky.feed.like", count: "many" }] };
  assert.equal(safeParse(IngestBatchSchema, { segments: [bad] }).ok, false);
});

test("flags duplicate collections within a segment", () => {
  const dup = { ...segment, collections: [...segment.collections, { nsid: "app.bsky.feed.like", count: 1 }] };
  const result = safeParse(IngestBatchSchema, { segments: [dup] });
  assert.ok(result.ok);
  assert.match(checkIngestBatch(result.value) ?? "", /duplicate nsid/);
});

test("flags seq ranges that run backwards", () => {
  const result = safeParse(IngestBatchSchema, { segments: [{ ...segment, minSeq: 5, maxSeq: 4 }] });
  assert.ok(result.ok);
  assert.match(checkIngestBatch(result.value) ?? "", /maxSeq < minSeq/);
});

test("namespaceOf takes the first two labels", () => {
  assert.equal(namespaceOf("app.bsky.feed.like"), "app.bsky");
  assert.equal(namespaceOf("net.anisota.beta.game.log"), "net.anisota");
  assert.equal(namespaceOf("sh.tangled"), "sh.tangled");
});