Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import { describe, expect, it } from "vitest";import { countCommentsByCommit, filterCommentsByCommit, groupCommentsByVersion, type CommentView} from "./comments";import type { CommitSummary } from "$lib/api/repo";
const comment = (rkey: string, createdAt: string): CommentView => ({ uri: `at://did:plc:alice/sh.tangled.feed.comment/${rkey}`, rkey, authorDid: "did:plc:alice", authorHandle: "alice.test", createdAt, body: rkey, bodyHtml: null});
const versions = [{ createdAt: "2026-01-01T00:00:00Z" }, { createdAt: "2026-01-03T00:00:00Z" }];
const layout = (comments: CommentView[]) => groupCommentsByVersion(comments, versions).map((group) => [ group.version, group.comments.map((c) => c.rkey) ]);
describe("groupCommentsByVersion", () => { it("files a comment under the newest version at or before it", () => { // on the boundary belongs to that version, not the one before expect( layout([ comment("boundary", "2026-01-01T00:00:00Z"), comment("during-v0", "2026-01-02T00:00:00Z"), comment("after-v1", "2026-01-04T00:00:00Z") ]) ).toEqual([ [0, ["boundary", "during-v0"]], [1, ["after-v1"]] ]); });
it("keeps a version with no comments, so the timeline stays complete", () => { expect(layout([comment("only", "2026-01-02T00:00:00Z")])).toEqual([ [0, ["only"]], [1, []] ]); });
it("puts anything older than v0 in a headerless leading group", () => { expect(layout([comment("early", "2025-12-31T00:00:00Z")])).toEqual([ [undefined, ["early"]], [0, []], [1, []] ]); });
it("orders comments itself rather than trusting the caller", () => { expect( layout([ comment("late", "2026-01-04T00:00:00Z"), comment("early", "2026-01-02T00:00:00Z") ]) ).toEqual([ [0, ["early"]], [1, ["late"]] ]); });
it("has nothing to group without versions", () => { expect(groupCommentsByVersion([comment("a", "2026-01-02T00:00:00Z")], [])).toEqual([ { version: undefined, comments: [expect.objectContaining({ rkey: "a" })] } ]); });});
const pinned = (rkey: string, oid: string): CommentView => ({ ...comment(rkey, "2026-01-02T00:00:00Z"), embed: { $type: "sh.tangled.embed.commit", repo: "did:plc:alice", commit: { $type: "sh.tangled.git.oid", oid } }});
describe("countCommentsByCommit", () => { it("sums the comments pinned to one commit and skips the unpinned ones", () => { const counts = countCommentsByCommit([ pinned("a", "cafe1234"), pinned("b", "cafe1234"), pinned("c", "beef5678"), comment("loose", "2026-01-02T00:00:00Z") ]); expect([...counts]).toEqual([ ["cafe1234", 2], ["beef5678", 1] ]); });
it("leaves an unpinned commit out rather than reporting 0", () => { const counts = countCommentsByCommit([pinned("a", "cafe1234")]); expect(counts.has("beef5678")).toBe(false); });});
const commit = (hash: string): CommitSummary => ({ hash, shortHash: hash.slice(0, 8), subject: hash, body: "", authorName: "alice", authorEmail: "alice@test", when: "2026-01-02T00:00:00Z"});
describe("filterCommentsByCommit", () => { const commits = [commit("cafe1234"), commit("beef5678")]; const rkeys = (comments: CommentView[]) => comments.map((c) => c.rkey);
it("keeps an unpinned comment and drops a pin the filter rejects", () => { expect( rkeys( filterCommentsByCommit( [ comment("loose", "2026-01-02T00:00:00Z"), pinned("in", "cafe1234"), pinned("out", "beef5678") ], commits, (c) => c.hash === "cafe1234" ) ) ).toEqual(["loose", "in"]); });
it("drops a pin that names no known commit", () => { // an older version's commit, or one a force-push dropped: never in range expect( rkeys(filterCommentsByCommit([pinned("gone", "d00d9999")], commits, () => true)) ).toEqual([]); });
it("filters nothing without a filter", () => { const comments = [pinned("gone", "d00d9999"), comment("loose", "2026-01-02T00:00:00Z")]; expect(filterCommentsByCommit(comments, commits)).toBe(comments); });});