// `getReplyCount` field-fallback tests. Pixelfed servers stamp `reply_count`
// (singular); Mastodon stamps `replies_count` (plural). Halflight's
// `StatusService::getMastodon` aliases both, but every consumer outside
// this helper that reads only one field will silently zero on the other
// server. The helper guarantees a non-null number across both wire
// shapes; this suite locks down the four-cell contract (one set, the
// other set, both set, neither set) plus the null-status guard the
// caller-side fixtures (synthetic placeholders, search results) rely on.

import { getReplyCount } from 'src/utils'
import type { Status } from 'src/lib/api-types'

// `getReplyCount` only reads `reply_count` and `replies_count`; any other
// Status field is irrelevant to the fallback decision. Cast through unknown
// to avoid leaking a partial-Status type into the production module.
function s(reply_count?: number | null, replies_count?: number | null): Status {
  return { reply_count, replies_count } as unknown as Status
}

describe('getReplyCount', () => {
  it('returns reply_count when only Pixelfed shape is present', () => {
    expect(getReplyCount(s(5, undefined))).toBe(5)
  })

  it('returns replies_count when only Mastodon shape is present', () => {
    expect(getReplyCount(s(undefined, 5))).toBe(5)
  })

  it('prefers reply_count when both shapes are present (Pixelfed wins)', () => {
    // Halflight's getMastodon aliases both fields to the same value, but if
    // they ever diverge (e.g. write-time Pixelfed stamp vs. read-time Mastodon
    // alias from a stale row) the singular field is the canonical source.
    expect(getReplyCount(s(7, 11))).toBe(7)
  })

  it('returns 0 when neither shape is present', () => {
    expect(getReplyCount(s(undefined, undefined))).toBe(0)
  })

  it('returns 0 for a null/undefined status (synthetic-placeholder guard)', () => {
    // Search results, url-paste previews, and remote-outbox synthetic rows
    // can call this with a missing status. The `if (!s) return 0` guard
    // keeps every consumer's render path safe.
    expect(getReplyCount(null)).toBe(0)
    expect(getReplyCount(undefined)).toBe(0)
  })

  it('treats explicit 0 as zero (not falling through to the other field)', () => {
    // `reply_count: 0` is a meaningful value — a brand-new post with no
    // replies. The `??` (nullish coalescing) operator ensures 0 wins over
    // an undefined `replies_count`; if the helper used `||` it would fall
    // through and silently zero on Mastodon's side anyway.
    expect(getReplyCount(s(0, 7))).toBe(0)
  })
})
