import { test } from 'node:test' import assert from 'node:assert/strict' import { createApiClient } from '@/shared' /** 真打一遍客户端的请求构造:路径、方法、请求体形状必须与服务端契约一致。 */ test('setHeat 走 PUT /entries/heat 并带上 entryIds + heat', async () => { const captured: { url?: string; method?: string; body?: unknown } = {} const client = createApiClient({ baseUrl: '/api', requestId: () => 'req-fixed', fetchImpl: async (input, init) => { captured.url = String(input) captured.method = init?.method captured.body = init?.body return new Response( JSON.stringify({ ok: true, data: { updated: 3, heat: 2, entryIds: [4, 5, 6] } }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ) }, }) const result = await client.entries.setHeat({ entryIds: [4, 5, 6], heat: 2 }) assert.equal(captured.url, '/api/entries/heat') assert.equal(captured.method, 'PUT') // 写操作由客户端补 requestId(幂等重放),其余字段原样。 assert.equal(captured.body, JSON.stringify({ entryIds: [4, 5, 6], heat: 2, requestId: 'req-fixed' })) assert.deepEqual(result, { updated: 3, heat: 2, entryIds: [4, 5, 6] }) }) test('setHeat 传 null 表示清除批次', async () => { const captured: { body?: unknown } = {} const client = createApiClient({ baseUrl: '/api', requestId: () => 'req-fixed', fetchImpl: async (_input, init) => { captured.body = init?.body return new Response( JSON.stringify({ ok: true, data: { updated: 2, heat: null, entryIds: [1, 2] } }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ) }, }) const result = await client.entries.setHeat({ entryIds: [1, 2], heat: null }) assert.equal(captured.body, JSON.stringify({ entryIds: [1, 2], heat: null, requestId: 'req-fixed' })) assert.equal(result.heat, null) })