@tuturuuu/utils 0.0.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +305 -0
- package/biome.json +5 -0
- package/jsr.json +8 -8
- package/package.json +63 -32
- package/src/__tests__/ai-temp-auth.test.ts +309 -0
- package/src/__tests__/api-proxy-guard.test.ts +1451 -0
- package/src/__tests__/app-url.test.ts +270 -0
- package/src/__tests__/avatar-url.test.ts +97 -0
- package/src/__tests__/color-helper.test.ts +179 -0
- package/src/__tests__/constants.test.ts +351 -0
- package/src/__tests__/crypto.test.ts +107 -0
- package/src/__tests__/date-helper.test.ts +408 -0
- package/src/__tests__/fixtures/task-description-full-featured.json +456 -0
- package/src/__tests__/format.test.ts +317 -0
- package/src/__tests__/html-sanitizer.test.ts +360 -0
- package/src/__tests__/interest-calculator.test.ts +336 -0
- package/src/__tests__/interest-detector.test.ts +222 -0
- package/src/__tests__/label-colors.test.ts +241 -0
- package/src/__tests__/name-helper.test.ts +158 -0
- package/src/__tests__/node-diff.test.ts +576 -0
- package/src/__tests__/notification-service.test.ts +210 -0
- package/src/__tests__/onboarding-helper.test.ts +331 -0
- package/src/__tests__/path-helper.test.ts +152 -0
- package/src/__tests__/permissions.test.tsx +81 -0
- package/src/__tests__/request-emoji-limit.test.ts +172 -0
- package/src/__tests__/search-helper.test.ts +51 -0
- package/src/__tests__/storage-display-name.test.ts +37 -0
- package/src/__tests__/storage-path.test.ts +238 -0
- package/src/__tests__/tag-utils.test.ts +205 -0
- package/src/__tests__/task-description-yjs-state.test.ts +581 -0
- package/src/__tests__/task-helper-board-api-routing.test.ts +94 -0
- package/src/__tests__/task-helper-create-task.test.ts +129 -0
- package/src/__tests__/task-helpers.test.ts +464 -0
- package/src/__tests__/task-overrides.test.ts +305 -0
- package/src/__tests__/task-reorder-cache.test.ts +74 -0
- package/src/__tests__/task-sort-keys.test.ts +36 -0
- package/src/__tests__/task-transformers.test.ts +62 -0
- package/src/__tests__/text-helper.test.ts +776 -0
- package/src/__tests__/time-helper.test.ts +70 -0
- package/src/__tests__/time-tracker-period.test.ts +55 -0
- package/src/__tests__/timezone.test.ts +117 -0
- package/src/__tests__/upstash-rest.test.ts +77 -0
- package/src/__tests__/uuid-helper.test.ts +133 -0
- package/src/__tests__/workspace-helper.test.ts +859 -0
- package/src/__tests__/workspace-limits.test.ts +255 -0
- package/src/__tests__/yjs-helper.test.ts +581 -0
- package/src/abuse-protection/__tests__/backend-rate-limit.test.ts +113 -0
- package/src/abuse-protection/__tests__/edge.test.ts +136 -0
- package/src/abuse-protection/__tests__/index.test.ts +562 -0
- package/src/abuse-protection/__tests__/reputation.test.ts +192 -0
- package/src/abuse-protection/backend-rate-limit.ts +44 -0
- package/src/abuse-protection/constants.ts +117 -0
- package/src/abuse-protection/edge.ts +223 -0
- package/src/abuse-protection/index.ts +1545 -0
- package/src/abuse-protection/reputation.ts +587 -0
- package/src/abuse-protection/types.ts +97 -0
- package/src/abuse-protection/user-agent.ts +124 -0
- package/src/abuse-protection/user-suspension.ts +231 -0
- package/src/ai-temp-auth.ts +315 -0
- package/src/api-proxy-guard.ts +965 -0
- package/src/app-url.ts +96 -0
- package/src/avatar-url.ts +64 -0
- package/src/break-duration.ts +84 -0
- package/src/calendar-auth-token.test.ts +37 -0
- package/src/calendar-auth-token.ts +19 -0
- package/src/calendar-sync-coordination.md +197 -0
- package/src/calendar-utils.test.ts +169 -0
- package/src/calendar-utils.ts +91 -0
- package/src/color-helper.ts +110 -0
- package/src/common/nextjs.tsx +99 -0
- package/src/common/scan.tsx +15 -0
- package/src/configs/reports.ts +160 -0
- package/src/constants.ts +85 -0
- package/src/crypto.ts +21 -0
- package/src/currencies.ts +97 -0
- package/src/date-helper.ts +313 -0
- package/src/editor/convert-to-task.ts +264 -0
- package/src/editor/index.ts +5 -0
- package/src/email/__tests__/client.test.ts +141 -0
- package/src/email/__tests__/validation.test.ts +46 -0
- package/src/email/client.ts +92 -0
- package/src/email/server.ts +128 -0
- package/src/email/validation.ts +11 -0
- package/src/encryption/__tests__/calendar-events.test.ts +411 -0
- package/src/encryption/__tests__/configuration.test.ts +114 -0
- package/src/encryption/__tests__/field-encryption.test.ts +232 -0
- package/src/encryption/__tests__/key-generation.test.ts +30 -0
- package/src/encryption/__tests__/performance-edge-cases.test.ts +187 -0
- package/src/encryption/__tests__/test-helpers.ts +22 -0
- package/src/encryption/__tests__/workspace-key-encryption.test.ts +129 -0
- package/src/encryption/encryption-service.ts +343 -0
- package/src/encryption/index.ts +25 -0
- package/src/encryption/types.ts +57 -0
- package/src/exchange-rates.ts +49 -0
- package/src/feature-flags/__tests__/feature-flags.test.ts +302 -0
- package/src/feature-flags/core.ts +322 -0
- package/src/feature-flags/data.ts +16 -0
- package/src/feature-flags/default.ts +18 -0
- package/src/feature-flags/index.ts +7 -0
- package/src/feature-flags/requestable-features.ts +79 -0
- package/src/feature-flags/types.ts +4 -0
- package/src/fetcher.ts +2 -0
- package/src/finance/index.ts +4 -0
- package/src/finance/interest-calculator.ts +456 -0
- package/src/finance/interest-detector.ts +141 -0
- package/src/finance/transform-invoice-results.ts +219 -0
- package/src/finance/wallet-permissions.test.ts +169 -0
- package/src/finance/wallet-permissions.ts +82 -0
- package/src/format.ts +122 -3
- package/src/generated/platform-build-metadata.ts +11 -0
- package/src/hooks/use-platform.ts +64 -0
- package/src/html-sanitizer.ts +155 -0
- package/src/internal-domains.ts +497 -0
- package/src/keyboard-preset.ts +109 -0
- package/src/label-colors.ts +213 -0
- package/src/launchable-apps.test.ts +126 -0
- package/src/launchable-apps.ts +490 -0
- package/src/name-helper.ts +269 -0
- package/src/next-config.test.ts +234 -0
- package/src/next-config.ts +203 -0
- package/src/node-diff.ts +375 -0
- package/src/notification-service.ts +379 -0
- package/src/nova/scores/__tests__/calculate.test.ts +254 -0
- package/src/nova/scores/calculate.ts +132 -0
- package/src/nova/submissions/check-permission.ts +132 -0
- package/src/onboarding-helper.ts +213 -0
- package/src/path-helper.ts +93 -0
- package/src/permissions.tsx +1170 -0
- package/src/plan-helpers.test.ts +188 -0
- package/src/plan-helpers.ts +80 -0
- package/src/platform-release.test.ts +74 -0
- package/src/platform-release.ts +155 -0
- package/src/portless.ts +124 -0
- package/src/priority-styles.ts +42 -0
- package/src/request-emoji-limit.ts +335 -0
- package/src/search-helper.ts +18 -0
- package/src/search.test.ts +89 -0
- package/src/search.ts +355 -0
- package/src/storage-display-name.ts +30 -0
- package/src/storage-path.ts +147 -0
- package/src/tag-utils.ts +159 -0
- package/src/task/reorder.ts +245 -0
- package/src/task/transformers.ts +149 -0
- package/src/task-date-timezone.ts +133 -0
- package/src/task-description-content.ts +240 -0
- package/src/task-helper/board.ts +193 -0
- package/src/task-helper/bulk-actions.ts +564 -0
- package/src/task-helper/personal-external-staging.ts +21 -0
- package/src/task-helper/recycle-bin.ts +202 -0
- package/src/task-helper/relationships.ts +346 -0
- package/src/task-helper/shared.ts +109 -0
- package/src/task-helper/sort-keys.ts +337 -0
- package/src/task-helper/task-hooks-basic.ts +342 -0
- package/src/task-helper/task-hooks-move.ts +264 -0
- package/src/task-helper/task-operations.ts +278 -0
- package/src/task-helper.ts +12 -0
- package/src/task-helpers.ts +241 -0
- package/src/task-list-status.ts +62 -0
- package/src/task-overrides.ts +82 -0
- package/src/task-snapshot.ts +374 -0
- package/src/text-diff.ts +81 -0
- package/src/text-helper.ts +537 -0
- package/src/time-helper.ts +63 -0
- package/src/time-tracker-period.ts +73 -0
- package/src/timeblock-helper.ts +418 -0
- package/src/timezone.ts +190 -0
- package/src/timezones.json +1271 -0
- package/src/upstash-rest.ts +56 -0
- package/src/user-helper.ts +296 -0
- package/src/uuid-helper.ts +11 -0
- package/src/workspace-handle.ts +10 -0
- package/src/workspace-helper.ts +1408 -0
- package/src/workspace-limits.ts +68 -0
- package/src/yjs-helper.ts +217 -0
- package/src/yjs-task-description.ts +81 -0
- package/tsconfig.json +3 -5
- package/tsconfig.typecheck.json +33 -0
- package/vitest.config.ts +36 -0
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
- package/eslint.config.mjs +0 -20
- package/rollup.config.js +0 -41
- package/src/index.ts +0 -1
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const mocks = vi.hoisted(() => ({
|
|
4
|
+
getRedis: vi.fn(),
|
|
5
|
+
redis: {
|
|
6
|
+
decr: vi.fn(),
|
|
7
|
+
del: vi.fn(),
|
|
8
|
+
expire: vi.fn(),
|
|
9
|
+
get: vi.fn(),
|
|
10
|
+
incr: vi.fn(),
|
|
11
|
+
set: vi.fn(),
|
|
12
|
+
ttl: vi.fn(),
|
|
13
|
+
},
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
vi.mock('../upstash-rest.js', () => ({
|
|
17
|
+
getUpstashRestRedisClient: () => mocks.getRedis(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
describe('ai temp auth', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.clearAllMocks();
|
|
23
|
+
mocks.getRedis.mockResolvedValue(mocks.redis);
|
|
24
|
+
mocks.redis.get.mockResolvedValue('0');
|
|
25
|
+
mocks.redis.set.mockResolvedValue('OK');
|
|
26
|
+
mocks.redis.incr.mockResolvedValue(1);
|
|
27
|
+
mocks.redis.decr.mockResolvedValue(0);
|
|
28
|
+
mocks.redis.expire.mockResolvedValue(1);
|
|
29
|
+
mocks.redis.del.mockResolvedValue(1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('mints a 60-second token and stores only its digest in Redis', async () => {
|
|
33
|
+
const { mintAiTempAuthToken } = await import('../ai-temp-auth.js');
|
|
34
|
+
|
|
35
|
+
const minted = await mintAiTempAuthToken({
|
|
36
|
+
user: { id: 'user-1', email: 'user@test.com' },
|
|
37
|
+
wsId: 'workspace-1',
|
|
38
|
+
creditWsId: 'workspace-1',
|
|
39
|
+
creditSource: 'workspace',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(minted).not.toBeNull();
|
|
43
|
+
expect(minted?.token).toContain('.');
|
|
44
|
+
expect(minted?.expiresAt).toBeGreaterThan(Date.now());
|
|
45
|
+
expect(mocks.redis.set).toHaveBeenCalledTimes(1);
|
|
46
|
+
|
|
47
|
+
const [key, payload, options] = mocks.redis.set.mock.calls[0]!;
|
|
48
|
+
expect(key).toMatch(/^ai:temp-auth:token:/);
|
|
49
|
+
expect(key).not.toContain(minted!.token);
|
|
50
|
+
expect(JSON.stringify(payload)).not.toContain(minted!.token);
|
|
51
|
+
expect(options).toEqual({ ex: 60 });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('validates an unrevoked token and returns its cached identity context', async () => {
|
|
55
|
+
const { mintAiTempAuthToken, validateAiTempAuthRequest } = await import(
|
|
56
|
+
'../ai-temp-auth.js'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const minted = await mintAiTempAuthToken({
|
|
60
|
+
user: { id: 'user-1', email: 'user@test.com' },
|
|
61
|
+
wsId: 'workspace-1',
|
|
62
|
+
creditWsId: 'workspace-2',
|
|
63
|
+
creditSource: 'personal',
|
|
64
|
+
});
|
|
65
|
+
const storedPayload = mocks.redis.set.mock.calls[0]![1];
|
|
66
|
+
mocks.redis.get
|
|
67
|
+
.mockResolvedValueOnce(storedPayload)
|
|
68
|
+
.mockResolvedValueOnce(storedPayload.authVersion);
|
|
69
|
+
|
|
70
|
+
const request = new Request('https://app.test/api/ai/chat', {
|
|
71
|
+
headers: { 'x-tuturuuu-ai-temp-auth': minted!.token },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
await expect(validateAiTempAuthRequest(request)).resolves.toEqual({
|
|
75
|
+
status: 'valid',
|
|
76
|
+
context: expect.objectContaining({
|
|
77
|
+
user: { id: 'user-1', email: 'user@test.com' },
|
|
78
|
+
wsId: 'workspace-1',
|
|
79
|
+
creditWsId: 'workspace-2',
|
|
80
|
+
creditSource: 'personal',
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('rejects a token when the user auth version has changed', async () => {
|
|
86
|
+
const { mintAiTempAuthToken, validateAiTempAuthRequest } = await import(
|
|
87
|
+
'../ai-temp-auth.js'
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const minted = await mintAiTempAuthToken({
|
|
91
|
+
user: { id: 'user-1', email: null },
|
|
92
|
+
wsId: 'workspace-1',
|
|
93
|
+
});
|
|
94
|
+
const storedPayload = mocks.redis.set.mock.calls[0]![1];
|
|
95
|
+
mocks.redis.get
|
|
96
|
+
.mockResolvedValueOnce(storedPayload)
|
|
97
|
+
.mockResolvedValueOnce('2');
|
|
98
|
+
|
|
99
|
+
const request = new Request('https://app.test/api/ai/chat', {
|
|
100
|
+
headers: { 'x-tuturuuu-ai-temp-auth': minted!.token },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await expect(validateAiTempAuthRequest(request)).resolves.toEqual({
|
|
104
|
+
status: 'revoked',
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('falls back when Redis is unavailable', async () => {
|
|
109
|
+
mocks.getRedis.mockResolvedValue(null);
|
|
110
|
+
const { mintAiTempAuthToken, validateAiTempAuthRequest } = await import(
|
|
111
|
+
'../ai-temp-auth.js'
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
await expect(
|
|
115
|
+
mintAiTempAuthToken({ user: { id: 'user-1', email: null } })
|
|
116
|
+
).resolves.toBeNull();
|
|
117
|
+
|
|
118
|
+
const request = new Request('https://app.test/api/ai/chat', {
|
|
119
|
+
headers: { 'x-tuturuuu-ai-temp-auth': 'token.secret' },
|
|
120
|
+
});
|
|
121
|
+
await expect(validateAiTempAuthRequest(request)).resolves.toEqual({
|
|
122
|
+
status: 'unavailable',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('falls back when Redis token reads or writes fail', async () => {
|
|
127
|
+
const { mintAiTempAuthToken, validateAiTempAuthRequest } = await import(
|
|
128
|
+
'../ai-temp-auth.js'
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
mocks.redis.set.mockRejectedValueOnce(new Error('redis write failed'));
|
|
132
|
+
await expect(
|
|
133
|
+
mintAiTempAuthToken({ user: { id: 'user-1', email: null } })
|
|
134
|
+
).resolves.toBeNull();
|
|
135
|
+
|
|
136
|
+
mocks.redis.get.mockRejectedValueOnce(new Error('redis read failed'));
|
|
137
|
+
const request = new Request('https://app.test/api/ai/chat', {
|
|
138
|
+
headers: { 'x-tuturuuu-ai-temp-auth': 'token.secret' },
|
|
139
|
+
});
|
|
140
|
+
await expect(validateAiTempAuthRequest(request)).resolves.toEqual({
|
|
141
|
+
status: 'invalid',
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('bumps the per-user auth version for automatic token revocation', async () => {
|
|
146
|
+
const { revokeUserAiTempAuthTokens } = await import('../ai-temp-auth.js');
|
|
147
|
+
|
|
148
|
+
await expect(revokeUserAiTempAuthTokens('user-1')).resolves.toBe(true);
|
|
149
|
+
expect(mocks.redis.incr).toHaveBeenCalledWith(
|
|
150
|
+
'ai:temp-auth:user-version:user-1'
|
|
151
|
+
);
|
|
152
|
+
expect(mocks.redis.expire).toHaveBeenCalledWith(
|
|
153
|
+
'ai:temp-auth:user-version:user-1',
|
|
154
|
+
86_400
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('ai credit snapshots', () => {
|
|
160
|
+
beforeEach(() => {
|
|
161
|
+
vi.clearAllMocks();
|
|
162
|
+
mocks.getRedis.mockResolvedValue(mocks.redis);
|
|
163
|
+
mocks.redis.get.mockResolvedValue(null);
|
|
164
|
+
mocks.redis.set.mockResolvedValue('OK');
|
|
165
|
+
mocks.redis.del.mockResolvedValue(1);
|
|
166
|
+
mocks.redis.incr.mockResolvedValue(1);
|
|
167
|
+
mocks.redis.decr.mockResolvedValue(0);
|
|
168
|
+
mocks.redis.expire.mockResolvedValue(1);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('writes, reads, and invalidates short-lived credit snapshots', async () => {
|
|
172
|
+
const {
|
|
173
|
+
invalidateAiCreditSnapshot,
|
|
174
|
+
readAiCreditSnapshot,
|
|
175
|
+
writeAiCreditSnapshot,
|
|
176
|
+
} = await import('../ai-temp-auth.js');
|
|
177
|
+
const snapshot = {
|
|
178
|
+
remainingCredits: 1200,
|
|
179
|
+
maxOutputTokens: 4096,
|
|
180
|
+
tier: 'PRO',
|
|
181
|
+
allowedModels: ['google/gemini-2.5-flash'],
|
|
182
|
+
allowedFeatures: ['chat'],
|
|
183
|
+
dailyLimit: null,
|
|
184
|
+
updatedAt: Date.now(),
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
await expect(
|
|
188
|
+
writeAiCreditSnapshot({
|
|
189
|
+
wsId: 'workspace-1',
|
|
190
|
+
userId: 'user-1',
|
|
191
|
+
snapshot,
|
|
192
|
+
})
|
|
193
|
+
).resolves.toBe(true);
|
|
194
|
+
expect(mocks.redis.set).toHaveBeenCalledWith(
|
|
195
|
+
'ai:credits:snapshot:workspace-1:user-1',
|
|
196
|
+
snapshot,
|
|
197
|
+
{ ex: 15 }
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
mocks.redis.get.mockResolvedValue(snapshot);
|
|
201
|
+
await expect(
|
|
202
|
+
readAiCreditSnapshot({ wsId: 'workspace-1', userId: 'user-1' })
|
|
203
|
+
).resolves.toEqual(snapshot);
|
|
204
|
+
|
|
205
|
+
await expect(
|
|
206
|
+
invalidateAiCreditSnapshot({ wsId: 'workspace-1', userId: 'user-1' })
|
|
207
|
+
).resolves.toBe(true);
|
|
208
|
+
expect(mocks.redis.del).toHaveBeenCalledWith(
|
|
209
|
+
'ai:credits:snapshot:workspace-1:user-1'
|
|
210
|
+
);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('tracks charge in-flight markers with a short TTL', async () => {
|
|
214
|
+
const {
|
|
215
|
+
decrementAiCreditChargeInFlight,
|
|
216
|
+
hasAiCreditChargeInFlight,
|
|
217
|
+
incrementAiCreditChargeInFlight,
|
|
218
|
+
} = await import('../ai-temp-auth.js');
|
|
219
|
+
|
|
220
|
+
await expect(
|
|
221
|
+
incrementAiCreditChargeInFlight({ wsId: 'workspace-1', userId: 'user-1' })
|
|
222
|
+
).resolves.toBe(true);
|
|
223
|
+
expect(mocks.redis.incr).toHaveBeenCalledWith(
|
|
224
|
+
'ai:credits:in-flight:workspace-1:user-1'
|
|
225
|
+
);
|
|
226
|
+
expect(mocks.redis.expire).toHaveBeenCalledWith(
|
|
227
|
+
'ai:credits:in-flight:workspace-1:user-1',
|
|
228
|
+
60
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
mocks.redis.get.mockResolvedValueOnce(1);
|
|
232
|
+
await expect(
|
|
233
|
+
hasAiCreditChargeInFlight({ wsId: 'workspace-1', userId: 'user-1' })
|
|
234
|
+
).resolves.toBe(true);
|
|
235
|
+
|
|
236
|
+
await expect(
|
|
237
|
+
decrementAiCreditChargeInFlight({ wsId: 'workspace-1', userId: 'user-1' })
|
|
238
|
+
).resolves.toBe(true);
|
|
239
|
+
expect(mocks.redis.decr).toHaveBeenCalledWith(
|
|
240
|
+
'ai:credits:in-flight:workspace-1:user-1'
|
|
241
|
+
);
|
|
242
|
+
expect(mocks.redis.del).toHaveBeenCalledWith(
|
|
243
|
+
'ai:credits:in-flight:workspace-1:user-1'
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('fails open when Redis snapshot operations throw', async () => {
|
|
248
|
+
const {
|
|
249
|
+
invalidateAiCreditSnapshot,
|
|
250
|
+
readAiCreditSnapshot,
|
|
251
|
+
writeAiCreditSnapshot,
|
|
252
|
+
} = await import('../ai-temp-auth.js');
|
|
253
|
+
const snapshot = {
|
|
254
|
+
remainingCredits: 1200,
|
|
255
|
+
maxOutputTokens: 4096,
|
|
256
|
+
tier: 'PRO',
|
|
257
|
+
allowedModels: [],
|
|
258
|
+
allowedFeatures: [],
|
|
259
|
+
dailyLimit: null,
|
|
260
|
+
updatedAt: Date.now(),
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
mocks.redis.set.mockRejectedValueOnce(new Error('redis write failed'));
|
|
264
|
+
await expect(
|
|
265
|
+
writeAiCreditSnapshot({
|
|
266
|
+
wsId: 'workspace-1',
|
|
267
|
+
userId: 'user-1',
|
|
268
|
+
snapshot,
|
|
269
|
+
})
|
|
270
|
+
).resolves.toBe(false);
|
|
271
|
+
|
|
272
|
+
mocks.redis.get.mockRejectedValueOnce(new Error('redis read failed'));
|
|
273
|
+
await expect(
|
|
274
|
+
readAiCreditSnapshot({ wsId: 'workspace-1', userId: 'user-1' })
|
|
275
|
+
).resolves.toBeNull();
|
|
276
|
+
|
|
277
|
+
mocks.redis.del.mockRejectedValueOnce(new Error('redis delete failed'));
|
|
278
|
+
await expect(
|
|
279
|
+
invalidateAiCreditSnapshot({ wsId: 'workspace-1', userId: 'user-1' })
|
|
280
|
+
).resolves.toBe(false);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('uses a credit snapshot only when it is fresh, not near exhaustion, and no charge is in flight', async () => {
|
|
284
|
+
const { isAiCreditSnapshotUsable } = await import('../ai-temp-auth.js');
|
|
285
|
+
const freshSnapshot = {
|
|
286
|
+
remainingCredits: 1200,
|
|
287
|
+
maxOutputTokens: 4096,
|
|
288
|
+
tier: 'PRO',
|
|
289
|
+
allowedModels: [],
|
|
290
|
+
allowedFeatures: [],
|
|
291
|
+
dailyLimit: null,
|
|
292
|
+
updatedAt: Date.now(),
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
expect(isAiCreditSnapshotUsable(freshSnapshot)).toBe(true);
|
|
296
|
+
expect(
|
|
297
|
+
isAiCreditSnapshotUsable({ ...freshSnapshot, remainingCredits: 49 })
|
|
298
|
+
).toBe(false);
|
|
299
|
+
expect(
|
|
300
|
+
isAiCreditSnapshotUsable({
|
|
301
|
+
...freshSnapshot,
|
|
302
|
+
updatedAt: Date.now() - 16_000,
|
|
303
|
+
})
|
|
304
|
+
).toBe(false);
|
|
305
|
+
expect(isAiCreditSnapshotUsable(freshSnapshot, { inFlight: true })).toBe(
|
|
306
|
+
false
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
});
|