File size: 3,471 Bytes
c7d34c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { NewApiCostResolver, matchNewApiCostLog, quotaToUsdEquivalent } from './new-api';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';

const baseInput = {
    model: 'gpt-image-2',
    startedAtMs: 1_778_737_200_000,
    finishedAtMs: 1_778_737_255_000
};

describe('quotaToUsdEquivalent', () => {
    it('converts new-api quota to the default USD-equivalent amount', () => {
        assert.equal(quotaToUsdEquivalent(3750), 0.0075);
        assert.equal(quotaToUsdEquivalent(75000), 0.15);
    });
});

describe('matchNewApiCostLog', () => {
    it('returns a high-confidence actual cost when exactly one log matches', () => {
        const result = matchNewApiCostLog({
            ...baseInput,
            logs: [
                { id: 1, type: 2, model_name: 'gpt-5.5', quota: 10, created_at: 1778737200 },
                { id: 2, type: 2, model_name: 'gpt-image-2', quota: 3750, created_at: 1778737230, request_id: 'req-1' }
            ]
        });

        assert.deepEqual(result, {
            actualAmount: 0.0075,
            actualQuota: 3750,
            currency: 'usd-equivalent',
            source: 'new-api-log-token',
            confidence: 'high',
            upstreamProvider: 'new-api',
            matchedLogId: 2,
            matchedRequestId: 'req-1'
        });
    });

    it('does not hard-match when more than one candidate log is present', () => {
        const result = matchNewApiCostLog({
            ...baseInput,
            logs: [
                { id: 2, type: 2, model_name: 'gpt-image-2', quota: 3750, created_at: 1778737230 },
                { id: 3, type: 2, model_name: 'gpt-image-2', quota: 3750, created_at: 1778737231 }
            ]
        });

        assert.equal(result.source, 'unavailable');
        assert.equal(result.confidence, 'low');
        assert.match(result.reason || '', /2 条候选/);
    });

    it('returns unavailable when no log matches the request window', () => {
        const result = matchNewApiCostLog({
            ...baseInput,
            logs: [{ id: 2, type: 2, model_name: 'gpt-image-2', quota: 3750, created_at: 1778730000 }]
        });

        assert.equal(result.source, 'unavailable');
        assert.equal(result.confidence, 'none');
    });

    it('requires the new-api consume log type', () => {
        const result = matchNewApiCostLog({
            ...baseInput,
            logs: [{ id: 2, model_name: 'gpt-image-2', quota: 3750, created_at: 1778737230 }]
        });

        assert.equal(result.source, 'unavailable');
        assert.equal(result.confidence, 'none');
    });
});

describe('NewApiCostResolver', () => {
    it('returns unavailable when the log endpoint is not present or fails', async () => {
        const originalFetch = globalThis.fetch;
        globalThis.fetch = (async () => new Response('not found', { status: 404 })) as typeof fetch;
        try {
            const result = await new NewApiCostResolver().resolve({
                apiBaseUrl: 'https://example.test/v1',
                apiKey: 'sk-test',
                model: 'gpt-image-2',
                startedAtMs: baseInput.startedAtMs,
                finishedAtMs: baseInput.finishedAtMs,
                expectedImageCount: 1
            });

            assert.equal(result.source, 'unavailable');
            assert.equal(result.confidence, 'none');
        } finally {
            globalThis.fetch = originalFetch;
        }
    });
});