Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

6.4 KB
Raw Blame
import { beforeEach, expect, it, vi } from 'vitest'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { nativePushDismissal } from './native-push-dismissal'
import {
  rememberPushDismissal,
  rememberPushDismissals,
  wasPushDismissed
} from './push-dismissal-watermarks'
import { foregroundNotificationBehavior } from './push-receive'
import { presentForegroundPush } from './android-foreground-push'
import { dismissPresentedPushNotification } from './push-tray-dismissal'
import { loadNotificationDeliveryPreferences } from './notification-delivery-preferences'

const memory = vi.hoisted(() => new Map<string, string>())
const nativeLedger = vi.hoisted(() => new Map<string, number>())
vi.mock('./native-push-dismissal', () => ({
  nativePushDismissal: {
    rememberAll: vi.fn(async (payloads) => {
      for (const payload of payloads) {
        const key = JSON.stringify([
          payload.hostFingerprint,
          payload.notificationEpoch,
          payload.notificationId
        ])
        nativeLedger.set(key, Math.max(nativeLedger.get(key) ?? 0, payload.notificationSeq))
      }
    }),
    wasDismissed: vi.fn(
      async (payload) =>
        (nativeLedger.get(
          JSON.stringify([
            payload.hostFingerprint,
            payload.notificationEpoch,
            payload.notificationId
          ])
        ) ?? -1) >= payload.notificationSeq
    )
  }
}))
vi.mock('@react-native-async-storage/async-storage', () => ({
  default: {
    getItem: vi.fn(async (key: string) => memory.get(key) ?? null),
    setItem: vi.fn(async (key: string, value: string) => {
      memory.set(key, value)
    })
  }
}))
const schedule = vi.hoisted(() => vi.fn(async () => 'socket-alert'))
vi.mock('expo-notifications', () => ({
  getPresentedNotificationsAsync: async () => [],
  scheduleNotificationAsync: schedule
}))
vi.mock('react-native', () => ({ Platform: { OS: 'android' } }))
vi.mock('../transport/host-store', () => ({ loadHostCatalog: async () => [{ id: 'host' }] }))
vi.mock('./push-host-fingerprint', () => ({ resolveHostIdForFingerprint: () => 'host' }))
vi.mock('../storage/preferences', () => ({
  loadPushNotificationsEnabled: async () => true
}))
vi.mock('./notification-viewing-policy', () => ({
  shouldSuppressNotificationWhileViewing: () => false
}))
vi.mock('./notification-delivery-preferences', () => ({
  loadNotificationDeliveryPreferences: vi.fn(async () => ({ sound: true }))
}))

const payload = {
  hostFingerprint: 'abcdefghijklmnop',
  notificationEpoch: 'epoch',
  notificationId: 'note',
  notificationSeq: 20
}
const fence = { ...payload, notificationSeq: 21 }

beforeEach(() => {
  vi.clearAllMocks()
  vi.mocked(AsyncStorage.getItem).mockImplementation(async (key) => memory.get(key) ?? null)
  memory.clear()
  nativeLedger.clear()
})

it('uses only native storage for iOS dismissal reads and writes', async () => {
  await rememberPushDismissal(fence)
  expect(await wasPushDismissed(payload)).toBe(true)
  expect(await wasPushDismissed({ ...payload, notificationSeq: 22 })).toBe(false)
  expect(await wasPushDismissed({ ...payload, notificationEpoch: 'new-epoch' })).toBe(false)
  expect(AsyncStorage.getItem).not.toHaveBeenCalled()
  expect(AsyncStorage.setItem).not.toHaveBeenCalled()
})

it('keeps a batch in the native ledger and joins it before checking dismissal', async () => {
  const other = { ...fence, notificationId: 'second' }
  const pending = rememberPushDismissals([fence, other])
  const secondDismissed = wasPushDismissed(other)
  await pending
  expect(nativePushDismissal!.rememberAll).toHaveBeenCalledTimes(1)
  expect(await secondDismissed).toBe(true)
  expect(await wasPushDismissed({ ...other, notificationSeq: 22 })).toBe(false)
  expect(AsyncStorage.getItem).not.toHaveBeenCalled()
  expect(AsyncStorage.setItem).not.toHaveBeenCalled()
})

it('rechecks a negative native snapshot overtaken by a dismissal', async () => {
  let finish!: () => void
  vi.mocked(nativePushDismissal!.wasDismissed).mockImplementationOnce(async () => {
    await new Promise<void>((resolve) => {
      finish = resolve
    })
    return false
  })
  const pending = wasPushDismissed(payload)
  await vi.waitFor(() => expect(finish).toBeDefined())
  await rememberPushDismissal(fence)
  finish()
  expect(await pending).toBe(true)
  expect(nativePushDismissal!.wasDismissed).toHaveBeenCalledTimes(2)
})

it('surfaces native write failures without switching storage or poisoning later operations', async () => {
  vi.mocked(nativePushDismissal!.rememberAll).mockRejectedValueOnce(new Error('native failure'))
  await expect(rememberPushDismissal(fence)).rejects.toThrow('native failure')
  expect(AsyncStorage.setItem).not.toHaveBeenCalled()
  await rememberPushDismissal(fence)
  expect(await wasPushDismissed(payload)).toBe(true)
})

it('suppresses presentation when dismissal completes during the handler sound read', async () => {
  let finish!: () => void
  vi.mocked(loadNotificationDeliveryPreferences).mockImplementationOnce(async () => {
    await new Promise<void>((resolve) => {
      finish = resolve
    })
    return { sound: true } as Awaited<ReturnType<typeof loadNotificationDeliveryPreferences>>
  })
  const foregroundAlert = {
    identifier: 'foreground-alert',
    trigger: null,
    content: { title: null, subtitle: null, body: null, sound: null, data: { orca: payload } }
  }
  const pending = foregroundNotificationBehavior({ request: foregroundAlert })
  await vi.waitFor(() => expect(finish).toBeDefined())
  await foregroundNotificationBehavior({
    request: { content: { data: { orca: { ...fence, kind: 'dismiss' } } } }
  })
  expect(await wasPushDismissed(payload)).toBe(true)
  finish()
  expect(await pending).toEqual({
    shouldShowBanner: false,
    shouldShowList: false,
    shouldPlaySound: false,
    shouldSetBadge: false
  })
})

it('does not schedule an alert dismissed while final fallback preparation is pending', async () => {
  let finish: (allowed: boolean) => void = () => {}
  const prepare = vi.fn(
    () =>
      new Promise<boolean>((resolve) => {
        finish = resolve
      })
  )
  const pending = presentForegroundPush(
    'socket-alert',
    { title: 'Needs input', body: 'Reply', data: payload, sound: 'default' },
    prepare
  )
  await vi.waitFor(() => expect(prepare).toHaveBeenCalledOnce())
  await dismissPresentedPushNotification(payload.notificationId, payload.hostFingerprint, fence)
  expect(await wasPushDismissed(payload)).toBe(true)
  finish(true)
  await pending
  expect(schedule).not.toHaveBeenCalled()
})