@supatype/client 0.1.0-alpha.10
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test.log +23 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/dist/auth.d.ts +279 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +848 -0
- package/dist/auth.js.map +1 -0
- package/dist/error-codes-doc.d.ts +31 -0
- package/dist/error-codes-doc.d.ts.map +1 -0
- package/dist/error-codes-doc.js +294 -0
- package/dist/error-codes-doc.js.map +1 -0
- package/dist/errors.d.ts +197 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +162 -0
- package/dist/errors.js.map +1 -0
- package/dist/fetch-with-retry.d.ts +49 -0
- package/dist/fetch-with-retry.d.ts.map +1 -0
- package/dist/fetch-with-retry.js +150 -0
- package/dist/fetch-with-retry.js.map +1 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +55 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +264 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.d.ts +69 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +279 -0
- package/dist/realtime.js.map +1 -0
- package/dist/retry.d.ts +24 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +25 -0
- package/dist/retry.js.map +1 -0
- package/dist/serverless-docs.d.ts +38 -0
- package/dist/serverless-docs.d.ts.map +1 -0
- package/dist/serverless-docs.js +83 -0
- package/dist/serverless-docs.js.map +1 -0
- package/dist/storage.d.ts +102 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +169 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +26 -0
- package/src/auth.ts +1024 -0
- package/src/error-codes-doc.ts +315 -0
- package/src/errors.ts +201 -0
- package/src/fetch-with-retry.ts +200 -0
- package/src/index.ts +518 -0
- package/src/query.ts +331 -0
- package/src/realtime.ts +418 -0
- package/src/retry.ts +35 -0
- package/src/serverless-docs.ts +107 -0
- package/src/storage.ts +234 -0
- package/src/types.ts +264 -0
- package/tests/auth.test.ts +351 -0
- package/tests/mfa-totp-integration.test.ts +359 -0
- package/tests/query.test.ts +286 -0
- package/tests/rate-limit-integration.test.ts +185 -0
- package/tests/realtime-multi-tenant-integration.test.ts +372 -0
- package/tests/retry-integration.test.ts +212 -0
- package/tests/serverless.test.ts +115 -0
- package/tests/storage-quota-integration.test.ts +189 -0
- package/tests/storage-upload-limit-integration.test.ts +185 -0
- package/tests/types.test.ts +137 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration test — Task 90: SDK retry behaviour
|
|
3
|
+
*
|
|
4
|
+
* Simulates 503 -> SDK retries 3x with backoff -> succeeds or throws after max retries.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
8
|
+
import { fetchWithRetry } from "../src/fetch-with-retry.js"
|
|
9
|
+
import { NetworkError } from "../src/errors.js"
|
|
10
|
+
|
|
11
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
function mockResponse(status: number, body: unknown = {}): Response {
|
|
14
|
+
return {
|
|
15
|
+
ok: status >= 200 && status < 300,
|
|
16
|
+
status,
|
|
17
|
+
json: vi.fn().mockResolvedValue(body),
|
|
18
|
+
headers: new Headers(),
|
|
19
|
+
statusText: status === 503 ? "Service Unavailable" : "OK",
|
|
20
|
+
} as unknown as Response
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ─── Tests ───────────────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
describe("Task 90 — SDK retry integration", () => {
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
vi.restoreAllMocks()
|
|
28
|
+
vi.useFakeTimers()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.useRealTimers()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("succeeds immediately on 200 with no retries", async () => {
|
|
36
|
+
const fetchSpy = vi.fn().mockResolvedValue(mockResponse(200, { data: "ok" }))
|
|
37
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
38
|
+
|
|
39
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
40
|
+
const res = await promise
|
|
41
|
+
|
|
42
|
+
expect(res.ok).toBe(true)
|
|
43
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("retries on 503 and succeeds on second attempt", async () => {
|
|
47
|
+
const fetchSpy = vi.fn()
|
|
48
|
+
.mockResolvedValueOnce(mockResponse(503))
|
|
49
|
+
.mockResolvedValueOnce(mockResponse(200, { data: "recovered" }))
|
|
50
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
51
|
+
|
|
52
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
53
|
+
|
|
54
|
+
// Advance past first retry delay (200ms)
|
|
55
|
+
await vi.advanceTimersByTimeAsync(250)
|
|
56
|
+
|
|
57
|
+
const res = await promise
|
|
58
|
+
expect(res.ok).toBe(true)
|
|
59
|
+
expect(fetchSpy).toHaveBeenCalledTimes(2)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("retries on 500 server errors", async () => {
|
|
63
|
+
const fetchSpy = vi.fn()
|
|
64
|
+
.mockResolvedValueOnce(mockResponse(500))
|
|
65
|
+
.mockResolvedValueOnce(mockResponse(502))
|
|
66
|
+
.mockResolvedValueOnce(mockResponse(200, { data: "ok" }))
|
|
67
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
68
|
+
|
|
69
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
70
|
+
|
|
71
|
+
// Advance through retry delays
|
|
72
|
+
await vi.advanceTimersByTimeAsync(250) // First retry (200ms delay)
|
|
73
|
+
await vi.advanceTimersByTimeAsync(1100) // Second retry (1000ms delay)
|
|
74
|
+
|
|
75
|
+
const res = await promise
|
|
76
|
+
expect(res.ok).toBe(true)
|
|
77
|
+
expect(fetchSpy).toHaveBeenCalledTimes(3)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it("retries up to maxRetries then returns the 503 response", async () => {
|
|
81
|
+
const fetchSpy = vi.fn().mockResolvedValue(mockResponse(503))
|
|
82
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
83
|
+
|
|
84
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
85
|
+
|
|
86
|
+
// Advance through all retry delays
|
|
87
|
+
await vi.advanceTimersByTimeAsync(250) // retry 1 (200ms)
|
|
88
|
+
await vi.advanceTimersByTimeAsync(1100) // retry 2 (1000ms)
|
|
89
|
+
await vi.advanceTimersByTimeAsync(3100) // retry 3 (3000ms)
|
|
90
|
+
|
|
91
|
+
const res = await promise
|
|
92
|
+
// After all retries exhausted, the last 503 response is returned
|
|
93
|
+
expect(res.status).toBe(503)
|
|
94
|
+
expect(fetchSpy).toHaveBeenCalledTimes(4) // initial + 3 retries
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("does not retry on 4xx client errors (except 429)", async () => {
|
|
98
|
+
const fetchSpy = vi.fn().mockResolvedValue(mockResponse(404, { error: "Not found" }))
|
|
99
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
100
|
+
|
|
101
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
102
|
+
const res = await promise
|
|
103
|
+
|
|
104
|
+
expect(res.status).toBe(404)
|
|
105
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1) // No retries for 4xx
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it("does not retry on 401", async () => {
|
|
109
|
+
const fetchSpy = vi.fn().mockResolvedValue(mockResponse(401, { error: "Unauthorized" }))
|
|
110
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
111
|
+
|
|
112
|
+
const res = await fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
113
|
+
|
|
114
|
+
expect(res.status).toBe(401)
|
|
115
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("retries on network errors (fetch throws)", async () => {
|
|
119
|
+
const fetchSpy = vi.fn()
|
|
120
|
+
.mockRejectedValueOnce(new Error("ECONNREFUSED"))
|
|
121
|
+
.mockRejectedValueOnce(new Error("ECONNREFUSED"))
|
|
122
|
+
.mockResolvedValueOnce(mockResponse(200, { data: "ok" }))
|
|
123
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
124
|
+
|
|
125
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 5000 })
|
|
126
|
+
|
|
127
|
+
await vi.advanceTimersByTimeAsync(250)
|
|
128
|
+
await vi.advanceTimersByTimeAsync(1100)
|
|
129
|
+
|
|
130
|
+
const res = await promise
|
|
131
|
+
expect(res.ok).toBe(true)
|
|
132
|
+
expect(fetchSpy).toHaveBeenCalledTimes(3)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it("throws NetworkError after exhausting retries on network failures", async () => {
|
|
136
|
+
vi.useRealTimers()
|
|
137
|
+
const fetchSpy = vi.fn().mockRejectedValue(new Error("ECONNREFUSED"))
|
|
138
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
139
|
+
|
|
140
|
+
await expect(
|
|
141
|
+
fetchWithRetry("http://api.test/data", { maxRetries: 0, timeout: 5000 }),
|
|
142
|
+
).rejects.toThrow(NetworkError)
|
|
143
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1)
|
|
144
|
+
vi.useFakeTimers()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it("throws NetworkError with CONNECTION_REFUSED code", async () => {
|
|
148
|
+
vi.useRealTimers()
|
|
149
|
+
const fetchSpy = vi.fn().mockRejectedValue(new Error("ECONNREFUSED"))
|
|
150
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
await fetchWithRetry("http://api.test/data", { maxRetries: 0, timeout: 5000 })
|
|
154
|
+
expect.fail("Should have thrown")
|
|
155
|
+
} catch (err) {
|
|
156
|
+
expect(err).toBeInstanceOf(NetworkError)
|
|
157
|
+
expect((err as NetworkError).code).toBe("NETWORK_CONNECTION_REFUSED")
|
|
158
|
+
}
|
|
159
|
+
vi.useFakeTimers()
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it("respects retry: false option — no retries attempted", async () => {
|
|
163
|
+
const fetchSpy = vi.fn().mockResolvedValue(mockResponse(503))
|
|
164
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
165
|
+
|
|
166
|
+
const res = await fetchWithRetry("http://api.test/data", { retry: false, timeout: 5000 })
|
|
167
|
+
|
|
168
|
+
expect(res.status).toBe(503)
|
|
169
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it("uses exponential backoff delays: 200ms, 1000ms, 3000ms", async () => {
|
|
173
|
+
const timestamps: number[] = []
|
|
174
|
+
const fetchSpy = vi.fn().mockImplementation(async () => {
|
|
175
|
+
timestamps.push(Date.now())
|
|
176
|
+
return mockResponse(503)
|
|
177
|
+
})
|
|
178
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
179
|
+
|
|
180
|
+
const promise = fetchWithRetry("http://api.test/data", { maxRetries: 3, timeout: 30000 })
|
|
181
|
+
|
|
182
|
+
// Initial call happens immediately
|
|
183
|
+
await vi.advanceTimersByTimeAsync(0)
|
|
184
|
+
expect(timestamps).toHaveLength(1)
|
|
185
|
+
|
|
186
|
+
// First retry after ~200ms
|
|
187
|
+
await vi.advanceTimersByTimeAsync(200)
|
|
188
|
+
expect(timestamps).toHaveLength(2)
|
|
189
|
+
|
|
190
|
+
// Second retry after ~1000ms
|
|
191
|
+
await vi.advanceTimersByTimeAsync(1000)
|
|
192
|
+
expect(timestamps).toHaveLength(3)
|
|
193
|
+
|
|
194
|
+
// Third retry after ~3000ms
|
|
195
|
+
await vi.advanceTimersByTimeAsync(3000)
|
|
196
|
+
expect(timestamps).toHaveLength(4)
|
|
197
|
+
|
|
198
|
+
await promise
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it("AbortError from timeout throws NetworkError with TIMEOUT code", async () => {
|
|
202
|
+
const abortError = new Error("The operation was aborted")
|
|
203
|
+
abortError.name = "AbortError"
|
|
204
|
+
const fetchSpy = vi.fn().mockRejectedValue(abortError)
|
|
205
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
206
|
+
|
|
207
|
+
// Timeout is handled by the AbortController in fetchWithRetry
|
|
208
|
+
await expect(
|
|
209
|
+
fetchWithRetry("http://api.test/data", { maxRetries: 0, timeout: 100 }),
|
|
210
|
+
).rejects.toThrow("timed out")
|
|
211
|
+
})
|
|
212
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
2
|
+
import { detectServerlessEnvironment, warnIfServerlessDirectConnection } from "../src/fetch-with-retry.js"
|
|
3
|
+
|
|
4
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Set an environment variable for the duration of a test, restoring it
|
|
8
|
+
* (or deleting it) afterwards.
|
|
9
|
+
*/
|
|
10
|
+
function withEnv(key: string, value: string): () => void {
|
|
11
|
+
const prev = process.env[key]
|
|
12
|
+
process.env[key] = value
|
|
13
|
+
return () => {
|
|
14
|
+
if (prev === undefined) {
|
|
15
|
+
delete process.env[key]
|
|
16
|
+
} else {
|
|
17
|
+
process.env[key] = prev
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ─── detectServerlessEnvironment ─────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
describe("detectServerlessEnvironment", () => {
|
|
25
|
+
let restoreEnv: (() => void) | undefined
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
restoreEnv?.()
|
|
29
|
+
restoreEnv = undefined
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it("returns detected=false when no serverless env vars are set", () => {
|
|
33
|
+
const result = detectServerlessEnvironment()
|
|
34
|
+
expect(result.detected).toBe(false)
|
|
35
|
+
expect(result.platform).toBeNull()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("detects Vercel when VERCEL=1", () => {
|
|
39
|
+
restoreEnv = withEnv("VERCEL", "1")
|
|
40
|
+
const result = detectServerlessEnvironment()
|
|
41
|
+
expect(result.detected).toBe(true)
|
|
42
|
+
expect(result.platform).toBe("Vercel")
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it("does not detect Vercel when VERCEL has wrong value", () => {
|
|
46
|
+
restoreEnv = withEnv("VERCEL", "0")
|
|
47
|
+
const result = detectServerlessEnvironment()
|
|
48
|
+
// VERCEL must be exactly "1"
|
|
49
|
+
expect(result.detected).toBe(false)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("detects AWS Lambda when AWS_LAMBDA_FUNCTION_NAME is set", () => {
|
|
53
|
+
restoreEnv = withEnv("AWS_LAMBDA_FUNCTION_NAME", "my-function")
|
|
54
|
+
const result = detectServerlessEnvironment()
|
|
55
|
+
expect(result.detected).toBe(true)
|
|
56
|
+
expect(result.platform).toBe("AWS Lambda")
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("detects Netlify when NETLIFY=true", () => {
|
|
60
|
+
restoreEnv = withEnv("NETLIFY", "true")
|
|
61
|
+
const result = detectServerlessEnvironment()
|
|
62
|
+
expect(result.detected).toBe(true)
|
|
63
|
+
expect(result.platform).toBe("Netlify")
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it("detects Cloudflare Workers when CF_PAGES is set", () => {
|
|
67
|
+
restoreEnv = withEnv("CF_PAGES", "1")
|
|
68
|
+
const result = detectServerlessEnvironment()
|
|
69
|
+
expect(result.detected).toBe(true)
|
|
70
|
+
expect(result.platform).toBe("Cloudflare Workers")
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// ─── warnIfServerlessDirectConnection ────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
describe("warnIfServerlessDirectConnection", () => {
|
|
77
|
+
let restoreEnv: (() => void) | undefined
|
|
78
|
+
let warnSpy: ReturnType<typeof vi.spyOn>
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
|
|
82
|
+
// Reset the one-time guard between tests by re-importing.
|
|
83
|
+
// Since the guard is module-level state, we need to reset it.
|
|
84
|
+
// We do this by directly resetting via a dynamic import trick — but
|
|
85
|
+
// the simplest approach is to just accept the guard fires once per
|
|
86
|
+
// describe block. Instead, let's reset it manually.
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
afterEach(() => {
|
|
90
|
+
restoreEnv?.()
|
|
91
|
+
restoreEnv = undefined
|
|
92
|
+
warnSpy.mockRestore()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it("does not warn when not in a serverless environment", () => {
|
|
96
|
+
warnIfServerlessDirectConnection("postgres://localhost:5432/mydb")
|
|
97
|
+
expect(warnSpy).not.toHaveBeenCalled()
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it("warns when a direct postgres:// URL is used in a serverless env", () => {
|
|
101
|
+
restoreEnv = withEnv("AWS_LAMBDA_FUNCTION_NAME", "handler")
|
|
102
|
+
warnIfServerlessDirectConnection("postgres://db.example.com:5432/mydb")
|
|
103
|
+
expect(warnSpy).toHaveBeenCalledOnce()
|
|
104
|
+
expect(warnSpy.mock.calls[0]?.[0]).toContain(
|
|
105
|
+
"Direct database connections are not recommended in serverless environments"
|
|
106
|
+
)
|
|
107
|
+
expect(warnSpy.mock.calls[0]?.[0]).toContain("AWS Lambda")
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it("does not warn for HTTP gateway URLs in a serverless env", () => {
|
|
111
|
+
restoreEnv = withEnv("VERCEL", "1")
|
|
112
|
+
warnIfServerlessDirectConnection("https://my-project.supatype.co")
|
|
113
|
+
expect(warnSpy).not.toHaveBeenCalled()
|
|
114
|
+
})
|
|
115
|
+
})
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration test — Task 93: Storage quota exceeded
|
|
3
|
+
*
|
|
4
|
+
* Tests: quota exceeded -> 507 with clear message.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
8
|
+
import { StorageClient } from "../src/storage.js"
|
|
9
|
+
|
|
10
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
const STORAGE_URL = "http://localhost:18473/storage/v1"
|
|
13
|
+
const HEADERS = { apikey: "test-anon-key", Authorization: "Bearer test-token" }
|
|
14
|
+
|
|
15
|
+
function freshClient(): StorageClient {
|
|
16
|
+
return new StorageClient(STORAGE_URL, HEADERS)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createMockBlob(sizeBytes: number): Blob {
|
|
20
|
+
return new Blob([new Uint8Array(sizeBytes)])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function mockFetchResponse(
|
|
24
|
+
status: number,
|
|
25
|
+
body: unknown,
|
|
26
|
+
ok?: boolean,
|
|
27
|
+
): ReturnType<typeof vi.fn> {
|
|
28
|
+
return vi.fn().mockResolvedValue({
|
|
29
|
+
ok: ok ?? (status >= 200 && status < 300),
|
|
30
|
+
status,
|
|
31
|
+
json: vi.fn().mockResolvedValue(body),
|
|
32
|
+
headers: new Headers(),
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ─── Tests ───────────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
describe("Task 93 — Storage quota integration", () => {
|
|
39
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
40
|
+
|
|
41
|
+
describe("Quota exceeded on upload", () => {
|
|
42
|
+
it("returns 507 Insufficient Storage with clear message", async () => {
|
|
43
|
+
vi.stubGlobal("fetch", mockFetchResponse(507, {
|
|
44
|
+
message: "Storage quota exceeded. Your project has used 100% of its 1GB storage allocation.",
|
|
45
|
+
statusCode: 507,
|
|
46
|
+
error: "Insufficient Storage",
|
|
47
|
+
}, false))
|
|
48
|
+
|
|
49
|
+
const client = freshClient()
|
|
50
|
+
const blob = createMockBlob(10 * 1024 * 1024) // 10 MB
|
|
51
|
+
const { data, error } = await client.from("uploads").upload("file.dat", blob)
|
|
52
|
+
|
|
53
|
+
expect(data).toBeNull()
|
|
54
|
+
expect(error).not.toBeNull()
|
|
55
|
+
expect(error!.status).toBe(507)
|
|
56
|
+
expect(error!.message).toContain("quota exceeded")
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("error message mentions the quota limit", async () => {
|
|
60
|
+
vi.stubGlobal("fetch", mockFetchResponse(507, {
|
|
61
|
+
message: "Storage quota exceeded. Your project has used 100% of its 1GB storage allocation. Upgrade your plan for more storage.",
|
|
62
|
+
}, false))
|
|
63
|
+
|
|
64
|
+
const client = freshClient()
|
|
65
|
+
const { error } = await client.from("uploads").upload("big.zip", createMockBlob(1024))
|
|
66
|
+
|
|
67
|
+
expect(error!.message).toContain("1GB")
|
|
68
|
+
expect(error!.message).toContain("quota")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("error message suggests upgrading plan", async () => {
|
|
72
|
+
vi.stubGlobal("fetch", mockFetchResponse(507, {
|
|
73
|
+
message: "Storage quota exceeded. Upgrade your plan for more storage.",
|
|
74
|
+
}, false))
|
|
75
|
+
|
|
76
|
+
const client = freshClient()
|
|
77
|
+
const { error } = await client.from("uploads").upload("document.pdf", createMockBlob(1024))
|
|
78
|
+
|
|
79
|
+
expect(error!.message).toContain("Upgrade")
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe("Quota-aware upload flow", () => {
|
|
84
|
+
it("succeeds when under quota", async () => {
|
|
85
|
+
vi.stubGlobal("fetch", mockFetchResponse(200, { Key: "bucket/file.txt" }))
|
|
86
|
+
|
|
87
|
+
const client = freshClient()
|
|
88
|
+
const { data, error } = await client.from("bucket").upload("file.txt", createMockBlob(1024))
|
|
89
|
+
|
|
90
|
+
expect(error).toBeNull()
|
|
91
|
+
expect(data!.path).toBe("file.txt")
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it("first upload succeeds, second hits quota", async () => {
|
|
95
|
+
const fetchSpy = vi.fn()
|
|
96
|
+
// First upload succeeds
|
|
97
|
+
.mockResolvedValueOnce({
|
|
98
|
+
ok: true,
|
|
99
|
+
status: 200,
|
|
100
|
+
json: vi.fn().mockResolvedValue({ Key: "bucket/first.txt" }),
|
|
101
|
+
headers: new Headers(),
|
|
102
|
+
})
|
|
103
|
+
// Second upload hits quota
|
|
104
|
+
.mockResolvedValueOnce({
|
|
105
|
+
ok: false,
|
|
106
|
+
status: 507,
|
|
107
|
+
json: vi.fn().mockResolvedValue({
|
|
108
|
+
message: "Storage quota exceeded. Your project has used 100% of its 1GB storage allocation.",
|
|
109
|
+
}),
|
|
110
|
+
headers: new Headers(),
|
|
111
|
+
})
|
|
112
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
113
|
+
|
|
114
|
+
const client = freshClient()
|
|
115
|
+
|
|
116
|
+
const first = await client.from("bucket").upload("first.txt", createMockBlob(500 * 1024 * 1024))
|
|
117
|
+
expect(first.error).toBeNull()
|
|
118
|
+
|
|
119
|
+
const second = await client.from("bucket").upload("second.txt", createMockBlob(600 * 1024 * 1024))
|
|
120
|
+
expect(second.data).toBeNull()
|
|
121
|
+
expect(second.error!.status).toBe(507)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
describe("Quota error vs size limit error differentiation", () => {
|
|
126
|
+
it("413 is file-level size limit, not quota", async () => {
|
|
127
|
+
vi.stubGlobal("fetch", mockFetchResponse(413, {
|
|
128
|
+
message: "Payload too large: file exceeds the maximum upload size of 50MB",
|
|
129
|
+
}, false))
|
|
130
|
+
|
|
131
|
+
const client = freshClient()
|
|
132
|
+
const { error } = await client.from("bucket").upload("huge.bin", createMockBlob(100))
|
|
133
|
+
|
|
134
|
+
expect(error!.status).toBe(413)
|
|
135
|
+
expect(error!.message).toContain("too large")
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it("507 is project-level quota, not file size", async () => {
|
|
139
|
+
vi.stubGlobal("fetch", mockFetchResponse(507, {
|
|
140
|
+
message: "Storage quota exceeded for project",
|
|
141
|
+
}, false))
|
|
142
|
+
|
|
143
|
+
const client = freshClient()
|
|
144
|
+
const { error } = await client.from("bucket").upload("small.txt", createMockBlob(100))
|
|
145
|
+
|
|
146
|
+
expect(error!.status).toBe(507)
|
|
147
|
+
expect(error!.message).toContain("quota")
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe("Quota on other storage operations", () => {
|
|
152
|
+
it("list works normally even when quota is exceeded", async () => {
|
|
153
|
+
vi.stubGlobal("fetch", mockFetchResponse(200, [
|
|
154
|
+
{ id: "1", name: "file1.txt", bucket_id: "bucket", created_at: "", updated_at: "", last_accessed_at: "" },
|
|
155
|
+
{ id: "2", name: "file2.txt", bucket_id: "bucket", created_at: "", updated_at: "", last_accessed_at: "" },
|
|
156
|
+
]))
|
|
157
|
+
|
|
158
|
+
const client = freshClient()
|
|
159
|
+
const { data, error } = await client.from("bucket").list()
|
|
160
|
+
|
|
161
|
+
expect(error).toBeNull()
|
|
162
|
+
expect(data).toHaveLength(2)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it("download works normally even when quota is exceeded", async () => {
|
|
166
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
|
|
167
|
+
ok: true,
|
|
168
|
+
status: 200,
|
|
169
|
+
blob: vi.fn().mockResolvedValue(new Blob(["file content"])),
|
|
170
|
+
headers: new Headers(),
|
|
171
|
+
}))
|
|
172
|
+
|
|
173
|
+
const client = freshClient()
|
|
174
|
+
const { data, error } = await client.from("bucket").download("file.txt")
|
|
175
|
+
|
|
176
|
+
expect(error).toBeNull()
|
|
177
|
+
expect(data).not.toBeNull()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it("delete works normally even when quota is exceeded", async () => {
|
|
181
|
+
vi.stubGlobal("fetch", mockFetchResponse(200, []))
|
|
182
|
+
|
|
183
|
+
const client = freshClient()
|
|
184
|
+
const { error } = await client.from("bucket").remove(["old-file.txt"])
|
|
185
|
+
|
|
186
|
+
expect(error).toBeNull()
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
})
|