-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextension-integration.test.ts
More file actions
139 lines (113 loc) · 5.24 KB
/
Copy pathextension-integration.test.ts
File metadata and controls
139 lines (113 loc) · 5.24 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
/**
* Integration tests for browser extension backend communication
*
* These tests verify that the backend correctly handles requests from the browser extension:
* - GET /list-docs: Retrieve archived documents
* - POST /form: Archive a webpage with screenshot and HTML
*/
import supertest from 'supertest';
import { ImportMock } from 'ts-mock-imports';
import app from './app';
import * as Ledger from './src/ledger';
import * as Store from './src/store';
import * as Bundle from './src/types/Bundle';
describe('Browser Extension Integration Tests', () => {
afterEach(() => {
ImportMock.restore();
});
describe('GET /list-docs', () => {
it('should return list of archived documents', async () => {
const mockDocs = [
{
id: 'doc-123',
data: { url: 'https://example.com', title: 'Example Page' },
annotations: { description: 'Test document' },
},
{
id: 'doc-456',
data: { url: 'https://test.com', title: 'Test Page' },
annotations: { description: 'Another test' },
},
];
ImportMock.mockFunction(Ledger, 'listDocs', Promise.resolve(mockDocs));
const response = await supertest(app).get('/list-docs');
expect(response.status).toBe(200);
expect(response.text).toContain('doc-123');
expect(response.text).toContain('example.com');
});
it('should return empty list when no documents exist', async () => {
ImportMock.mockFunction(Ledger, 'listDocs', Promise.resolve([]));
const response = await supertest(app).get('/list-docs');
expect(response.status).toBe(200);
expect(response.text).toBe('[]');
});
});
describe('POST /form', () => {
it('should accept and process valid webpage archive from extension', async () => {
const mockBundle: Bundle.Bundle = [
{ kind: 'screenshot', hash: 'hash-screenshot-123' },
{ kind: 'screenshot_thumbnail', hash: 'hash-thumb-123' },
{ kind: 'one_file', hash: 'hash-html-123' },
];
ImportMock.mockFunction(Store, 'newBundle', Promise.resolve(mockBundle));
ImportMock.mockFunction(Ledger, 'insertDoc', Promise.resolve(undefined));
// Create a minimal base64 PNG (1x1 transparent pixel)
const minimalPng = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
const response = await supertest(app)
.post('/form')
.field('url', 'https://example.com/test-page')
.field('title', 'Test Page Title')
.field('scr', `data:image/png;base64,${minimalPng}`)
.field('onefile', '<html><body>Archived page content</body></html>');
expect(response.status).toBe(200);
expect(response.text).toBe('Received POST on /form');
});
it('should accept multipart form data with all required fields', async () => {
const mockBundle: Bundle.Bundle = [
{ kind: 'screenshot', hash: 'hash-abc' },
{ kind: 'screenshot_thumbnail', hash: 'hash-def' },
{ kind: 'one_file', hash: 'hash-ghi' },
];
ImportMock.mockFunction(Store, 'newBundle', Promise.resolve(mockBundle));
ImportMock.mockFunction(Ledger, 'insertDoc', Promise.resolve(undefined));
const minimalPng = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
const response = await supertest(app)
.post('/form')
.field('url', 'https://another-example.com')
.field('title', 'Another Test Page')
.field('scr', `data:image/png;base64,${minimalPng}`)
.field('onefile', '<html><head><title>Test</title></head><body><h1>Content</h1></body></html>');
expect(response.status).toBe(200);
expect(response.text).toBe('Received POST on /form');
});
});
describe('Extension Communication Flow', () => {
it('should handle typical extension workflow', async () => {
// Step 1: Extension checks document list before archiving
const initialDocs = [
{ id: 'existing-doc', data: { url: 'https://old.com', title: 'Old Doc' } },
];
ImportMock.mockFunction(Ledger, 'listDocs', Promise.resolve(initialDocs));
const listResponse = await supertest(app).get('/list-docs');
expect(listResponse.status).toBe(200);
expect(listResponse.text).toContain('old.com');
// Step 2: Extension archives a new page
const mockBundle: Bundle.Bundle = [
{ kind: 'screenshot', hash: 'new-screenshot' },
{ kind: 'screenshot_thumbnail', hash: 'new-thumb' },
{ kind: 'one_file', hash: 'new-html' },
];
ImportMock.mockFunction(Store, 'newBundle', Promise.resolve(mockBundle));
ImportMock.mockFunction(Ledger, 'insertDoc', Promise.resolve(undefined));
const minimalPng = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
const archiveResponse = await supertest(app)
.post('/form')
.field('url', 'https://new-page.com')
.field('title', 'New Page')
.field('scr', `data:image/png;base64,${minimalPng}`)
.field('onefile', '<html><body>New content</body></html>');
expect(archiveResponse.status).toBe(200);
expect(archiveResponse.text).toBe('Received POST on /form');
});
});
});