|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from "@jest/globals" |
| 2 | +import { TestNetwork, SeedClient, usersSeed } from "@atproto/dev-env" |
| 3 | +import { AtpAgent } from "@atproto/api" |
| 4 | +import { Trotsky, StepTimeline } from "../../lib/trotsky" |
| 5 | + |
| 6 | +describe("StepTimeline", () => { |
| 7 | + let network: TestNetwork |
| 8 | + let agent: AtpAgent |
| 9 | + let sc: SeedClient |
| 10 | + |
| 11 | + // accounts |
| 12 | + let bob: { "did": string; "handle": string; "password": string } |
| 13 | + let alice: { "did": string; "handle": string; "password": string } |
| 14 | + let carol: { "did": string; "handle": string; "password": string } |
| 15 | + |
| 16 | + beforeAll(async () => { |
| 17 | + network = await TestNetwork.create({ |
| 18 | + "dbPostgresSchema": "trotsky_step_timeline" |
| 19 | + }) |
| 20 | + |
| 21 | + agent = network.pds.getClient() |
| 22 | + sc = network.getSeedClient() |
| 23 | + |
| 24 | + // Seed users |
| 25 | + await usersSeed(sc) |
| 26 | + bob = sc.accounts[sc.dids.bob] |
| 27 | + alice = sc.accounts[sc.dids.alice] |
| 28 | + carol = sc.accounts[sc.dids.carol] |
| 29 | + |
| 30 | + // Login as bob |
| 31 | + await agent.login({ "identifier": bob.handle, "password": bob.password }) |
| 32 | + |
| 33 | + // Bob follows Alice and Carol |
| 34 | + await agent.app.bsky.graph.follow.create( |
| 35 | + { "repo": bob.did }, |
| 36 | + { "subject": alice.did, "createdAt": new Date().toISOString() } |
| 37 | + ) |
| 38 | + await agent.app.bsky.graph.follow.create( |
| 39 | + { "repo": bob.did }, |
| 40 | + { "subject": carol.did, "createdAt": new Date().toISOString() } |
| 41 | + ) |
| 42 | + |
| 43 | + // Alice and Carol create posts |
| 44 | + await sc.post(alice.did, "Alice's first post about TypeScript") |
| 45 | + await sc.post(alice.did, "Alice's second post about JavaScript") |
| 46 | + await sc.post(carol.did, "Carol's post about Python") |
| 47 | + await sc.post(carol.did, "Carol's post about Rust") |
| 48 | + |
| 49 | + await network.processAll() |
| 50 | + }, 120e3) |
| 51 | + |
| 52 | + afterAll(async () => { |
| 53 | + await network.close() |
| 54 | + }) |
| 55 | + |
| 56 | + test("should clone properly", () => { |
| 57 | + const step = Trotsky.init(agent).timeline() |
| 58 | + const cloned = step.clone() |
| 59 | + expect(cloned).toBeInstanceOf(StepTimeline) |
| 60 | + }) |
| 61 | + |
| 62 | + test("should get timeline posts", async () => { |
| 63 | + const timeline = await Trotsky.init(agent) |
| 64 | + .timeline() |
| 65 | + .runHere() |
| 66 | + |
| 67 | + expect(timeline).toBeInstanceOf(StepTimeline) |
| 68 | + expect(timeline.output).toBeInstanceOf(Array) |
| 69 | + // Bob should see posts from Alice and Carol (at least 4 posts) |
| 70 | + expect(timeline.output.length).toBeGreaterThanOrEqual(4) |
| 71 | + }) |
| 72 | + |
| 73 | + test("should return posts with correct structure", async () => { |
| 74 | + const timeline = await Trotsky.init(agent) |
| 75 | + .timeline() |
| 76 | + .runHere() |
| 77 | + |
| 78 | + timeline.output.forEach(post => { |
| 79 | + expect(post).toHaveProperty("uri") |
| 80 | + expect(post).toHaveProperty("cid") |
| 81 | + expect(post).toHaveProperty("author") |
| 82 | + expect(post).toHaveProperty("record") |
| 83 | + expect(post.author).toHaveProperty("handle") |
| 84 | + expect(post.author).toHaveProperty("did") |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + test("should verify timeline contains posts from followed users", async () => { |
| 89 | + const timeline = await Trotsky.init(agent) |
| 90 | + .timeline() |
| 91 | + .runHere() |
| 92 | + |
| 93 | + const authorDids = timeline.output.map(post => post.author.did) |
| 94 | + |
| 95 | + // Timeline should contain posts from Alice or Carol |
| 96 | + const hasAliceOrCarol = authorDids.some(did => |
| 97 | + did === alice.did || did === carol.did |
| 98 | + ) |
| 99 | + expect(hasAliceOrCarol).toBe(true) |
| 100 | + }) |
| 101 | + |
| 102 | + test("should iterate through each timeline post", async () => { |
| 103 | + const postUris: string[] = [] |
| 104 | + |
| 105 | + await Trotsky.init(agent) |
| 106 | + .timeline() |
| 107 | + .take(3) |
| 108 | + .each() |
| 109 | + .tap((step) => { |
| 110 | + if (step?.context?.uri) { |
| 111 | + postUris.push(step.context.uri) |
| 112 | + } |
| 113 | + }) |
| 114 | + .run() |
| 115 | + |
| 116 | + expect(postUris.length).toBeGreaterThan(0) |
| 117 | + expect(postUris.length).toBeLessThanOrEqual(3) |
| 118 | + }) |
| 119 | + |
| 120 | + test("should filter timeline posts with when()", async () => { |
| 121 | + const filteredPosts: string[] = [] |
| 122 | + |
| 123 | + await Trotsky.init(agent) |
| 124 | + .timeline() |
| 125 | + .take(10) |
| 126 | + .each() |
| 127 | + .when((step) => step?.context?.author?.did === alice.did) |
| 128 | + .tap((step) => { |
| 129 | + if (step?.context?.uri) { |
| 130 | + filteredPosts.push(step.context.uri) |
| 131 | + } |
| 132 | + }) |
| 133 | + .run() |
| 134 | + |
| 135 | + // All filtered posts should be from Alice |
| 136 | + const timeline = await Trotsky.init(agent).timeline().runHere() |
| 137 | + const alicePosts = timeline.output.filter(p => p.author.did === alice.did) |
| 138 | + |
| 139 | + expect(filteredPosts.length).toBeLessThanOrEqual(alicePosts.length) |
| 140 | + }) |
| 141 | + |
| 142 | + test("should handle pagination with take()", async () => { |
| 143 | + const timeline = await Trotsky.init(agent) |
| 144 | + .timeline() |
| 145 | + .take(2) |
| 146 | + .runHere() |
| 147 | + |
| 148 | + expect(timeline.output.length).toBeLessThanOrEqual(2) |
| 149 | + }) |
| 150 | + |
| 151 | + test("should work with custom query parameters", async () => { |
| 152 | + const timeline = await Trotsky.init(agent) |
| 153 | + .timeline({ "limit": 5 }) |
| 154 | + .runHere() |
| 155 | + |
| 156 | + expect(timeline.output).toBeInstanceOf(Array) |
| 157 | + expect(timeline.output.length).toBeLessThanOrEqual(5) |
| 158 | + }) |
| 159 | + |
| 160 | + test("should return indexed timestamps", async () => { |
| 161 | + const timeline = await Trotsky.init(agent) |
| 162 | + .timeline() |
| 163 | + .take(5) |
| 164 | + .runHere() |
| 165 | + |
| 166 | + timeline.output.forEach(post => { |
| 167 | + expect(post).toHaveProperty("indexedAt") |
| 168 | + expect(post.indexedAt).toBeTruthy() |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + test("should work with tap() to process results", async () => { |
| 173 | + let processedCount = 0 |
| 174 | + |
| 175 | + await Trotsky.init(agent) |
| 176 | + .timeline() |
| 177 | + .take(3) |
| 178 | + .each() |
| 179 | + .tap(() => { |
| 180 | + processedCount++ |
| 181 | + }) |
| 182 | + .run() |
| 183 | + |
| 184 | + expect(processedCount).toBeGreaterThan(0) |
| 185 | + expect(processedCount).toBeLessThanOrEqual(3) |
| 186 | + }) |
| 187 | + |
| 188 | + test("should handle empty timeline for new user", async () => { |
| 189 | + // Create a new user who doesn't follow anyone |
| 190 | + await sc.createAccount("newuser", { |
| 191 | + "handle": "newuser.test", |
| 192 | + "email": "newuser@test.com", |
| 193 | + "password": "password" |
| 194 | + }) |
| 195 | + |
| 196 | + const newAgent = network.pds.getClient() |
| 197 | + await newAgent.login({ "identifier": "newuser.test", "password": "password" }) |
| 198 | + |
| 199 | + await network.processAll() |
| 200 | + |
| 201 | + const timeline = await Trotsky.init(newAgent) |
| 202 | + .timeline() |
| 203 | + .runHere() |
| 204 | + |
| 205 | + expect(timeline.output).toBeInstanceOf(Array) |
| 206 | + // New user should have empty or minimal timeline |
| 207 | + expect(timeline.output.length).toBeLessThanOrEqual(10) |
| 208 | + }) |
| 209 | + |
| 210 | + test("should support algorithm parameter", async () => { |
| 211 | + const timeline = await Trotsky.init(agent) |
| 212 | + .timeline({ "algorithm": "reverse-chronological" }) |
| 213 | + .take(5) |
| 214 | + .runHere() |
| 215 | + |
| 216 | + expect(timeline.output).toBeInstanceOf(Array) |
| 217 | + }) |
| 218 | +}) |
0 commit comments