Files
halobestie-clone/backend/test/routes/client.chat-pricing.routes.test.js
ramadhan sjamsani d33d4419ea Phase 4 Stage 1: backend foundation (additive endpoints + schema)
Schema (idempotent migration):
- payment_sessions.is_free_trial -> is_first_session_discount (data copied)
- payment_sessions.mode TEXT NOT NULL DEFAULT 'chat' CHECK (chat|call)
- chat_sessions.topics TEXT[] for ESP picks (info-only)

New endpoints:
- GET /api/client/onboarding-state (drives verif sheet + S6 paywall gate)
- GET /api/client/chat-pricing (rewrite: chat+call groups + first-session
  discount block, per-customer eligibility)
- GET /api/shared/auth-providers (env-probed; replaces ENABLE_SOCIAL_AUTH
  build flag — frontend cutover lands in stage 2)
- GET /api/client/support-handles (Tanya Admin handles, CC-config-driven)

session_warning WS event fires once at 180s remaining.

app_config seeds (mock pricing tiers, first-session discount, support
handles, payment method order, end-session 2-step toggle).

CC SettingsPage: 3 new sections (first-session discount, pricing tiers
JSON editors, support handles).

15/15 Vitest passing. chat_sessions.is_free_trial also renamed for
consistency (plan only specified payment_sessions; pairing.service.js
read both).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 15:56:28 +08:00

99 lines
3.5 KiB
JavaScript

import { describe, it, expect, beforeAll, beforeEach, afterAll, vi } from 'vitest'
vi.mock('../../src/plugins/websocket.js', () => ({
sendToUser: vi.fn(() => false),
sendToSessionParticipant: vi.fn(() => false),
registerWebSocketPlugin: vi.fn(async () => {}),
registerWebSocketRoute: vi.fn(),
isUserOnlineWs: vi.fn(() => false),
getSessionConnections: vi.fn(() => ({})),
}))
vi.mock('../../src/services/notification.service.js', () => ({
sendPushNotification: vi.fn(async () => true),
registerDeviceToken: vi.fn(async () => {}),
}))
const { buildPublic } = await import('../helpers/server.js')
const { resetDb, resetAppConfig, db } = await import('../helpers/db.js')
const { createCustomer } = await import('../helpers/fixtures.js')
const { customerJwt, authHeader } = await import('../helpers/jwt.js')
describe('GET /api/client/chat/pricing (Phase 4)', () => {
let app
let customer
let token
beforeAll(async () => {
await resetAppConfig()
app = await buildPublic()
})
beforeEach(async () => {
await resetDb()
const phone = `+628${Math.floor(Math.random() * 1e10).toString().padStart(10, '0')}`
customer = await createCustomer({ callName: 'PricingTester', phone })
token = customerJwt(customer.id)
})
afterAll(async () => {
await app?.close()
})
it('returns chat + call tier groups and a discount block; eligibility flips when the customer has a completed session', async () => {
const res = await app.inject({
method: 'GET',
url: '/api/client/chat/pricing',
headers: authHeader(token),
})
expect(res.statusCode).toBe(200)
const body = res.json()
expect(body.success).toBe(true)
const data = body.data
// Two tier groups, both non-empty
expect(Array.isArray(data.chat?.tiers)).toBe(true)
expect(Array.isArray(data.call?.tiers)).toBe(true)
expect(data.chat.tiers.length).toBeGreaterThan(0)
expect(data.call.tiers.length).toBeGreaterThan(0)
// Tier shape (chat 12-min should match the seed config)
const chat12 = data.chat.tiers.find((t) => t.minutes === 12)
expect(chat12).toBeDefined()
expect(chat12.price_idr).toBe(12000)
// Discount block — eligible (phone-verified + no completed sessions)
expect(data.first_session_discount.eligible).toBe(true)
expect(data.first_session_discount.actual_price_idr).toBe(2000)
expect(data.first_session_discount.gimmick_price_idr).toBe(12000)
expect(data.first_session_discount.duration_minutes).toBe(12)
expect(data.first_session_discount.modes).toEqual(['chat'])
// Insert a completed session — eligibility must flip.
const sql = db()
await sql`
INSERT INTO chat_sessions (customer_id, status, duration_minutes, price)
VALUES (${customer.id}, 'completed', 12, 12000)
`
const after = await app.inject({
method: 'GET',
url: '/api/client/chat/pricing',
headers: authHeader(token),
})
expect(after.statusCode).toBe(200)
expect(after.json().data.first_session_discount.eligible).toBe(false)
})
it('eligibility is false when phone is not set (anonymous customer)', async () => {
const anon = await createCustomer({ callName: 'AnonCust', phone: null })
const anonToken = customerJwt(anon.id)
const res = await app.inject({
method: 'GET',
url: '/api/client/chat/pricing',
headers: authHeader(anonToken),
})
expect(res.statusCode).toBe(200)
expect(res.json().data.first_session_discount.eligible).toBe(false)
})
})