Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.6 KB
Raw Blame
// FE-W4CNH-001 to FE-W4CNH-008
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { formatTimestamp, originalCategoryName, planCategoryColorSave } from './CollabNotes.helpers'

// The helper feeds t() with a key and params; echo them so the branches are visible.
const t = (key: string, params?: Record<string, number>) =>
  params ? `${key}:${Object.values(params)[0]}` : key

const NOW = new Date('2026-06-15T12:00:00Z')

beforeAll(() => {
  vi.useFakeTimers()
  vi.setSystemTime(NOW)
})

afterAll(() => {
  vi.useRealTimers()
})

function ago(minutes: number): string {
  return new Date(NOW.getTime() - minutes * 60_000).toISOString().replace('Z', '')
}

describe('formatTimestamp', () => {
  it('FE-W4CNH-001: renders an empty string for a missing timestamp', () => {
    expect(formatTimestamp(null, t, 'en')).toBe('')
    expect(formatTimestamp('', t, 'en')).toBe('')
  })

  it('FE-W4CNH-002: labels the last minute as just now', () => {
    expect(formatTimestamp(ago(0), t, 'en')).toBe('collab.chat.justNow')
  })

  it('FE-W4CNH-003: counts minutes below the hour', () => {
    expect(formatTimestamp(ago(5), t, 'en')).toBe('collab.chat.minutesAgo:5')
    expect(formatTimestamp(ago(59), t, 'en')).toBe('collab.chat.minutesAgo:59')
  })

  it('FE-W4CNH-004: counts hours below the day', () => {
    expect(formatTimestamp(ago(60), t, 'en')).toBe('collab.chat.hoursAgo:1')
    expect(formatTimestamp(ago(60 * 23), t, 'en')).toBe('collab.chat.hoursAgo:23')
  })

  it('FE-W4CNH-005: counts days below a week', () => {
    expect(formatTimestamp(ago(60 * 24), t, 'en')).toBe('collab.notes.daysAgo:1')
    expect(formatTimestamp(ago(60 * 24 * 6), t, 'en')).toBe('collab.notes.daysAgo:6')
  })

  it('FE-W4CNH-006: falls back to a localized short date beyond a week', () => {
    expect(formatTimestamp(ago(60 * 24 * 10), t, 'en-US')).toBe('Jun 5')
  })

  it('FE-W4CNH-007: treats a naive timestamp as UTC and accepts an explicit Z', () => {
    const withZ = new Date(NOW.getTime() - 5 * 60_000).toISOString()
    expect(formatTimestamp(withZ, t, 'en')).toBe('collab.chat.minutesAgo:5')
  })

  it('FE-W4CNH-008: falls back to English labels when the translation is missing', () => {
    const empty = () => ''
    expect(formatTimestamp(ago(0), empty, 'en')).toBe('just now')
    expect(formatTimestamp(ago(5), empty, 'en')).toBe('5m ago')
    expect(formatTimestamp(ago(120), empty, 'en')).toBe('2h ago')
    expect(formatTimestamp(ago(60 * 24 * 2), empty, 'en')).toBe('2d ago')
  })
})

describe('category colour save', () => {
  const notes = [{ id: 1, category: 'Food' }, { id: 2, category: 'Food' }, { id: 3, category: 'Sights' }]

  it('FE-W4CNH-009: a rename and a new colour in one Save recolour the notes under the old name', () => {
    const plan = planCategoryColorSave(notes, { Food: '#ef4444', Sights: '#3b82f6' }, { Dinner: '#10b981', Sights: '#3b82f6' }, { Food: 'Dinner' })
    expect(plan.updates).toEqual([{ noteId: 1, color: '#10b981' }, { noteId: 2, color: '#10b981' }])
    expect(plan.emptyCategories).toEqual({})
  })

  it('FE-W4CNH-010: only a category without notes is kept as an empty one', () => {
    const plan = planCategoryColorSave(notes, { Food: '#ef4444' }, { Food: '#ef4444', Spare: '#6366f1' })
    expect(plan.updates).toEqual([])
    expect(plan.emptyCategories).toEqual({ Spare: '#6366f1' })
  })

  it('FE-W4CNH-011: the original name of a renamed category is found, others map to themselves', () => {
    expect(originalCategoryName('Dinner', { Food: 'Dinner' })).toBe('Food')
    expect(originalCategoryName('Sights', { Food: 'Dinner' })).toBe('Sights')
  })
})