Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

75.9 KB
Raw Blame
import { randomUUID } from 'node:crypto'
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { storeOldLeads } from '../src/superTabs/leads.test-support.js'
import { MongoMemoryReplSet } from 'mongodb-memory-server-core'
import { PrismaClient } from '@prisma/client'
import Fastify, { type FastifyInstance } from 'fastify'
import type { AuthUser } from '../src/lib/ssoAuth.js'
import { SuperTabStore } from '../src/superTabs/store.js'
import { superTabRoutes } from '../src/routes/superTabs.js'
import { errorHandler } from '../src/middlewares/errorHandler.js'
import { migrateSwarm } from '../src/superTabs/migration/migrate.js'
import * as parts from '../src/superTabs/parts/index.js'
import { swarmTransaction } from '../src/superTabs/cache.js'
import { guardedWrite } from '../src/superTabs/parts/write.js'
import { SwarmReads } from '../src/superTabs/read/service.js'
import { HostReads } from '../src/superTabs/read/host.js'
import { buddyChatId } from '../src/superTabs/buddies.js'
import { INBOX_READS, INBOX_WINDOW_MS, LATEST_CHATS, MAX_THREADS, newestInThreads, perRange, PREVIEW_CHARS, resetInboxReads, SCAN_FIELDS, THREAD_CHATS, UNREAD_SCAN } from '../src/superTabs/read/inbox.js'
import { SEED_SCAN } from '../src/superTabs/read/threadMarkSeeds.js'
import { resetRangeCache } from '../src/superTabs/read/rangeCache.js'
import { stableId, type Author, type Conversation, type SuperTabState } from '../src/superTabs/model.js'
import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'
import { NOW, outsider as outsiderActor, owner as ownerActor, peer as peerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'
import { PROFILE_STAGES, profileEntry, type ProfileEntry } from './bench/profile.js'
import { filterOf, profiled } from './plans.test-support.js'
import { listNamedIndexes } from '../src/superTabs/parts/indexed.js'
import type { Prisma } from '@prisma/client'

// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.
let replica: MongoMemoryReplSet
let db: PrismaClient
let app: FastifyInstance
let store: SuperTabStore
const user = (actor: { userId: string; email: string }): AuthUser => ({ sub: actor.userId, email: actor.email, autonomousEnv: 'prod', role: 'user' })
const [owner, peer, outsider] = [ownerActor, peerActor, outsiderActor].map(user) as [AuthUser, AuthUser, AuthUser]
const ownerTeam: AuthUser = { ...owner, harnessSessionId: 'owner-team-session', harnessSessionKind: 'team' }
const signedIn = (authorization: string | undefined) =>
  authorization === 'Bearer team' ? ownerTeam : [owner, peer, outsider].find(u => `Bearer ${u.sub}` === authorization)
const MIGRATED = NOW - 60_000

beforeAll(async () => {
  replica = await MongoMemoryReplSet.create({ binary: { version: '7.0.24', downloadDir: join(tmpdir(), 'harness-super-tab-mongodb-binaries') },
    replSet: { count: 1, ip: '127.0.0.1', storageEngine: 'wiredTiger' } })
  const uri = replica.getUri(`swarm_inbox_test_${randomUUID().replaceAll('-', '_')}`)
  await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {
    env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,
  })
  db = new PrismaClient({ datasources: { db: { url: uri } } })
  for (const each of [owner, peer, outsider]) await db.user.create({ data: { id: each.sub, externalId: `fixture-${each.sub}`, email: each.email, autonomousEnv: 'prod' } })
  // The owner's own machine runs the Swarm's worker, so the owner may write as it.
  await db.machine.create({ data: { machineId: 'host', userId: owner.sub, apiKey: 'fixture-only', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })
  store = new SuperTabStore(db)
  app = Fastify()
  app.setErrorHandler(errorHandler)
  // External SSO is replaced with fixture accounts; every permission and write below is real.
  app.addHook('onRequest', async (request, reply) => {
    const found = signedIn(request.headers.authorization)
    if (!found) return reply.code(401).send({ success: false })
    request.user = found
  })
  await app.register(superTabRoutes, { store, github: null, reads: new SwarmReads(store), hosts: new HostReads(store) })
  await app.ready()
})
afterAll(async () => {
  await app?.close()
  await db?.$disconnect()
  await replica?.stop()
})

/** A Swarm in parts: a public project ("Web", with a worker on the owner's machine), a project private to its owner
 *  ("Secret work"), peer's Buddy chat, and peer's closed Chat with PM from before Buddies (nobody reads it). */
async function scene(extra: (s: ReturnType<typeof swarm>) => void = () => {}) {
  const s = swarm(MIGRATED)
  s.join(peerActor)
  s.join(outsiderActor)
  const secret = s.run({ type: 'project.create', name: 'Secret', visibility: 'private', brief: '' }).projectId as string
  const web = s.group('Web')
  const worker = s.add('worker-harness', web, false, ownerActor, 'host', owner.sub)
  s.run({ type: 'work.assign', conversationId: web, agentId: worker })
  const secretGroup = s.group('Secret work', secret)
  const closedId = s.group('Peer and PM')
  let buddyId = s.group('Peer\'s Buddy')
  s.edit(state => {
    const generation = state.people.find(p => p.id === peer.sub)!.generation
    state.conversations.find(c => c.id === closedId)!.audience = { people: [{ id: peer.sub, generation }] }
    const chat = state.conversations.find(c => c.id === buddyId)!
    buddyId = buddyChatId(chat.projectId ?? 'lead', { id: peer.sub, generation })
    Object.assign(chat, { id: buddyId, kind: 'buddy', leadId: null, agentIds: [], audience: { people: [{ id: peer.sub, generation }] } })
  })
  extra(s)
  // Stored before its removal: the Swarm-wide Leads conversation, which no inbox lists now (model.ts leadsRemoved).
  s.edit(state => { storeOldLeads(state) })
  const tabId = `tab-${randomUUID()}`
  const state = { ...structuredClone(s.state), id: tabId } as SuperTabState
  await writeLegacySwarm(db, { state, revision: 3 })
  const report = await migrateSwarm(db, tabId, { now: () => MIGRATED })
  if (!report.ok) throw new Error(report.error)
  return { tabId, web, worker, secretGroup, closedId, buddyId, leads: state.leadsConversationId! }
}

const get = async (url: string, as: AuthUser | 'team' = owner) => {
  const response = await app.inject({ method: 'GET', url, headers: { authorization: `Bearer ${as === 'team' ? 'team' : as.sub}` } })
  return { status: response.statusCode, body: response.json() as { data: any; error?: { code: string; message: string } } }
}
const inbox = (tabId: string, as: AuthUser | 'team' = owner) => get(`/api/super-tabs/${tabId}/inbox`, as)

/** Sends a message as a person (or as an agent the owner runs) through the real command, and answers its stored row. */
async function send(tabId: string, conversationId: string, text: string, opts: { as?: AuthUser; agentId?: string; replyTo?: string } = {}) {
  await store.execute(opts.as ?? owner, tabId, { operationId: randomUUID(), command: { type: 'message.send', conversationId, text,
    replyTo: opts.replyTo ?? null, recipientIds: [] } }, opts.agentId)
  return (await db.superTabMessage.findFirst({ where: { tabId, conversationId, text }, orderBy: { sequence: 'desc' } }))!
}
const agentAuthor = (id: string): Author => ({ kind: 'agent', id, name: id, membershipGeneration: 'fixture' })
const personAuthor = (u: AuthUser): Author => ({ kind: 'person', id: u.sub, name: u.email, membershipGeneration: 'fixture' })

/** Rows no command writes (a Buddy's reply, a Lead's tag, legacy notices), stored as the store stores them: numbered after the
 *  chat's newest, the chat's counters moved as numberMessages moves them for rows people read. */
async function insert(tabId: string, conversationId: string, rows: Array<{ id?: string; text: string; author: Author; thread?: string | null;
  source?: Record<string, unknown>; recipientIds?: string[]; mentions?: string[] }>) {
  const ids: string[] = []
  // One createMany, not a create per row: 5,001 creates outlasted Prisma's 5-second transaction limit on a CI runner
  // ("Transaction not found", task #2459), so the test's outcome depended on the machine's speed.
  await swarmTransaction(db, async tx => {
    const current = (await parts.loadActive(tx, tabId, { groupIds: [conversationId] })).groups.find(g => g.key === conversationId)!
    const data = structuredClone(current.data!) as Conversation
    const created: Prisma.SuperTabMessageCreateManyInput[] = []
    for (const row of rows) {
      const sequence = (data.messageSeq ?? 0) + 1
      const id = row.id ?? `m-${randomUUID()}`
      ids.push(id)
      data.messageSeq = sequence
      const hidden = !!row.source?.recipientsOnly
      if (!hidden) {
        data.lastMessageSequence = sequence
        data.lastActivityAt = new Date().toISOString()
        if (!row.thread || row.thread === id) data.lastMainSequence = sequence
      }
      created.push({ id, tabId, conversationId, sequence, author: row.author as never, text: row.text,
        thread: row.thread === undefined ? id : row.thread, recipientIds: row.recipientIds ?? [], mentions: row.mentions ?? [],
        ...(row.source ? { source: row.source as never } : {}), createdAt: new Date() })
    }
    await tx.superTabMessage.createMany({ data: created })
    await guardedWrite(tx, tabId, { op: 'update', kind: 'group', key: conversationId, seen: current.version, data }, Date.now())
  })
  return ids
}
/** Changes one chat's stored data as a command would. */
async function editChat(tabId: string, conversationId: string, edit: (data: Conversation) => void) {
  await swarmTransaction(db, async tx => {
    const current = (await parts.loadActive(tx, tabId, { groupIds: [conversationId] })).groups.find(g => g.key === conversationId)!
    const data = structuredClone(current.data!) as Conversation
    edit(data)
    await guardedWrite(tx, tabId, { op: 'update', kind: 'group', key: conversationId, seen: current.version, data }, Date.now())
  })
}
const sequenceOf = async (id: string) => (await db.superTabMessage.findUniqueOrThrow({ where: { id } })).sequence
const markRead = async (tabId: string, conversationId: string, sequence: number, as: AuthUser) => {
  const response = await app.inject({ method: 'POST', url: `/api/super-tabs/${tabId}/conversations/${conversationId}/read`,
    headers: { authorization: `Bearer ${as.sub}` }, payload: { sequence } })
  expect(response.statusCode).toBe(200)
}
const threadsOf = (data: any) => (data.threads as any[]).map(t => ({ chat: t.conversationId, root: t.root.text, unread: t.unreadReplies,
  last: t.lastReplier?.id ?? null }))
const latestOf = (data: any) => Object.fromEntries((data.latest as any[]).map(l => [l.conversationId, l.message.text]))

/** Every chat gets a thread and a newer top-level message. Each reader marks each chat they read as read at its thread's root,
 *  before the replies: a chat never marked read counts as read.
 *  Web (public): owner's root tagging peer and outsider, two owner replies and one worker reply, then "Web latest".
 *  Secret work (owner's private project): owner's root, a worker reply, then "Secret latest".
 *  Peer's Buddy chat: peer's root, the Buddy's reply, then the Buddy's "Buddy latest".
 *  Peer's closed Chat with PM: a root, a reply and "Closed latest", read by nobody.
 *  The removed Leads conversation (still stored): the owner's "Leads note", which nobody's inbox shows. */
async function busyScene() {
  const s = await scene()
  const [webRootId] = await insert(s.tabId, s.web, [{ text: 'Web root', author: personAuthor(owner), mentions: [peer.sub, outsider.sub] }])
  const [secretRoot] = await insert(s.tabId, s.secretGroup, [{ text: 'Secret root', author: personAuthor(owner) }])
  const [buddyRoot] = await insert(s.tabId, s.buddyId, [{ text: 'Buddy root', author: personAuthor(peer) }])
  const [closedRoot] = await insert(s.tabId, s.closedId, [{ text: 'Closed root', author: personAuthor(peer) }])
  const webRoot = await db.superTabMessage.findUniqueOrThrow({ where: { id: webRootId! } })
  for (const each of [owner, peer, outsider]) await markRead(s.tabId, s.web, webRoot.sequence, each)
  await markRead(s.tabId, s.secretGroup, await sequenceOf(secretRoot!), owner)
  await markRead(s.tabId, s.buddyId, await sequenceOf(buddyRoot!), peer)
  await send(s.tabId, s.web, 'Web reply 1', { replyTo: webRoot.id })
  await send(s.tabId, s.web, 'Web reply 2', { replyTo: webRoot.id })
  await send(s.tabId, s.web, 'Web worker reply', { replyTo: webRoot.id, agentId: s.worker })
  const webLatest = await send(s.tabId, s.web, 'Web latest')
  await insert(s.tabId, s.secretGroup, [{ text: 'Secret reply', author: agentAuthor(s.worker), thread: secretRoot }, { text: 'Secret latest', author: personAuthor(owner) }])
  await insert(s.tabId, s.buddyId, [{ text: 'Buddy reply', author: agentAuthor('buddy-agent'), thread: buddyRoot }, { text: 'Buddy latest', author: agentAuthor('buddy-agent') }])
  await insert(s.tabId, s.closedId, [{ text: 'Closed reply', author: agentAuthor(s.worker), thread: closedRoot }, { text: 'Closed latest', author: personAuthor(peer) }])
  await insert(s.tabId, s.leads, [{ text: 'Leads note', author: personAuthor(owner) }])
  return { ...s, webRoot, webLatest }
}

beforeEach(() => { resetInboxReads(); resetRangeCache() })

describe('a person\'s inbox: unread threads and each chat\'s latest message', () => {
  it('gives a collaborator exactly the chats the messages read gives them: their own Buddy chat, never the private project or the closed chat', async () => {
    const s = await busyScene()
    const { status, body } = await inbox(s.tabId, peer)
    expect(status).toBe(200)
    expect(threadsOf(body.data)).toEqual([
      { chat: s.buddyId, root: 'Buddy root', unread: 1, last: 'buddy-agent' },
      { chat: s.web, root: 'Web root', unread: 3, last: s.worker },
    ])
    expect(latestOf(body.data)).toEqual({ [s.web]: 'Web latest', [s.buddyId]: 'Buddy latest' })
    expect(body.data.more).toBe(false)
    expect(JSON.stringify(body.data)).not.toMatch(/Secret|Closed|secret|closed/)
    expect(JSON.stringify(body.data)).not.toContain(s.secretGroup)
    expect(JSON.stringify(body.data)).not.toContain(s.closedId)
  })

  it('gives the owner the private project, never a collaborator\'s Buddy chat or closed chat, and never counts their own replies', async () => {
    const s = await busyScene()
    const { status, body } = await inbox(s.tabId, owner)
    expect(status).toBe(200)
    // In Web the owner wrote the root and two replies: only the worker's reply, after their own newest, is unread for them.
    expect(threadsOf(body.data)).toEqual([
      { chat: s.secretGroup, root: 'Secret root', unread: 1, last: s.worker },
      { chat: s.web, root: 'Web root', unread: 1, last: s.worker },
    ])
    expect(latestOf(body.data)).toEqual({ [s.web]: 'Web latest', [s.secretGroup]: 'Secret latest' })
    expect(JSON.stringify(body.data)).not.toMatch(/Buddy|Closed/)
    expect(JSON.stringify(body.data)).not.toContain(s.buddyId)
  })

  it('gives someone outside the private project and the Buddy chat only the public chats', async () => {
    const s = await busyScene()
    const { status, body } = await inbox(s.tabId, outsider)
    expect(status).toBe(200)
    expect(threadsOf(body.data)).toEqual([{ chat: s.web, root: 'Web root', unread: 3, last: s.worker }])
    expect(latestOf(body.data)).toEqual({ [s.web]: 'Web latest' })
    expect(JSON.stringify(body.data)).not.toMatch(/Secret|Buddy|Closed/)
  })

  it('answers the root, the last reply time and the latest message as the messages read shows them', async () => {
    const s = await busyScene()
    const { body } = await inbox(s.tabId, outsider)
    const page = (await get(`/api/super-tabs/${s.tabId}/conversations/${s.web}/messages`, outsider)).body.data.messages as any[]
    const shown = (text: string) => page.find(m => m.text === text)
    const thread = body.data.threads[0]
    expect(thread).toEqual({ conversationId: s.web, rootId: s.webRoot.id,
      root: { id: shown('Web root').id, sequence: shown('Web root').sequence, author: shown('Web root').author, text: 'Web root',
        createdAt: shown('Web root').createdAt, attachments: 0 },
      lastReplyAt: shown('Web worker reply').createdAt, lastReplySequence: shown('Web worker reply').sequence,
      lastReplier: shown('Web worker reply').author, unreadReplies: 3 })
    expect(body.data.latest.find((l: any) => l.conversationId === s.web)).toEqual({ conversationId: s.web,
      message: { id: shown('Web latest').id, sequence: shown('Web latest').sequence, author: shown('Web latest').author, text: 'Web latest',
        createdAt: shown('Web latest').createdAt, attachments: 0 } })
  })

  it('counts replies only in a thread until the person reads the thread, never by reading the chat, and drops a thread they have read', async () => {
    const s = await busyScene()
    const reply1 = await db.superTabMessage.findFirstOrThrow({ where: { tabId: s.tabId, text: 'Web reply 1' } })
    const markThread = async (sequence: number) => expect((await app.inject({ method: 'POST', url: `/api/super-tabs/${s.tabId}/conversations/${s.web}/threads/${s.webRoot.id}/read`,
      headers: { authorization: `Bearer ${peer.sub}` }, payload: { sequence } })).statusCode).toBe(200)
    // The three replies are only in the thread: reading the chat past them reads none (Diego, 11-01).
    await markRead(s.tabId, s.web, s.webLatest.sequence, peer)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data).filter(t => t.chat === s.web)).toEqual([{ chat: s.web, root: 'Web root', unread: 3, last: s.worker }])
    await markThread(reply1.sequence)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data).filter(t => t.chat === s.web)).toEqual([{ chat: s.web, root: 'Web root', unread: 2, last: s.worker }])
    await markThread(s.webLatest.sequence)
    const after = (await inbox(s.tabId, peer)).body.data
    expect(threadsOf(after).filter(t => t.chat === s.web)).toEqual([])
    // The latest message stays: the Buddies list shows it whether read or not.
    expect(latestOf(after)[s.web]).toBe('Web latest')
  })

  it('never counts or names a row people cannot read, and never lists a thread whose root they cannot read', async () => {
    const s = await busyScene()
    // An agent-only notice in the Web thread (a reply only its recipient reads), and an agent-only root with a reply under it
    // that tags the outsider, so only the hidden root keeps that thread out.
    await insert(s.tabId, s.web, [{ text: 'Hidden reply', author: agentAuthor('hidden-agent'), thread: s.webRoot.id, source: { kind: 'note', recipientsOnly: true },
      recipientIds: [s.worker] }])
    const [hiddenRoot] = await insert(s.tabId, s.web, [{ text: 'Hidden root', author: agentAuthor('hidden-agent'), source: { kind: 'note', recipientsOnly: true },
      recipientIds: [s.worker] }])
    await insert(s.tabId, s.web, [{ text: 'Reply under a hidden root', author: agentAuthor(s.worker), thread: hiddenRoot, mentions: [outsider.sub] }])
    const { body } = await inbox(s.tabId, outsider)
    expect(threadsOf(body.data)).toEqual([{ chat: s.web, root: 'Web root', unread: 3, last: s.worker }])
    expect(JSON.stringify(body.data)).not.toMatch(/Hidden|hidden-agent/)
  })

  it('shows as latest the message a page ends with when the newest row is a task chat copy of a Lead notice nobody reads', async () => {
    const s = await busyScene()
    // A lost-task notice stored before such copies were marked (pageReads.ts deliveredCopies), whose Lead copy reached the Lead.
    const [copy] = await insert(s.tabId, s.web, [{ text: 'Lost task notice', author: agentAuthor('swarm'), source: { kind: 'work-lost' } }])
    await insert(s.tabId, s.leads, [{ id: stableId(copy!, 'lead'), text: 'Lead copy', author: agentAuthor('swarm'), source: { kind: 'work-lost' },
      recipientIds: ['lead-agent'] }])
    const page = (await get(`/api/super-tabs/${s.tabId}/conversations/${s.web}/messages`, outsider)).body.data.messages as any[]
    expect(page.map(m => m.text)).not.toContain('Lost task notice')
    expect(latestOf((await inbox(s.tabId, outsider)).body.data)[s.web]).toBe('Web latest')
  })

  it('never names a last replier who can no longer read the chat', async () => {
    const s = await busyScene()
    // Outsider replies in Web, then leaves the Swarm: peer still sees the reply counted, but not who wrote it.
    await send(s.tabId, s.web, 'Outsider reply', { as: outsider, replyTo: s.webRoot.id })
    await store.execute(owner, s.tabId, { operationId: randomUUID(), command: { type: 'member.remove', userId: outsider.sub } })
    const thread = (await inbox(s.tabId, peer)).body.data.threads.find((t: any) => t.conversationId === s.web)
    expect(thread.unreadReplies).toBe(4)
    // As thread summaries name them: the newest author who can still read the chat.
    expect(thread.lastReplier.id).toBe(s.worker)
    expect(JSON.stringify(thread)).not.toContain(outsider.sub)
  })

  it('refuses a removed member, a Team session and an agent alike, with nothing about the Swarm\'s chats', async () => {
    const s = await busyScene()
    await store.execute(owner, s.tabId, { operationId: randomUUID(), command: { type: 'member.remove', userId: peer.sub } })
    const removed = await inbox(s.tabId, peer)
    // Refused as the messages read refuses them, not answered as a member with nothing to read.
    const page = await get(`/api/super-tabs/${s.tabId}/conversations/${s.web}/messages`, peer)
    // As for a Swarm that does not exist (routes/superTabs.ts NOT_AVAILABLE).
    expect(page.status).toBe(404)
    expect({ status: removed.status, code: removed.body.error?.code }).toEqual({ status: page.status, code: page.body.error?.code })
    expect(removed.body.data).toBeUndefined()
    expect(JSON.stringify(removed.body)).not.toMatch(/Web|Buddy|Leads/)
    const team = await inbox(s.tabId, 'team')
    expect(team.status).toBe(403)
    expect(JSON.stringify(team.body)).not.toMatch(/Web|Secret|Leads/)
    // An agent has no read marks: the read takes no agent at all.
    expect((await get(`/api/super-tabs/${s.tabId}/inbox?asAgentId=${s.worker}`, owner)).status).toBe(400)
    // Another Swarm's id reads as one that does not exist.
    expect((await inbox(`tab-${randomUUID()}`, owner)).status).toBe(404)
  })

  it('does not list a thread whose only replies are the person\'s own', async () => {
    const s = await busyScene()
    const ownRoot = await send(s.tabId, s.web, 'Own root')
    await send(s.tabId, s.web, 'Own reply', { replyTo: ownRoot.id })
    const { body } = await inbox(s.tabId, owner)
    expect(threadsOf(body.data)).toEqual([
      { chat: s.secretGroup, root: 'Secret root', unread: 1, last: s.worker },
      { chat: s.web, root: 'Web root', unread: 1, last: s.worker },
    ])
    expect(JSON.stringify(body.data.threads)).not.toMatch(/Own root|Own reply/)
  })

  it('shows a top-level message as latest, never a thread reply, when the newest row is a copy nobody reads', async () => {
    const s = await scene()
    const root = await send(s.tabId, s.web, 'Thread root')
    await send(s.tabId, s.web, 'Thread reply', { replyTo: root.id })
    const [copy] = await insert(s.tabId, s.web, [{ text: 'Lost task notice', author: agentAuthor('swarm'), source: { kind: 'work-lost' } }])
    await insert(s.tabId, s.leads, [{ id: stableId(copy!, 'lead'), text: 'Lead copy', author: agentAuthor('swarm'), source: { kind: 'work-lost' },
      recipientIds: ['lead-agent'] }])
    expect(latestOf((await inbox(s.tabId, outsider)).body.data)[s.web]).toBe('Thread root')
  })
})

