: asapien/signet

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 
// Development-only browser transport regression; use a disposable dev stack.
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { mkdir, writeFile } from 'node:fs/promises';
const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright-core');
const base = process.env.SIGNET_URL || 'http://localhost:8080';
const output = process.env.SIGNET_EVIDENCE || '/tmp/signet-upload-browser';
await mkdir(output, { recursive: true });

// Valid two-page PDF with a large unused stream, exactly the accepted file limit.
function pdf(padding) {
  const text = (s) => `BT /F1 18 Tf 72 770 Td (${s}) Tj ET`;
  const a = text('Upload contract - page 1'), b = text('Upload contract - page 2');
  const page = (n) => `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 7 0 R >> >> /Contents ${n} 0 R >>`;
  const objects = ['<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>', page(5), page(6), `<< /Length ${a.length} >>\nstream\n${a}\nendstream`, `<< /Length ${b.length} >>\nstream\n${b}\nendstream`, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>'];
  let head = '%PDF-1.4\n', offsets = [];
  for (const [i, object] of objects.entries()) { offsets.push(head.length); head += `${i + 1} 0 obj\n${object}\nendobj\n`; }
  offsets.push(head.length);
  head += `8 0 obj\n<< /Length ${padding} >>\nstream\n`;
  const end = '\nendstream\nendobj\n';
  const xref = head.length + padding + end.length;
  const tail = `xref\n0 9\n0000000000 65535 f \n${offsets.map((n) => `${String(n).padStart(10, '0')} 00000 n \n`).join('')}trailer\n<< /Size 9 /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF\n`;
  return Buffer.concat([Buffer.from(head), Buffer.alloc(padding), Buffer.from(end + tail)]);
}
const limit = 32 * 1024 * 1024;
let padding = limit - pdf(0).length, bytes = pdf(padding);
while (bytes.length !== limit) { padding += limit - bytes.length; bytes = pdf(padding); }
const fixture = output + '/max.pdf';
await writeFile(fixture, bytes);
const digest = (value) => createHash('sha256').update(value).digest('hex');
const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: true });
const errors = [];
try {
  const context = await browser.newContext({ viewport: { width: 1200, height: 900 } });
  await context.setExtraHTTPHeaders({ 'Remote-User': 'asha', 'Remote-Groups': 'clerks', 'X-Requested-With': 'signet' });
  const page = await context.newPage();
  page.on('pageerror', (e) => errors.push(e.message));
  let release, begun, heldId, held = false;
  const gate = new Promise((resolve) => { release = resolve; });
  const started = new Promise((resolve) => { begun = resolve; });
  await page.route('**/api/docs/*/upload', async (route) => {
    if (!held) { held = true; heldId = /docs\/([^/]+)\/upload/.exec(route.request().url())[1]; begun(); await gate; }
    await route.continue();
  });
  await page.goto(base + '/#/');
  await page.getByRole('button', { name: 'New document', exact: true }).waitFor();
  await page.locator('input[type=file]').setInputFiles(fixture);
  await page.getByRole('button', { name: 'New document', exact: true }).click();
  let timer;
  try { await Promise.race([started, new Promise((_, reject) => { timer = setTimeout(() => reject(new Error('upload gate was not reached')), 30000); })]); }
  finally { clearTimeout(timer); }
  assert(await page.getByRole('button', { name: 'New document', exact: true }).isDisabled());
  assert(await page.getByRole('progressbar', { name: 'PDF upload progress' }).isVisible());
  assert.equal(await page.locator('progress').evaluate((p) => p.value), 262144);
  const partial = await (await context.request.get(base + '/api/docs/' + heldId)).json();
  assert(partial.uploading && !partial.can_edit);
  assert.equal((await context.request.get(base + `/api/docs/${heldId}/file/source`)).status(), 404);
  await page.screenshot({ path: output + '/progress.png' });
  release();
  await page.waitForURL('**/#/doc/' + heldId, { timeout: 90000 });
  await page.waitForFunction(() => document.querySelectorAll('.page img').length === 2 && [...document.querySelectorAll('.page img')].every((i) => i.complete && i.naturalWidth));
  const source = await context.request.get(base + `/api/docs/${heldId}/file/source`);
  assert.equal(digest(await source.body()), digest(bytes));
  await page.screenshot({ path: output + '/complete.png' });
  // A non-retryable chunk failure exposes only the safe incomplete-draft view.
  let failedId;
  await page.unroute('**/api/docs/*/upload');
  await page.route('**/api/docs/*/upload', async (route) => {
    failedId = /docs\/([^/]+)\/upload/.exec(route.request().url())[1];
    await route.fulfill({ status: 409, contentType: 'application/json', body: '{"error":"fixture chunk conflict"}' });
  });
  await page.goto(base + '/#/');
  await page.getByRole('button', { name: 'New document', exact: true }).waitFor();
  await page.locator('input[type=file]').setInputFiles(fixture);
  await page.getByRole('button', { name: 'New document', exact: true }).click();
  await page.getByRole('heading', { name: 'Upload incomplete', exact: true }).waitFor();
  assert.equal(await page.locator('.page').count(), 0);
  await page.screenshot({ path: output + '/incomplete.png' });
  await page.getByRole('button', { name: 'Delete incomplete draft', exact: true }).click();
  await page.getByRole('button', { name: 'New document', exact: true }).waitFor();
  assert.equal((await context.request.get(base + '/api/docs/' + failedId)).status(), 404);
  assert.deepEqual(errors, []);
  console.log(JSON.stringify({ result: 'passed', max_bytes: bytes.length, source_sha256: digest(bytes), id: heldId, two_pages: true, incomplete_delete: true }));
} finally { await browser.close(); }