-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathdump-jobs.test.ts
More file actions
147 lines (127 loc) · 4.41 KB
/
dump-jobs.test.ts
File metadata and controls
147 lines (127 loc) · 4.41 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { beforeEach, describe, expect, it, vi } from 'vitest'
import {
downloadDumpJobRoute,
getDumpJobStatusRoute,
startDumpJobRoute,
} from './dump-jobs'
import { executeOperation } from '.'
import type { DataSource } from '../types'
import type { StarbaseDBConfiguration } from '../handler'
vi.mock('.', () => ({
executeOperation: vi.fn(),
}))
let mockDataSource: DataSource
let mockConfig: StarbaseDBConfiguration
beforeEach(() => {
vi.clearAllMocks()
mockDataSource = {
source: 'internal',
rpc: { executeQuery: vi.fn() },
} as any
mockConfig = {
role: 'admin',
features: { export: true },
}
})
describe('Async Dump Jobs', () => {
it('starts an async dump job', async () => {
vi.spyOn(globalThis.crypto, 'randomUUID').mockReturnValue('job-1')
vi.mocked(executeOperation)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
const request = new Request('https://example.com/export/dump', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ batchSize: 250 }),
})
const response = await startDumpJobRoute(
request,
mockDataSource,
mockConfig
)
expect(response.status).toBe(202)
const payload: { result: { jobId: string } } = await response.json()
expect(payload.result.jobId).toBe('job-1')
})
it('returns 404 when status is requested for unknown job', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
const response = await getDumpJobStatusRoute(
'missing-job',
mockDataSource,
mockConfig
)
expect(response.status).toBe(404)
})
it('blocks download until job is completed', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{
job_id: 'job-2',
status: 'running',
error_message: null,
file_name: 'database_dump.sql',
batch_size: 500,
table_index: 0,
row_offset: 0,
schema_written: 0,
chunk_index: 1,
tables_json: '[]',
created_at: Date.now(),
updated_at: Date.now(),
completed_at: null,
},
])
const response = await downloadDumpJobRoute(
'job-2',
mockDataSource,
mockConfig
)
expect(response.status).toBe(409)
})
it('streams completed dump content', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{
job_id: 'job-3',
status: 'completed',
error_message: null,
file_name: 'database_dump.sql',
batch_size: 500,
table_index: 1,
row_offset: 0,
schema_written: 0,
chunk_index: 3,
tables_json: '["users"]',
created_at: Date.now(),
updated_at: Date.now(),
completed_at: Date.now(),
},
])
.mockResolvedValueOnce([
{ content: 'SQLite format 3\0' },
{ content: "\nINSERT INTO \"users\" VALUES (1, 'Alice');\n" },
])
.mockResolvedValueOnce([])
const response = await downloadDumpJobRoute(
'job-3',
mockDataSource,
mockConfig
)
expect(response.status).toBe(200)
expect(response.headers.get('Content-Type')).toBe('application/x-sqlite3')
const text = await response.text()
expect(text).toContain('SQLite format 3\0')
expect(text).toContain('INSERT INTO "users" VALUES (1, \'Alice\');')
})
})