describe('thread unread as Desktop marks it (controller.dart threadUnread)', () => {
  it('counts a chat the person never marked read as read', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Tagged root', author: personAuthor(owner), mentions: [peer.sub] }])
    await insert(s.tabId, s.web, [{ text: 'Worker reply', author: agentAuthor(s.worker), thread: root }])
    const { body } = await inbox(s.tabId, peer)
    expect(body.data.threads).toEqual([])
    // Once they read it, the next reply counts.
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Tagged root', unread: 1, last: s.worker }])
  })

  it('lists only threads the person started, replied in or was tagged in, before or after their mark', async () => {
    const s = await scene()
    const [elsewhere, started, repliedBefore, taggedBefore, repliedAfter, taggedAfter] = await insert(s.tabId, s.web, [
      { text: 'Not theirs', author: personAuthor(owner) },
      { text: 'Started', author: personAuthor(peer) },
      { text: 'Replied before', author: personAuthor(owner) },
      { text: 'Tagged before', author: personAuthor(owner) },
      { text: 'Replied after', author: personAuthor(owner) },
      { text: 'Tagged after', author: personAuthor(owner) },
    ])
    await insert(s.tabId, s.web, [{ text: 'Peer reply', author: personAuthor(peer), thread: repliedBefore },
      { text: 'Lead tags peer', author: agentAuthor(s.worker), thread: taggedBefore, mentions: [peer.sub] }])
    await markRead(s.tabId, s.web, (await db.superTabMessage.findFirstOrThrow({ where: { tabId: s.tabId, conversationId: s.web }, orderBy: { sequence: 'desc' } })).sequence, peer)
    await insert(s.tabId, s.web, [{ text: 'Peer reply after', author: personAuthor(peer), thread: repliedAfter },
      { text: 'Lead tags peer after', author: agentAuthor(s.worker), thread: taggedAfter, mentions: [peer.sub] }])
    await insert(s.tabId, s.web, [elsewhere, started, repliedBefore, taggedBefore, repliedAfter, taggedAfter]
      .map(thread => ({ text: 'Worker reply', author: agentAuthor(s.worker), thread })))
    const roots = threadsOf((await inbox(s.tabId, peer)).body.data).map(t => `${t.root}: ${t.unread}`).sort()
    // After their own reply only the worker's counts; after a tag both the tag and the worker's reply do.
    expect(roots).toEqual(['Replied after: 1', 'Replied before: 1', 'Started: 1', 'Tagged after: 2', 'Tagged before: 1'])
  })

  it('counts only others\' replies after the person\'s own newest reply, and skips a thread whose newest reply is theirs', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    await insert(s.tabId, s.web, [{ text: 'Worker 1', author: agentAuthor(s.worker), thread: root }, { text: 'Worker 2', author: agentAuthor(s.worker), thread: root },
      { text: 'Peer answers', author: personAuthor(peer), thread: root }])
    // Their reply is the newest: nothing in it waits for them, though their mark is older than the worker's replies.
    expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
    await insert(s.tabId, s.web, [{ text: 'Worker 3', author: agentAuthor(s.worker), thread: root }])
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
  })

  it('counts only replies past the person\'s mark of the thread, read on any device, and drops a thread read to its end', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    const [first] = await insert(s.tabId, s.web, [{ text: 'Worker 1', author: agentAuthor(s.worker), thread: root }])
    await insert(s.tabId, s.web, [{ text: 'Worker 2', author: agentAuthor(s.worker), thread: root }])
    const markThread = async (sequence: number, as: AuthUser) => {
      const response = await app.inject({ method: 'POST', url: `/api/super-tabs/${s.tabId}/conversations/${s.web}/threads/${root}/read`,
        headers: { authorization: `Bearer ${as.sub}` }, payload: { sequence } })
      expect(response.statusCode).toBe(200)
    }
    await markThread(await sequenceOf(first!), peer)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
    // Another person's thread mark never reads it for them.
    await markThread(10_000, owner)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
    await markThread(10_000, peer)
    expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
    await insert(s.tabId, s.web, [{ text: 'Worker 3', author: agentAuthor(s.worker), thread: root }])
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
  })

  it('leaves archived chats out of the latest messages and the threads', async () => {
    const s = await busyScene()
    await editChat(s.tabId, s.web, data => { data.archived = true })
    const { body } = await inbox(s.tabId, peer)
    expect(threadsOf(body.data)).toEqual([{ chat: s.buddyId, root: 'Buddy root', unread: 1, last: 'buddy-agent' }])
    expect(latestOf(body.data)).toEqual({ [s.buddyId]: 'Buddy latest' })
  })

  it('gives an older chat whose newest rows are all thread replies its latest top-level message', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Older root', author: personAuthor(owner) }])
    await insert(s.tabId, s.web, Array.from({ length: 20 }, (_, i) => ({ text: `Reply ${i}`, author: agentAuthor(s.worker), thread: root })))
    // Written before the server kept the main stream's newest number.
    await editChat(s.tabId, s.web, data => { delete data.lastMainSequence })
    const { body } = await inbox(s.tabId, peer)
    expect(latestOf(body.data)[s.web]).toBe('Older root')
    expect(body.data.more).toBe(false)
  })
})

