-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexport-service.test.ts
More file actions
139 lines (127 loc) · 4.37 KB
/
Copy pathexport-service.test.ts
File metadata and controls
139 lines (127 loc) · 4.37 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import type { IExportService } from './export-service';
describe('Export Service Contract', () => {
it('should allow a minimal IExportService implementation with all required methods', () => {
const service: IExportService = {
createExportJob: async () => ({
jobId: 'job_001',
status: 'pending',
createdAt: new Date().toISOString(),
}),
getExportJobProgress: async () => null,
getExportJobDownload: async () => null,
cancelExportJob: async () => false,
listExportJobs: async () => ({ jobs: [], hasMore: false }),
scheduleExport: async () => ({
name: 'test_schedule',
object: 'account',
schedule: { cronExpression: '0 0 * * *', timezone: 'UTC' },
delivery: { method: 'storage' },
enabled: true,
}),
};
expect(typeof service.createExportJob).toBe('function');
expect(typeof service.getExportJobProgress).toBe('function');
expect(typeof service.getExportJobDownload).toBe('function');
expect(typeof service.cancelExportJob).toBe('function');
expect(typeof service.listExportJobs).toBe('function');
expect(typeof service.scheduleExport).toBe('function');
});
it('should create and track an export job', async () => {
const jobs = new Map<string, any>();
let counter = 0;
const service: IExportService = {
createExportJob: async (input) => {
const jobId = `job_${++counter}`;
const job = {
jobId,
status: 'pending' as const,
estimatedRecords: input.limit,
createdAt: new Date().toISOString(),
};
jobs.set(jobId, { ...job, input });
return job;
},
getExportJobProgress: async (jobId) => {
const job = jobs.get(jobId);
if (!job) return null;
return {
success: true,
data: {
jobId,
status: job.status,
format: job.input.format ?? 'csv',
processedRecords: 0,
percentComplete: 0,
},
};
},
getExportJobDownload: async () => null,
cancelExportJob: async (jobId) => {
if (jobs.has(jobId)) {
jobs.get(jobId).status = 'cancelled';
return true;
}
return false;
},
listExportJobs: async () => ({
jobs: Array.from(jobs.values()).map((j) => ({
jobId: j.jobId,
object: j.input.object,
status: j.status,
format: j.input.format ?? 'csv',
createdAt: j.createdAt,
})),
hasMore: false,
}),
scheduleExport: async () => ({
name: 'test',
object: 'account',
schedule: { cronExpression: '0 0 * * *', timezone: 'UTC' },
delivery: { method: 'storage' },
enabled: true,
}),
};
const result = await service.createExportJob({
object: 'account',
format: 'csv',
fields: ['name', 'email'],
limit: 1000,
});
expect(result.jobId).toBe('job_1');
expect(result.status).toBe('pending');
const progress = await service.getExportJobProgress(result.jobId);
expect(progress).not.toBeNull();
expect(progress!.data.jobId).toBe('job_1');
const list = await service.listExportJobs();
expect(list.jobs).toHaveLength(1);
expect(list.hasMore).toBe(false);
const cancelled = await service.cancelExportJob(result.jobId);
expect(cancelled).toBe(true);
});
it('should support optional template methods', () => {
const service: IExportService = {
createExportJob: async () => ({
jobId: 'job_1',
status: 'pending',
createdAt: new Date().toISOString(),
}),
getExportJobProgress: async () => null,
getExportJobDownload: async () => null,
cancelExportJob: async () => false,
listExportJobs: async () => ({ jobs: [], hasMore: false }),
scheduleExport: async () => ({
name: 'test',
object: 'account',
schedule: { cronExpression: '0 0 * * *', timezone: 'UTC' },
delivery: { method: 'storage' },
enabled: true,
}),
getTemplate: async () => null,
listTemplates: async () => [],
};
expect(typeof service.getTemplate).toBe('function');
expect(typeof service.listTemplates).toBe('function');
});
});