105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { existsSync } from 'node:fs';
|
|
import { dirname, resolve } from 'node:path';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { expect, test } from './support/test.ts';
|
|
|
|
function visitCount(path: string, logicalPath: string): number {
|
|
if (!existsSync(path)) return 0;
|
|
const db = new DatabaseSync(path, { readOnly: true });
|
|
try {
|
|
const row = db
|
|
.prepare('SELECT count(*) AS count FROM visits WHERE domain = ? AND path = ?')
|
|
.get('go.example.org', logicalPath);
|
|
return Number(row?.count ?? 0);
|
|
} catch {
|
|
return 0;
|
|
} finally {
|
|
db.close();
|
|
}
|
|
}
|
|
|
|
test('the compiled writer persists outcomes, isolates locks and drains visits before shutdown', async ({
|
|
request,
|
|
appServer
|
|
}) => {
|
|
const databasePath = resolve(dirname(appServer.configurationPath), '../data/goto.sqlite');
|
|
const options = {
|
|
headers: {
|
|
host: 'go.example.org',
|
|
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:158.0) Gecko/20100101 Firefox/158.0'
|
|
},
|
|
maxRedirects: 0,
|
|
timeout: 2000
|
|
};
|
|
await expect
|
|
.poll(
|
|
async () => {
|
|
await request.get('/healthz', options);
|
|
return visitCount(databasePath, '/healthz');
|
|
},
|
|
{ timeout: 15_000 }
|
|
)
|
|
.toBeGreaterThan(0);
|
|
|
|
const conditionalOptions = {
|
|
...options,
|
|
headers: { ...options.headers, 'accept-language': 'fr-CA,en;q=0.9' }
|
|
};
|
|
const competingWriter = new DatabaseSync(databasePath);
|
|
try {
|
|
competingWriter.exec('PRAGMA busy_timeout = 1000');
|
|
competingWriter.exec('BEGIN IMMEDIATE');
|
|
for (const [path, destination, requestOptions] of [
|
|
['/docs', 'https://example.org/first-domain', options],
|
|
['/DOCS/', 'https://example.org/french-linux', conditionalOptions],
|
|
[
|
|
'/DoCs/Install?from=browser',
|
|
'https://example.org/first-domain/Install?from=browser',
|
|
options
|
|
]
|
|
] as const) {
|
|
const response = await request.get(path, requestOptions);
|
|
expect(response.status()).toBe(302);
|
|
expect(response.headers().location).toBe(destination);
|
|
}
|
|
expect(visitCount(databasePath, '/docs')).toBe(0);
|
|
} finally {
|
|
competingWriter.exec('ROLLBACK');
|
|
competingWriter.close();
|
|
}
|
|
const invalid = await request.get('/missing', options);
|
|
const base = await request.get('/', options);
|
|
const head = await request.head('/missing', options);
|
|
expect(invalid.status()).toBe(404);
|
|
expect(base.status()).toBe(404);
|
|
expect(head.status()).toBe(404);
|
|
expect(await appServer.stop()).toEqual({ code: 0, signal: null });
|
|
expect(visitCount(databasePath, '/docs')).toBe(3);
|
|
const db = new DatabaseSync(databasePath, { readOnly: true });
|
|
try {
|
|
expect(
|
|
db.prepare('SELECT DISTINCT outcome, browser, os FROM visits WHERE path = ?').all('/docs')
|
|
).toEqual([{ outcome: 'valid', browser: 'Firefox', os: 'Linux' }]);
|
|
expect(
|
|
db
|
|
.prepare(
|
|
'SELECT accept_language FROM visits WHERE path = ? AND accept_language IS NOT NULL'
|
|
)
|
|
.all('/docs')
|
|
).toEqual([{ accept_language: 'fr-CA,en;q=0.9' }]);
|
|
expect(
|
|
db
|
|
.prepare(
|
|
'SELECT path, outcome FROM visits WHERE domain = ? AND path IN (?, ?) ORDER BY path'
|
|
)
|
|
.all('go.example.org', '/', '/missing')
|
|
).toEqual([
|
|
{ path: '/', outcome: 'valid' },
|
|
{ path: '/missing', outcome: 'invalid' }
|
|
]);
|
|
} finally {
|
|
db.close();
|
|
}
|
|
expect(appServer.logs()).not.toContain('Visit tracking unavailable');
|
|
});
|