describe('reading a chat never reads a reply only in a thread (Diego, 11-01)', () => {
  const threadsRead = (tabId: string, as: AuthUser | 'team' = owner) => get(`/api/super-tabs/${tabId}/threads`, as)
  const post = async (url: string, as: AuthUser | 'team', payload: unknown) => {
    const response = await app.inject({ method: 'POST', url, headers: { authorization: `Bearer ${as === 'team' ? 'team' : as.sub}` }, payload: payload as never })
    return { status: response.statusCode, body: response.json() as { data: any; error?: { code: string } } }
  }
  const markThread = async (tabId: string, conversationId: string, root: string, sequence: number, as: AuthUser) =>
    expect((await post(`/api/super-tabs/${tabId}/conversations/${conversationId}/threads/${root}/read`, as, { sequence })).status).toBe(200)
  const follow = async (tabId: string, conversationId: string, root: string, value: boolean, as: AuthUser) =>
    expect((await post(`/api/super-tabs/${tabId}/conversations/${conversationId}/threads/${root}/follow`, as, { follow: value })).status).toBe(200)
  const newest = async (tabId: string, conversationId: string) =>
    (await db.superTabMessage.findFirstOrThrow({ where: { tabId, conversationId }, orderBy: { sequence: 'desc' } })).sequence
  /** The person's thread mark rows, by root id. */
  const marksOf = async (tabId: string, as: AuthUser) => Object.fromEntries((await db.superTabThreadMark.findMany({ where: { tabId, personId: as.sub } }))
    .map(r => [r.threadId, { sequence: r.sequence, follow: r.follow ?? null }]))
  const unreadRoots = (data: any) => threadsOf(data).map(t => `${t.root}: ${t.unread}`).sort()

  it('keeps a thread-only reply unread in the inbox and in Threads after the person reads the chat past it', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    await insert(s.tabId, s.web, [{ text: 'Thread-only reply', author: agentAuthor(s.worker), thread: root }])
    const [main] = await insert(s.tabId, s.web, [{ text: 'Main message', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await sequenceOf(main!), peer)
    expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
    const listed = (await threadsRead(s.tabId, peer)).body.data
    expect(listed.unread.map((t: any) => t.root.text)).toEqual(['Peer root'])
    expect(listed.read).toEqual([])
    // Read in the thread, it leaves the inbox and Threads lists it as read.
    await markThread(s.tabId, s.web, root!, await newest(s.tabId, s.web), peer)
    expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
    const after = (await threadsRead(s.tabId, peer)).body.data
    expect(after.unread).toEqual([])
    expect(after.read.map((t: any) => t.root.text)).toEqual(['Peer root'])
  })

  it('keeps a thread-only reply after SEED_SCAN hidden notices unread once the chat is read', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer question', author: personAuthor(peer) }])
    const old = await sequenceOf(root!)
    await markRead(s.tabId, s.web, old, peer)
    await insert(s.tabId, s.web, [
      ...Array.from({ length: SEED_SCAN }, (_, i) => ({ text: `Hidden notice ${i}`, author: agentAuthor(s.worker),
        source: { kind: 'note', recipientsOnly: true }, recipientIds: [s.worker] })),
      { text: 'Answer', author: agentAuthor(s.worker), thread: root },
      { text: 'Main', author: personAuthor(owner) },
    ])
    const top = await newest(s.tabId, s.web)
    await markRead(s.tabId, s.web, top, peer)
    expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: old, follow: null } })
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Peer question: 1'])
    expect((await threadsRead(s.tabId, peer)).body.data.unread.map((t: any) => t.root.text)).toEqual(['Peer question'])
  })

  it('holds a capped chat read at the last scanned reply and seeds the later thread on the next read', async () => {
    const s = await scene()
    const [early, late] = await insert(s.tabId, s.web, [{ text: 'Early', author: personAuthor(peer) }, { text: 'Late', author: personAuthor(peer) }])
    const old = await sequenceOf(late!)
    await markRead(s.tabId, s.web, old, peer)
    const replies = await insert(s.tabId, s.web, Array.from({ length: SEED_SCAN }, (_, i) => ({ text: `Own reply ${i}`, author: personAuthor(peer), thread: early })))
    const boundary = await sequenceOf(replies.at(-1)!)
    await insert(s.tabId, s.web, [{ text: 'Late answer', author: agentAuthor(s.worker), thread: late }, { text: 'Main', author: personAuthor(owner) }])
    const top = await newest(s.tabId, s.web)
    const first = await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, peer, { sequence: top })
    expect(first.status).toBe(200)
    expect(first.body.data.sequence).toBe(boundary)
    expect((await db.superTabReadMark.findUniqueOrThrow({ where: { id: stableId(s.tabId, s.web, peer.sub) } })).sequence).toBe(boundary)
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Late: 1'])
    await markRead(s.tabId, s.web, top, peer)
    expect(await marksOf(s.tabId, peer)).toEqual({ [late!]: { sequence: boundary, follow: null } })
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Late: 1'])
  })

  it('holds the mark before an unhandled thread when more than 50 threads have answers', async () => {
    const s = await scene()
    const roots = await insert(s.tabId, s.web, Array.from({ length: 51 }, (_, i) => ({ text: `Question ${i}`, author: personAuthor(peer) })))
    const old = await sequenceOf(roots.at(-1)!)
    await markRead(s.tabId, s.web, old, peer)
    const replies = await insert(s.tabId, s.web, roots.map(thread => ({ text: 'Answer', author: agentAuthor(s.worker), thread })))
    await insert(s.tabId, s.web, [{ text: 'Main', author: personAuthor(owner) }])
    const top = await newest(s.tabId, s.web)
    const first = await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, peer, { sequence: top })
    expect(first.body.data.sequence).toBe(await sequenceOf(replies[49]!))
    expect(Object.keys(await marksOf(s.tabId, peer))).toEqual(expect.arrayContaining(roots.slice(0, 50)))
    expect((await marksOf(s.tabId, peer))[roots[50]!]).toBeUndefined()
    await markRead(s.tabId, s.web, top, peer)
    expect(Object.keys(await marksOf(s.tabId, peer))).toHaveLength(51)
    expect((await marksOf(s.tabId, peer))[roots[0]!]).toEqual({ sequence: old, follow: null })
  })

  it('advances through a row ceiling containing no replies and leaves another chat unchanged', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Question', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    const main = await insert(s.tabId, s.web, Array.from({ length: 5_001 }, (_, i) => ({ text: `Main ${i}`, author: personAuthor(owner) })))
    const top = await newest(s.tabId, s.web)
    const first = await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, peer, { sequence: top })
    expect(first.body.data.sequence).toBe(await sequenceOf(main[4_999]!))
    await markRead(s.tabId, s.web, top, peer)
    expect((await db.superTabReadMark.findUniqueOrThrow({ where: { id: stableId(s.tabId, s.web, peer.sub) } })).sequence).toBe(top)
    expect(await db.superTabReadMark.count({ where: { tabId: s.tabId, personId: peer.sub, conversationId: { not: s.web } } })).toBe(0)
  })

  it('does not spend the thread-mark budget on threads the person is not in', async () => {
    const s = await scene()
    const roots = await insert(s.tabId, s.web, Array.from({ length: 51 }, (_, i) => ({ text: `Question ${i}`, author: personAuthor(i === 0 ? peer : owner) })))
    const old = await sequenceOf(roots.at(-1)!)
    await markRead(s.tabId, s.web, old, peer)
    await insert(s.tabId, s.web, roots.map(thread => ({ text: 'Answer', author: agentAuthor(s.worker), thread })))
    await insert(s.tabId, s.web, [{ text: 'Main', author: personAuthor(owner) }])
    const top = await newest(s.tabId, s.web)
    const response = await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, peer, { sequence: top })
    expect(response.body.data.sequence).toBe(top)
    expect(await marksOf(s.tabId, peer)).toEqual({ [roots[0]!]: { sequence: old, follow: null } })
  })

  it('does so for each thread the person started, replied in, was tagged in or follows, and for no other', async () => {
    const s = await scene()
    const [started, replied, taggedRoot, taggedReply, followed, notTheirs] = await insert(s.tabId, s.web, [
      { text: 'Started', author: personAuthor(peer) },
      { text: 'Replied', author: personAuthor(owner) },
      { text: 'Tagged root', author: personAuthor(owner), mentions: [peer.sub] },
      { text: 'Tagged reply', author: personAuthor(owner) },
      { text: 'Followed', author: personAuthor(owner) },
      { text: 'Not theirs', author: personAuthor(owner) },
    ])
    await insert(s.tabId, s.web, [{ text: 'Peer answers', author: personAuthor(peer), thread: replied }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    await follow(s.tabId, s.web, followed!, true, peer)
    await insert(s.tabId, s.web, [...[started, replied, taggedRoot, followed, notTheirs].map(thread => ({ text: 'Worker reply', author: agentAuthor(s.worker), thread })),
      { text: 'Worker tags peer', author: agentAuthor(s.worker), thread: taggedReply, mentions: [peer.sub] }])
    await insert(s.tabId, s.web, [{ text: 'Main message', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Followed: 1', 'Replied: 1', 'Started: 1', 'Tagged reply: 1', 'Tagged root: 1'])
    expect((await threadsRead(s.tabId, peer)).body.data.unread.map((t: any) => t.root.text).sort())
      .toEqual(['Followed', 'Replied', 'Started', 'Tagged reply', 'Tagged root'])
    expect(Object.keys(await marksOf(s.tabId, peer))).not.toContain(notTheirs)
  })

  it('leaves a thread the person already opened at its own mark, and writes none on a thread they unfollowed', async () => {
    const s = await scene()
    const [root, unfollowed] = await insert(s.tabId, s.web, [{ text: 'Opened', author: personAuthor(peer) }, { text: 'Unfollowed', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(unfollowed!), peer)
    await follow(s.tabId, s.web, unfollowed!, false, peer)
    const [first] = await insert(s.tabId, s.web, [{ text: 'Reply 1', author: agentAuthor(s.worker), thread: root }])
    await markThread(s.tabId, s.web, root!, await sequenceOf(first!), peer)
    await insert(s.tabId, s.web, [{ text: 'Reply 2', author: agentAuthor(s.worker), thread: root }, { text: 'Main 1', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    // The chat's mark is now past the thread's own: a later read of the chat must not raise the thread's to it.
    await insert(s.tabId, s.web, [{ text: 'Reply 3', author: agentAuthor(s.worker), thread: root },
      { text: 'Unfollowed reply', author: agentAuthor(s.worker), thread: unfollowed }, { text: 'Main 2', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: await sequenceOf(first!), follow: null }, [unfollowed!]: { sequence: 0, follow: false } })
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Opened: 2'])
  })

  it('gives a Follow alone the chat\'s old mark', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Followed', author: personAuthor(owner) }])
    const old = await sequenceOf(root!)
    await markRead(s.tabId, s.web, old, peer)
    await follow(s.tabId, s.web, root!, true, peer)
    await insert(s.tabId, s.web, [{ text: 'Reply', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: old, follow: true } })
  })

  it('marks nothing in a chat the person never read: its threads wait for their first visit', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await insert(s.tabId, s.web, [{ text: 'Reply', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    expect(await marksOf(s.tabId, peer)).toEqual({})
    expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
  })

  it('writes no mark for a root or reply the person cannot read, and none through a chat they cannot read', async () => {
    const s = await scene()
    const [own] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(own!), peer)
    const [hiddenRoot] = await insert(s.tabId, s.web, [{ text: 'Hidden root', author: agentAuthor('hidden-agent'), source: { kind: 'note', recipientsOnly: true },
      recipientIds: [s.worker], mentions: [peer.sub] }])
    await insert(s.tabId, s.web, [{ text: 'Reply under a hidden root', author: agentAuthor(s.worker), thread: hiddenRoot, mentions: [peer.sub] },
      { text: 'Hidden reply', author: agentAuthor('hidden-agent'), thread: own, source: { kind: 'note', recipientsOnly: true }, recipientIds: [s.worker] },
      { text: 'Main', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
    expect(await marksOf(s.tabId, peer)).toEqual({})
    expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
    const first = await get(`/api/super-tabs/${s.tabId}`, peer)
    expect(JSON.stringify(first.body.data?.derived ?? {})).not.toContain(hiddenRoot!)
    // The owner's private project and peer's Buddy chat: refused alike to the person who cannot read them, writing nothing.
    const [secretRoot] = await insert(s.tabId, s.secretGroup, [{ text: 'Secret root', author: personAuthor(owner), mentions: [peer.sub] }])
    await insert(s.tabId, s.secretGroup, [{ text: 'Secret reply', author: agentAuthor(s.worker), thread: secretRoot, mentions: [peer.sub] }])
    const [buddyRoot] = await insert(s.tabId, s.buddyId, [{ text: 'Buddy root', author: personAuthor(peer), mentions: [owner.sub] }])
    await insert(s.tabId, s.buddyId, [{ text: 'Buddy reply', author: agentAuthor('buddy-agent'), thread: buddyRoot, mentions: [owner.sub] }])
    const refusals = [await post(`/api/super-tabs/${s.tabId}/conversations/${s.secretGroup}/read`, peer, { sequence: 10_000 }),
      await post(`/api/super-tabs/${s.tabId}/conversations/${s.buddyId}/read`, owner, { sequence: 10_000 }),
      await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, 'team', { sequence: 10_000 })]
    expect(refusals.map(r => r.status)).toEqual([404, 404, 403])
    expect(JSON.stringify(refusals)).not.toMatch(/Secret|Buddy/)
    expect(await db.superTabThreadMark.count({ where: { tabId: s.tabId, threadId: { in: [secretRoot!, buddyRoot!, hiddenRoot!] } } })).toBe(0)
  })

  it('keeps the higher thread mark when the chat is read on two devices while the thread is opened on a third', async () => {
    const s = await scene()
    const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    const [reply1] = await insert(s.tabId, s.web, [{ text: 'Reply 1', author: agentAuthor(s.worker), thread: root }])
    await insert(s.tabId, s.web, [{ text: 'Reply 2', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
    const top = await newest(s.tabId, s.web)
    await Promise.all([markRead(s.tabId, s.web, top, peer), markRead(s.tabId, s.web, top, peer), markThread(s.tabId, s.web, root!, await sequenceOf(reply1!), peer)])
    expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: await sequenceOf(reply1!), follow: null } })
    expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Peer root: 1'])
  })

  it(`reads one bounded row range even when main rows vastly outnumber replies, whatever the range's length`, async () => {
    const s = await scene()
    const [early, late] = await insert(s.tabId, s.web, [{ text: 'Early', author: personAuthor(peer) }, { text: 'Late', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(late!), peer)
    await insert(s.tabId, s.web, [{ text: 'Early reply', author: agentAuthor(s.worker), thread: early },
      ...Array.from({ length: 5_100 }, (_, i) => ({ text: `Main ${i}`, author: personAuthor(owner) })),
      { text: 'Late reply', author: agentAuthor(s.worker), thread: late }])
    const top = await newest(s.tabId, s.web)
    await db.$runCommandRaw({ profile: 0 })
    await db.$runCommandRaw({ drop: 'system.profile' }).catch(() => {})
    await db.$runCommandRaw({ create: 'system.profile', capped: true, size: 16 * 1024 * 1024 })
    await db.$runCommandRaw({ profile: 2, slowms: 0 })
    try { await markRead(s.tabId, s.web, top, peer) } finally { await db.$runCommandRaw({ profile: 0 }) }
    const rows = (await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: [{ $match: { ns: { $not: { $regex: 'system\\.' } } } }, ...PROFILE_STAGES],
      cursor: {} }) as unknown as { cursor: { firstBatch: ProfileEntry[] } }).cursor.firstBatch.map(profileEntry)
    expect(rows.filter(r => r.planSummary === 'COLLSCAN')).toEqual([])
    const messages = rows.filter(r => r.ns.endsWith('.SuperTabMessage') && r.op !== 'getmore')
    const examined = messages.reduce((n, r) => n + (r.docsExamined ?? 0), 0)
    console.info(`mark-read seed cost: ${messages.length} message reads, ${examined} message docs examined, ${rows.filter(r => r.op !== 'getmore').length} operations\n` +
      rows.filter(r => r.op !== 'getmore').map(r => `  ${r.ns.split('.').at(-1)} ${r.op} ${r.planSummary ?? ''} docs=${r.docsExamined ?? 0} keys=${r.keysExamined ?? 0}`).join('\n'))
    // The range read, the thread's root and the look-back for whether the person is in it: never one read per row or thread.
    expect(messages.length).toBeLessThanOrEqual(3)
    expect(examined).toBeLessThanOrEqual(5_010)
    const stored = await db.superTabReadMark.findUniqueOrThrow({ where: { id: stableId(s.tabId, s.web, peer.sub) } })
    expect(stored.sequence).toBeLessThan(top)
    // The row ceiling holds the mark before the late reply; a later read can seed it.
    expect(await marksOf(s.tabId, peer)).toEqual({ [early!]: { sequence: await sequenceOf(late!), follow: null } })
  }, 60_000)
})

describe('the inbox\'s caps', () => {
  it('cuts preview text to PREVIEW_CHARS characters in the database, counting a surrogate pair as one', async () => {
    const s = await scene()
    const long = '😀'.repeat(PREVIEW_CHARS + 50)
    const [root] = await insert(s.tabId, s.web, [{ text: long, author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
    await insert(s.tabId, s.web, [{ text: 'Reply', author: agentAuthor(s.worker), thread: root }])
    const { body } = await inbox(s.tabId, peer)
    expect(body.data.threads[0].root.text).toBe('😀'.repeat(PREVIEW_CHARS))
    expect(body.data.latest.find((l: any) => l.conversationId === s.web).message.text).toBe('😀'.repeat(PREVIEW_CHARS))
  })

  it(`answers the newest ${MAX_THREADS} threads and says there are more`, async () => {
    const s = await scene(f => { for (let i = 0; i < 3; i++) f.group(`Busy ${i}`) })
    const chats = (await store.readLoad(peer, s.tabId)).state.conversations.filter(c => c.title.startsWith('Busy ')).map(c => c.id)
    for (const chat of chats) {
      const roots = await insert(s.tabId, chat, Array.from({ length: 40 }, (_, i) => ({ text: `Root ${i}`, author: personAuthor(peer) })))
      await markRead(s.tabId, chat, await sequenceOf(roots.at(-1)!), peer)
      await insert(s.tabId, chat, roots.map(thread => ({ text: 'Reply', author: agentAuthor(s.worker), thread })))
    }
    const { body } = await inbox(s.tabId, peer)
    expect(body.data.threads).toHaveLength(MAX_THREADS)
    expect(body.data.more).toBe(true)
    // Newest reply first: the last chat written holds the newest.
    expect(new Set(body.data.threads.slice(0, 40).map((t: any) => t.conversationId))).toEqual(new Set([chats[2]]))
  })

  it(`answers the latest message of the ${LATEST_CHATS} most recently active chats and says there are more`, async () => {
    const s = await scene(f => { for (let i = 0; i < LATEST_CHATS; i++) f.group(`Chat ${i}`) })
    const chats = (await store.readLoad(peer, s.tabId)).state.conversations.filter(c => c.title.startsWith('Chat ')).map(c => c.id)
    // Web and Leads get a message first, so they are the oldest active of LATEST_CHATS + 2 chats with a latest message.
    await insert(s.tabId, s.web, [{ text: 'Older in Web', author: personAuthor(owner) }])
    await insert(s.tabId, s.leads, [{ text: 'Older in Leads', author: personAuthor(owner) }])
    for (const chat of chats) await insert(s.tabId, chat, [{ text: `Latest in ${chat}`, author: personAuthor(owner) }])
    const { body } = await inbox(s.tabId, peer)
    expect(body.data.latest).toHaveLength(LATEST_CHATS)
    expect(new Set(body.data.latest.map((l: any) => l.conversationId))).toEqual(new Set(chats))
    expect(body.data.more).toBe(true)
  }, 60_000)

  it(`allows a person ${INBOX_READS} reads of one Swarm's inbox per window, then answers 429 with Retry-After`, async () => {
    const s = await busyScene()
    vi.useFakeTimers({ toFake: ['Date'] })
    try {
      vi.setSystemTime(Math.ceil(Date.now() / INBOX_WINDOW_MS) * INBOX_WINDOW_MS + 1_000)
      for (let i = 0; i < INBOX_READS; i++) expect((await inbox(s.tabId, peer)).status).toBe(200)
      const refused = await app.inject({ method: 'GET', url: `/api/super-tabs/${s.tabId}/inbox`, headers: { authorization: `Bearer ${peer.sub}` } })
      expect(refused.statusCode).toBe(429)
      expect(refused.headers['retry-after']).toBe('9')
      expect(refused.json().error.code).toBe('RATE_LIMITED')
      expect(refused.body).not.toMatch(/Web|Buddy|Leads/)
      // Counted per person and per Swarm.
      expect((await inbox(s.tabId, owner)).status).toBe(200)
      expect((await inbox((await busyScene()).tabId, peer)).status).toBe(200)
      vi.setSystemTime(Date.now() + INBOX_WINDOW_MS)
      expect((await inbox(s.tabId, peer)).status).toBe(200)
    } finally { vi.useRealTimers() }
  })
})

describe('what the inbox costs the database', () => {
  const CHATS = 35
  /** A person in CHATS active public chats, then one long chat of 4000 rows, the most recently active. Each of the CHATS has a
   *  thread they were tagged in, with 5 replies past their mark, then a top-level message. The long chat has 10 threads they
   *  are not in, each with 300 replies before their mark and 100 after. */
  async function bigScene() {
    const s = await scene(f => { for (let i = 0; i < CHATS; i++) f.group(`Chat ${i}`) })
    const state = (await store.readLoad(peer, s.tabId)).state
    const chats = state.conversations.filter(c => c.title.startsWith('Chat ')).map(c => c.id)
    for (const chat of chats) {
      const [root] = await insert(s.tabId, chat, [{ text: `Root in ${chat}`, author: personAuthor(owner), mentions: [peer.sub] }])
      await markRead(s.tabId, chat, await sequenceOf(root!), peer)
      await insert(s.tabId, chat, [...Array.from({ length: 5 }, (_, i) => ({ text: `Reply ${i}`, author: agentAuthor(s.worker), thread: root })),
        { text: `Latest in ${chat}`, author: personAuthor(owner) }])
    }
    const roots = await insert(s.tabId, s.web, Array.from({ length: 10 }, (_, i) => ({ text: `Long root ${i}`, author: personAuthor(owner) })))
    for (let batch = 0; batch < 3; batch++) {
      await insert(s.tabId, s.web, Array.from({ length: 1000 }, (_, i) => ({ text: `Long reply ${batch}.${i}`, author: agentAuthor(s.worker), thread: roots[i % roots.length]! })))
    }
    await markRead(s.tabId, s.web, (await db.superTabMessage.findFirstOrThrow({ where: { tabId: s.tabId, conversationId: s.web }, orderBy: { sequence: 'desc' } })).sequence, peer)
    await insert(s.tabId, s.web, Array.from({ length: 1000 }, (_, i) => ({ text: `Later reply ${i}`, author: agentAuthor(s.worker), thread: roots[i % roots.length]! })))
    return { ...s, chats }
  }
  const sent = async () => {
    const out = ((await db.$runCommandRaw({ serverStatus: 1 })) as { network: { bytesOut: unknown } }).network.bytesOut
    return typeof out === 'number' ? out : Number((out as { $numberLong?: string; $numberInt?: string }).$numberLong ?? (out as { $numberInt?: string }).$numberInt)
  }

  it(`reads by index only and examines a bounded number of messages for a person with ${CHATS}+ active chats`, async () => {
    const s = await bigScene()
    expect(await db.superTabMessage.count({ where: { tabId: s.tabId } })).toBeGreaterThan(4200)
    await inbox(s.tabId, peer)
    await db.$runCommandRaw({ profile: 0 })
    await db.$runCommandRaw({ drop: 'system.profile' }).catch(() => {})
    await db.$runCommandRaw({ create: 'system.profile', capped: true, size: 16 * 1024 * 1024 })
    await db.$runCommandRaw({ profile: 2, slowms: 0 })
    let reply: Awaited<ReturnType<typeof inbox>>
    try {
      reply = await inbox(s.tabId, peer)
    } finally { await db.$runCommandRaw({ profile: 0 }) }
    expect(reply.status).toBe(200)
    // What MongoDB sent for one more call, unprofiled, less what reading the counter itself costs (as superTabRead.test.ts).
    const first = await sent(), own = await sent() - first
    const start = await sent()
    await inbox(s.tabId, peer)
    const bytesFromDatabase = Math.max(0, await sent() - start - own)
    const rows = (await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: [{ $match: { ns: { $not: { $regex: 'system\\.' } } } }, ...PROFILE_STAGES],
      cursor: {} }) as unknown as { cursor: { firstBatch: ProfileEntry[] } }).cursor.firstBatch.map(profileEntry)
    const messages = rows.filter(r => r.ns.endsWith('.SuperTabMessage'))
    expect(rows.filter(r => r.planSummary === 'COLLSCAN')).toEqual([])
    for (const r of messages) expect(r.planSummary, JSON.stringify(r)).toMatch(/^IXSCAN/)
    // A read's later batches (getmore) belong to that read.
    const reads = rows.filter(r => r.op !== 'getmore')
    const messageReads = messages.filter(r => r.op !== 'getmore')
    const docsExamined = rows.reduce((n, r) => n + (r.docsExamined ?? 0), 0)
    const messageDocs = messages.reduce((n, r) => n + (r.docsExamined ?? 0), 0)
    const replyBytes = JSON.stringify(reply.body.data).length
    console.info(`inbox cost: ${CHATS + 4} active chats, ${reads.length} database reads (${messageReads.length} of messages), ` +
      `${docsExamined} docs examined (${messageDocs} messages), ${bytesFromDatabase} bytes from the database, ${replyBytes} reply bytes\n` +
      reads.map(r => `  ${r.ns.split('.').at(-1)} ${r.op} ${r.planSummary ?? ''} docs=${r.docsExamined ?? 0}`).join('\n'))
    // A fixed few reads whatever the chat count: the latest rows, the unread rows, their roots, the look-back for threads
    // whose newer rows do not show the person (the read marks are another collection, and the person's thread marks of the
    // scanned chats one more indexed read). Nothing new to check for delivered copies here, so no read for them. This scene's
    // long threads pass the look-back's cap, so it reads a second time by union (inbox.ts newestInThreads, task #2148).
    expect(messageReads.length).toBeLessThanOrEqual(5)
    expect(reads.length).toBeLessThanOrEqual(8)
    expect(rows.filter(r => r.ns.endsWith('.SuperTabThreadMark') && r.op !== 'getmore')).toHaveLength(1)
    // Bounded by the caps, never by history: the long chat's rows are read only to its newest UNREAD_SCAN + 1, and each of its
    // threads the person is not in only to MEMBER_SCAN rows before the mark (not its 301).
    expect(messageDocs).toBeLessThan(2_000)
    expect(reply.body.data.latest).toHaveLength(CHATS + 1)
    // The THREAD_CHATS most recently active unread chats are read: the long chat (none of its threads are the person's) and
    // THREAD_CHATS - 1 others, each with one thread.
    expect(reply.body.data.threads).toHaveLength(THREAD_CHATS - 1)
    // More unread chats than one read takes, and the long chat had more unread rows than its scan: the reply says so.
    expect(reply.body.data.more).toBe(true)
    expect(replyBytes).toBeLessThan(60_000)
    expect(bytesFromDatabase).toBeLessThan(250_000)
  }, 120_000)
})

// Ken's slow log of 11-04: a getMore of `{tabId, conversationId, sequence $gt}` returning ~419 rows was 32% of the evening's slow
// message ops. It was the unread scan's second batch: its union over the unread chats asked for Mongo's default first batch.
describe('the inbox\'s scan of unread chats', () => {
  it(`reads ${THREAD_CHATS} unread chats of ${UNREAD_SCAN + 1} rows each in one operation, never a getMore`, async () => {
    const s = await scene(f => { for (let i = 0; i < THREAD_CHATS; i++) f.group(`Busy ${i}`) })
    const chats = (await store.readLoad(peer, s.tabId)).state.conversations.filter(c => c.title.startsWith('Busy ')).map(c => c.id)
    for (const chat of chats) {
      const [root] = await insert(s.tabId, chat, [{ text: `Root in ${chat}`, author: personAuthor(owner), mentions: [peer.sub] }])
      await markRead(s.tabId, chat, await sequenceOf(root!), peer)
      await insert(s.tabId, chat, Array.from({ length: UNREAD_SCAN + 5 }, (_, i) => ({ text: `Reply ${i}`, author: agentAuthor(s.worker), thread: root })))
    }
    const entries = await profiled(db, async () => expect((await inbox(s.tabId, peer)).status).toBe(200))
    const messages = entries.filter(e => e.ns.endsWith('.SuperTabMessage'))
    expect(messages.filter(e => e.op === 'getmore'), JSON.stringify(messages.map(e => [e.op, e.nreturned]))).toEqual([])
    const scan = messages.filter(e => e.op !== 'getmore' && JSON.stringify(filterOf(e)).includes('"$gt"'))
    expect(scan.reduce((n, e) => n + (e.nreturned ?? 0), 0)).toBe(THREAD_CHATS * (UNREAD_SCAN + 1))
  }, 120_000)
})

// Ken's slow log of 11-04: the `$or` over threads' ranges (66% of the peak's slow message ops) and the chats' unread scans were read
// again on every inbox read, which apps make whenever the Swarm moves, though most of the ranges had not changed. A range is kept
// (read/rangeCache.ts) under its chat's newest sequence and reactionSeq, which every new row, edit and delete moves.
describe('re-reading the inbox', () => {
  /** Peer in two chats. Web: peer's root, read in the thread at the first of two worker replies, then the chat read past a main
   *  message, so the thread's mark is behind the chat's. Other: owner's root tagging peer, read at the root, then two worker
   *  replies. */
  async function rereadScene() {
    const s = await scene(f => { f.group('Other') })
    const other = (await store.readLoad(peer, s.tabId)).state.conversations.find(c => c.title === 'Other')!.id
    const [webRoot] = await insert(s.tabId, s.web, [{ text: 'Web root', author: personAuthor(peer) }])
    await markRead(s.tabId, s.web, await sequenceOf(webRoot!), peer)
    const [first] = await insert(s.tabId, s.web, [{ text: 'Web 1', author: agentAuthor(s.worker), thread: webRoot }])
    await insert(s.tabId, s.web, [{ text: 'Web 2', author: agentAuthor(s.worker), thread: webRoot }])
    const response = await app.inject({ method: 'POST', url: `/api/super-tabs/${s.tabId}/conversations/${s.web}/threads/${webRoot}/read`,
      headers: { authorization: `Bearer ${peer.sub}` }, payload: { sequence: await sequenceOf(first!) } })
    expect(response.statusCode).toBe(200)
    const [main] = await insert(s.tabId, s.web, [{ text: 'Web main', author: personAuthor(owner) }])
    await markRead(s.tabId, s.web, await sequenceOf(main!), peer)
    const [otherRoot] = await insert(s.tabId, other, [{ text: 'Other root', author: personAuthor(owner), mentions: [peer.sub] }])
    await markRead(s.tabId, other, await sequenceOf(otherRoot!), peer)
    await insert(s.tabId, other, [1, 2].map(i => ({ text: `Other ${i}`, author: agentAuthor(s.worker), thread: otherRoot })))
    return { ...s, other, webRoot: webRoot!, otherRoot: otherRoot! }
  }
  /** The message reads that read a range of rows (by sequence), and what they cost. */
  const ranges = (entries: ProfileEntry[]) => entries.filter(e => e.ns.endsWith('.SuperTabMessage') && JSON.stringify(filterOf(e) ?? {}).includes('"sequence"'))
  const cost = (entries: ProfileEntry[]) => {
    const messages = entries.filter(e => e.ns.endsWith('.SuperTabMessage'))
    return { reads: messages.length, ranges: ranges(entries).length, docsExamined: messages.reduce((n, e) => n + (e.docsExamined ?? 0), 0),
      returned: messages.reduce((n, e) => n + (e.nreturned ?? 0), 0), bytes: messages.reduce((n, e) => n + (e.responseLength ?? 0), 0) }
  }
  const read = async (tabId: string) => {
    let body: any
    const entries = await profiled(db, async () => { const reply = await inbox(tabId, peer); expect(reply.status).toBe(200); body = reply.body.data })
    return { body, entries }
  }

  it('reads no range again when nothing in it changed, and answers the same', async () => {
    const s = await rereadScene()
    const cold = await read(s.tabId)
    expect(threadsOf(cold.body).sort((a, b) => a.root < b.root ? -1 : 1)).toEqual([
      { chat: s.other, root: 'Other root', unread: 2, last: s.worker }, { chat: s.web, root: 'Web root', unread: 1, last: s.worker }])
    const warm = await read(s.tabId)
    console.info(`inbox message reads, cold: ${JSON.stringify(cost(cold.entries))}; re-read unchanged: ${JSON.stringify(cost(warm.entries))}`)
    expect(ranges(cold.entries).length).toBeGreaterThan(0)
    expect(ranges(warm.entries), JSON.stringify(ranges(warm.entries).map(filterOf))).toEqual([])
    expect(warm.body).toEqual(cold.body)
  })

  it('reads again only the chat a new reply is in, and counts it', async () => {
    const s = await rereadScene()
    await read(s.tabId)
    await insert(s.tabId, s.other, [{ text: 'Other 3', author: agentAuthor(s.worker), thread: s.otherRoot }])
    const after = await read(s.tabId)
    const named = ranges(after.entries).map(e => JSON.stringify(filterOf(e)))
    expect(named.length).toBeGreaterThan(0)
    for (const filter of named) {
      expect(filter).toContain(s.other)
      expect(filter).not.toContain(s.web)
    }
    expect(threadsOf(after.body).find(t => t.chat === s.other)!.unread).toBe(3)
    expect(threadsOf(after.body).find(t => t.chat === s.web)!.unread).toBe(1)
  })

  it('no longer counts a reply deleted after it was read', async () => {
    const s = await rereadScene()
    const reply = await send(s.tabId, s.web, 'Owner reply', { replyTo: s.webRoot })
    expect(threadsOf((await read(s.tabId)).body).find(t => t.chat === s.web)).toMatchObject({ unread: 2, last: owner.sub })
    await store.execute(owner, s.tabId, { operationId: randomUUID(), command: { type: 'message.delete', conversationId: s.web, messageId: reply.id } })
    expect(await db.superTabMessage.findUnique({ where: { id: reply.id } })).toBeNull()
    expect(threadsOf((await read(s.tabId)).body).find(t => t.chat === s.web)).toMatchObject({ unread: 1, last: s.worker })
  })

  it('never answers one Swarm\'s rows for another\'s same chat numbers', async () => {
    const a = await rereadScene(), b = await rereadScene()
    expect(threadsOf((await read(a.tabId)).body).map(t => t.chat).sort()).toEqual([a.other, a.web].sort())
    expect(threadsOf((await read(b.tabId)).body).map(t => t.chat).sort()).toEqual([b.other, b.web].sort())
  })
})

// Task #2148: Ken's profile showed these reads spending their time racing two plans per range, in a union no hint can reach.
describe('reading many thread ranges at once', () => {
  /** What one pipeline per thread answers, as these reads were written before: the answers the single read must keep. */
  const oneByOne = (tabId: string, ranges: Array<{ conversationId: string; thread: string; sequence: Record<string, number> }>, limit: number, namedBy?: string) =>
    perRange(db, ranges.map(r => [{ $match: { tabId, conversationId: r.conversationId, thread: r.thread, sequence: r.sequence } }, { $sort: { sequence: -1 } }, { $limit: limit },
      ...namedBy ? [{ $match: { $or: [{ 'author.kind': 'person', 'author.id': namedBy }, { mentions: namedBy }] } }] : [],
      { $project: { ...SCAN_FIELDS, conversationId: 1 } }] as Prisma.InputJsonValue[]))
  /** Two chats, four threads: short, longer than any limit here, one nobody names the person in, and one with no row in its range. */
  async function threads() {
    const s = await scene(f => { f.group('Second') })
    const second = (await store.readLoad(peer, s.tabId)).state.conversations.find(c => c.title === 'Second')!.id
    const [shortRoot, longRoot, quietRoot, emptyRoot] = await insert(s.tabId, s.web, [1, 2, 3, 4].map(i => ({ text: `Root ${i}`, author: personAuthor(owner) })))
    const [otherRoot] = await insert(s.tabId, second, [{ text: 'Other root', author: personAuthor(owner) }])
    await insert(s.tabId, s.web, [
      { text: 'Tagged', author: agentAuthor(s.worker), thread: shortRoot, mentions: [peer.sub] },
      { text: 'Theirs', author: personAuthor(peer), thread: longRoot },
      { text: 'Tagged early', author: agentAuthor(s.worker), thread: longRoot, mentions: [peer.sub] },
      ...Array.from({ length: 30 }, (_, i) => ({ text: `Long ${i}`, author: agentAuthor(s.worker), thread: longRoot })),
      { text: 'Tagged late', author: agentAuthor(s.worker), thread: longRoot, mentions: [peer.sub] },
      ...Array.from({ length: 3 }, (_, i) => ({ text: `Quiet ${i}`, author: agentAuthor(s.worker), thread: quietRoot })),
    ])
    await insert(s.tabId, second, [{ text: 'Theirs there', author: personAuthor(peer), thread: otherRoot }, { text: 'Not theirs', author: agentAuthor(s.worker), thread: otherRoot }])
    const top = (await db.superTabMessage.findFirstOrThrow({ where: { tabId: s.tabId, conversationId: s.web }, orderBy: { sequence: 'desc' } })).sequence, first = await sequenceOf(shortRoot!)
    const web = (thread: string, sequence: Record<string, number>) => ({ conversationId: s.web, thread, sequence })
    return { tabId: s.tabId, top, first, emptyRoot: emptyRoot!, ranges: (sequence: Record<string, number>) => [web(longRoot!, sequence), web(shortRoot!, sequence), web(quietRoot!, sequence),
      web(emptyRoot!, sequence), { conversationId: second, thread: otherRoot!, sequence: { $gt: 0 } }] }
  }

  it('answers the rows one pipeline per thread answers, in the same order', async () => {
    const f = await threads()
    for (const sequence of [{ $lte: f.top }, { $gt: f.first }, { $lte: f.first + 10 }]) {
      for (const limit of [1, 5, 10, 100]) {
        expect(await newestInThreads(db, f.tabId, f.ranges(sequence), limit)).toEqual(await oneByOne(f.tabId, f.ranges(sequence), limit))
        expect(await newestInThreads(db, f.tabId, f.ranges(sequence), limit, peer.sub)).toEqual(await oneByOne(f.tabId, f.ranges(sequence), limit, peer.sub))
      }
    }
  }, 60_000)

  it('names a person only within each thread\'s newest rows: an older tag or reply does not count', async () => {
    const f = await threads()
    const named = await newestInThreads(db, f.tabId, f.ranges({ $lte: f.top }), 5, peer.sub)
    const texts = await db.superTabMessage.findMany({ where: { id: { in: named.map(m => m.id) } }, select: { text: true } })
    expect(texts.map(m => m.text).sort()).toEqual(['Tagged', 'Tagged late', 'Theirs there'].sort())
  }, 60_000)

  it('cuts a thread to its limit in a batch under its cap, in the hinted read: a tag past the limit does not count', async () => {
    const f = await threads()
    await listNamedIndexes(db)
    // The tag and the 6 rows after it: 7 rows, limit 5, and with four ranges that hold nothing the batch's cap is 7.
    const tagged = await db.superTabMessage.findFirstOrThrow({ where: { tabId: f.tabId, text: 'Tagged early' } })
    const [long, ...others] = f.ranges({ $gt: f.top })
    const ranges = [{ ...long!, sequence: { $gte: tagged.sequence, $lte: tagged.sequence + 6 } }, ...others.map(r => ({ ...r, sequence: { $gt: 1_000_000 } }))]
    let plain: Awaited<ReturnType<typeof newestInThreads>> = []
    const entries = await profiled(db, async () => { plain = await newestInThreads(db, f.tabId, ranges, 5) })
    const reads = entries.filter(e => e.ns.endsWith('.SuperTabMessage') && e.op !== 'getmore')
    expect(reads).toHaveLength(1)
    expect(reads[0]!.command?.hint).toEqual({ tabId: 1, conversationId: 1, thread: 1, sequence: 1 })
    expect(plain.map(m => m.sequence)).toEqual([6, 5, 4, 3, 2].map(i => tagged.sequence + i))
    expect(plain).toEqual(await oneByOne(f.tabId, ranges, 5))
    expect(await newestInThreads(db, f.tabId, ranges, 5, peer.sub)).toEqual([])
    expect(await oneByOne(f.tabId, ranges, 5, peer.sub)).toEqual([])
    // A wider limit reaches the tag, so what cut it above is the limit.
    expect((await newestInThreads(db, f.tabId, ranges, 7, peer.sub)).map(m => m.id)).toEqual([tagged.id])
  }, 60_000)

  it('answers nothing for no ranges, and a range with no rows is simply absent', async () => {
    const f = await threads()
    expect(await newestInThreads(db, f.tabId, [], 5)).toEqual([])
    const empty = [{ conversationId: f.ranges({ $gt: 0 })[0]!.conversationId, thread: f.emptyRoot, sequence: { $gt: f.top } }]
    expect(await newestInThreads(db, f.tabId, empty, 5)).toEqual([])
  }, 60_000)

  it('reads a batch in one operation hinted to the thread index, with no union to plan', async () => {
    const f = await threads()
    await listNamedIndexes(db)
    // Limit 100 and few rows: the batch stays under its cap.
    const entries = await profiled(db, () => newestInThreads(db, f.tabId, f.ranges({ $gt: f.first }), 100))
    const reads = entries.filter(e => e.ns.endsWith('.SuperTabMessage') && e.op !== 'getmore')
    expect(reads).toHaveLength(1)
    expect(reads[0]!.command?.hint).toEqual({ tabId: 1, conversationId: 1, thread: 1, sequence: 1 })
    // Every range is in its first filter (the profile keeps only that): nothing is left for a union to plan.
    expect((filterOf(reads[0]!) as { $or: unknown[] }).$or).toHaveLength(5)
    expect(reads[0]!.planSummary).toContain('thread: 1')
  }, 60_000)

  it('falls back to each thread\'s own limit when a batch has more rows than its cap, and reads no thread past it there', async () => {
    const f = await threads()
    await listNamedIndexes(db)
    const entries = await profiled(db, () => newestInThreads(db, f.tabId, f.ranges({ $lte: f.top }), 1))
    const reads = entries.filter(e => e.ns.endsWith('.SuperTabMessage') && e.op !== 'getmore')
    expect(reads).toHaveLength(2)
    expect(reads.filter(e => e.command?.hint)).toHaveLength(1)
    expect(filterOf(reads.find(e => !e.command?.hint)!)).not.toHaveProperty('$or')
  }, 60_000)
})

// anh Dee, 11-04 (#2742): "remove these messages", the "<machine> left the Swarm: ..." lines in every project chat. Those already
// stored are read by nobody, and a project chat whose newest row is one does not stay unread. The same for the "<who> changed the
// instructions" lines already stored (Dee and Ken, 11-05, #3165): read by nobody, in every app, whatever its version.
describe.each([
  ['a machine-left line stored before #2742', 'left the Swarm', { text: 'Studio left the Swarm: its host confirmed its cleanup.', author: personAuthor(owner),
    source: { kind: 'machine-left', machine: 'Studio' } }],
  ['an instructions line stored before #3132', 'changed the instructions', { text: 'Lead changed the instructions for owner: Routing: No Haiku.\n\nIn Routing:\nAdded: No Haiku.', author: personAuthor(owner),
    source: { kind: 'instructions', projectId: 'lead', revision: 2 } }],
])('%s', (_name, hidden, left) => {
  /** The project chat as a person's first load serves it, with their read mark there. */
  const served = async (tabId: string, as: AuthUser) => {
    const data = (await get(`/api/super-tabs/${tabId}/parts`, as)).body.data
    const chat = (data.groups as any[]).find(g => g.key === 'lead')!.data
    return { lastMainSequence: chat.lastMainSequence as number, mark: (data.derived.readMarks ?? {}).lead as number | undefined }
  }

  it('is in no page and no preview', async () => {
    const s = await scene()
    await send(s.tabId, 'lead', 'Project latest')
    await insert(s.tabId, 'lead', [left])
    for (const as of [owner, peer]) {
      const page = (await get(`/api/super-tabs/${s.tabId}/conversations/lead/messages`, as)).body.data.messages as any[]
      expect(page.map(m => m.text)).toContain('Project latest')
      expect(JSON.stringify(page)).not.toContain(hidden)
      expect(latestOf((await inbox(s.tabId, as)).body.data).lead).toBe('Project latest')
    }
  })

  it('leaves a person who had read the chat with it read, though their app sends no read', async () => {
    const s = await scene()
    const latest = await send(s.tabId, 'lead', 'Project latest')
    await markRead(s.tabId, 'lead', latest.sequence, peer)
    await insert(s.tabId, 'lead', [left])
    // Desktop and the phone count a chat unread while its newest main row is past their mark, and send no read at or below
    // the mark the server holds: the chat's newest main row is served as the newest one people read.
    expect(await served(s.tabId, peer)).toEqual({ lastMainSequence: latest.sequence, mark: latest.sequence })
    // Asked again, from what this server already learned, the answer is the same.
    expect((await served(s.tabId, peer)).lastMainSequence).toBe(latest.sequence)
  })

  it('serves a poll the same number when the project chat is sent again, as for a reply in a thread', async () => {
    const s = await scene()
    const latest = await send(s.tabId, 'lead', 'Project latest')
    await markRead(s.tabId, 'lead', latest.sequence, peer)
    await insert(s.tabId, 'lead', [left])
    const { cursor } = (await get(`/api/super-tabs/${s.tabId}/parts`, peer)).body.data
    // A reply only in a thread: the chat comes again whole from the change feed, its newest main row still the hidden line.
    await send(s.tabId, 'lead', 'A reply', { replyTo: latest.id })
    const changes = (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, peer)).body.data
    const chat = (changes.groups as any[]).find(g => g.key === 'lead')
    expect(chat?.data.lastMainSequence).toBe(latest.sequence)
  })

  it('serves a row people read after the line as the chat\'s newest', async () => {
    const s = await scene()
    await insert(s.tabId, 'lead', [left])
    const after = await send(s.tabId, 'lead', 'After the line')
    expect((await served(s.tabId, peer)).lastMainSequence).toBe(after.sequence)
  })
})