import {
  TrailDetailSchema,
  TrailPostSchema,
  TrailSchema,
} from '@crumbs/core/schemas'
import {describe, expect, test} from 'bun:test'

import {
  optimisticTrail,
  optimisticTrailDetail,
  optimisticTrailPost,
} from '@/lib/optimistic-trail'

const author = {
  id: '22222222-2222-4222-8222-222222222222',
  handle: 'ada',
  avatarUrl: null,
}
const meta = {
  id: '11111111-1111-4111-8111-111111111111',
  author,
  now: '2026-07-03T00:00:00.000Z',
}

describe('optimistic trail', () => {
  // the snapshot layer reads through fromSchema(...); an invalid optimistic
  // value would be silently dropped on the next cold start
  test('optimisticTrail is TrailSchema-valid', () => {
    expect(
      TrailSchema.safeParse(optimisticTrail('Japan', meta)).success,
    ).toBe(true)
  })

  test('optimisticTrailDetail is valid and owned by the creator', () => {
    const detail = optimisticTrailDetail('Japan', meta)
    expect(TrailDetailSchema.safeParse(detail).success).toBe(true)
    expect(detail.viewerRole).toBe('owner')
    expect(detail.crumbs).toEqual([])
    expect(detail.posts).toEqual([])
  })

  test('optimisticTrailPost is valid with empty (unresolved) facets', () => {
    const post = optimisticTrailPost('hi @ada', {
      ...meta,
      id: '33333333-3333-4333-8333-333333333333',
    })
    expect(TrailPostSchema.safeParse(post).success).toBe(true)
    expect(post.facets).toEqual([])
    expect(post.author.handle).toBe('ada')
  })
})
