-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathbrowserbase.service.spec.ts
More file actions
128 lines (108 loc) · 3.93 KB
/
Copy pathbrowserbase.service.spec.ts
File metadata and controls
128 lines (108 loc) · 3.93 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
// apps/api/src/browserbase/browserbase.service.spec.ts
import { Test } from '@nestjs/testing';
import { NotFoundException } from '@nestjs/common';
import { BrowserbaseService } from './browserbase.service';
jest.mock('@db', () => ({
db: {
browserAutomationRun: {
findUnique: jest.fn(),
},
},
}));
jest.mock('@/app/s3', () => ({
getSignedUrl: jest.fn().mockResolvedValue('https://s3.example.com/signed'),
s3Client: { send: jest.fn() },
BUCKET_NAME: 'test-bucket',
}));
import { db } from '@db';
import { getSignedUrl } from '@/app/s3';
describe('BrowserbaseService.getScreenshotRedirectUrl', () => {
let service: BrowserbaseService;
beforeEach(async () => {
jest.clearAllMocks();
const moduleRef = await Test.createTestingModule({
providers: [BrowserbaseService],
}).compile();
service = moduleRef.get(BrowserbaseService);
});
it('returns a freshly minted presigned URL for an in-scope run', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue({
id: 'bar_1',
screenshotUrl: 'browser-automations/org_1/bau_1/bar_1.jpg',
automation: { task: { organizationId: 'org_1' } },
});
const url = await service.getScreenshotRedirectUrl({
runId: 'bar_1',
organizationId: 'org_1',
});
expect(url).toBe('https://s3.example.com/signed');
expect(db.browserAutomationRun.findUnique).toHaveBeenCalledWith({
where: { id: 'bar_1' },
include: { automation: { include: { task: true } } },
});
});
it('throws NotFoundException when the run does not exist', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue(null);
await expect(
service.getScreenshotRedirectUrl({
runId: 'bar_missing',
organizationId: 'org_1',
}),
).rejects.toBeInstanceOf(NotFoundException);
});
it('throws NotFoundException when the run belongs to a different org', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue({
id: 'bar_1',
screenshotUrl: 'browser-automations/org_2/bau_1/bar_1.jpg',
automation: { task: { organizationId: 'org_2' } },
});
await expect(
service.getScreenshotRedirectUrl({
runId: 'bar_1',
organizationId: 'org_1',
}),
).rejects.toBeInstanceOf(NotFoundException);
});
it('throws NotFoundException when the run has no screenshot', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue({
id: 'bar_1',
screenshotUrl: null,
automation: { task: { organizationId: 'org_1' } },
});
await expect(
service.getScreenshotRedirectUrl({
runId: 'bar_1',
organizationId: 'org_1',
}),
).rejects.toBeInstanceOf(NotFoundException);
});
it('signs the URL without Content-Disposition when download is falsy', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue({
id: 'bar_1',
screenshotUrl: 'browser-automations/org_1/bau_1/bar_1.jpg',
automation: { task: { organizationId: 'org_1' } },
});
await service.getScreenshotRedirectUrl({
runId: 'bar_1',
organizationId: 'org_1',
});
const command = (getSignedUrl as jest.Mock).mock.calls[0][1];
expect(command.input.ResponseContentDisposition).toBeUndefined();
});
it('signs the URL with attachment Content-Disposition when download is true', async () => {
(db.browserAutomationRun.findUnique as jest.Mock).mockResolvedValue({
id: 'bar_1',
screenshotUrl: 'browser-automations/org_1/bau_1/bar_1.jpg',
automation: { task: { organizationId: 'org_1' } },
});
await service.getScreenshotRedirectUrl({
runId: 'bar_1',
organizationId: 'org_1',
download: true,
});
const command = (getSignedUrl as jest.Mock).mock.calls[0][1];
expect(command.input.ResponseContentDisposition).toBe(
'attachment; filename="screenshot-bar_1.jpg"',
);
});
});