merge(app): land artifact download/share after a retained deadline
This commit is contained in:
@@ -121,6 +121,24 @@ List<JsonMap> _steps(String origin, String state) {
|
||||
},
|
||||
];
|
||||
}
|
||||
if (state == 'files') {
|
||||
return <JsonMap>[
|
||||
<String, dynamic>{
|
||||
'stage': 'reviewer',
|
||||
'sequence': 1,
|
||||
'status': 'completed',
|
||||
'control': <String, dynamic>{'visit': 1},
|
||||
'artifacts': [
|
||||
_file(
|
||||
'report.csv',
|
||||
url: '$origin/fixture/report.csv',
|
||||
contentType: 'text/csv',
|
||||
sizeBytes: 23,
|
||||
),
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
return <JsonMap>[
|
||||
<String, dynamic>{
|
||||
'stage': 'implementer',
|
||||
|
||||
@@ -91,6 +91,7 @@ const probePng = () => {
|
||||
]);
|
||||
};
|
||||
const png = probePng();
|
||||
const probeCsv = Buffer.from('id,name\n1,alpha\n2,beta\n');
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
|
||||
@@ -100,6 +101,14 @@ const server = createServer(async (req, res) => {
|
||||
res.end('missing artifact\n');
|
||||
return;
|
||||
}
|
||||
if (pathname.endsWith('/report.csv')) {
|
||||
res.writeHead(200, {
|
||||
'content-type': 'text/csv',
|
||||
'content-length': probeCsv.length,
|
||||
});
|
||||
res.end(probeCsv);
|
||||
return;
|
||||
}
|
||||
if (pathname.endsWith('.png')) {
|
||||
res.writeHead(200, { 'content-type': 'image/png' });
|
||||
res.end(png);
|
||||
@@ -179,6 +188,49 @@ try {
|
||||
})).result.sessionId;
|
||||
await send('Page.enable', {}, session);
|
||||
await send('Runtime.enable', {}, session);
|
||||
// Record the real bytes the app hands to the OS. The download anchor's
|
||||
// click is swallowed so headless Chrome never writes into the profile; the
|
||||
// share function replaces the browser sheet with a recorder.
|
||||
await send('Page.addScriptToEvaluateOnNewDocument', {
|
||||
source: `(() => {
|
||||
const state = { objects: [], downloads: [], shares: [] };
|
||||
window.__artifactTransfer = state;
|
||||
const realCreate = URL.createObjectURL.bind(URL);
|
||||
URL.createObjectURL = (blob) => {
|
||||
const url = realCreate(blob);
|
||||
const entry = { url, type: blob.type, size: blob.size, name: null, bytes: null };
|
||||
state.objects.push(entry);
|
||||
blob.arrayBuffer().then((buffer) => {
|
||||
entry.bytes = Array.from(new Uint8Array(buffer));
|
||||
});
|
||||
return url;
|
||||
};
|
||||
HTMLAnchorElement.prototype.click = function () {
|
||||
state.downloads.push({ name: this.download, href: this.href });
|
||||
};
|
||||
const navProto = Object.getPrototypeOf(navigator);
|
||||
Object.defineProperty(navProto, 'share', {
|
||||
configurable: true,
|
||||
value: async (data) => {
|
||||
state.shares.push((data?.files || []).map((file) => {
|
||||
const entry = { name: file.name, type: file.type, size: file.size, bytes: null };
|
||||
file.arrayBuffer().then((buffer) => {
|
||||
entry.bytes = Array.from(new Uint8Array(buffer));
|
||||
});
|
||||
return entry;
|
||||
}));
|
||||
},
|
||||
});
|
||||
Object.defineProperty(navProto, 'canShare', {
|
||||
configurable: true,
|
||||
value: (data) => Array.isArray(data?.files) && data.files.length > 0,
|
||||
});
|
||||
Object.defineProperty(navProto, 'userActivation', {
|
||||
configurable: true,
|
||||
get: () => ({ isActive: true, hasBeenActive: true }),
|
||||
});
|
||||
})();`,
|
||||
}, session);
|
||||
await send('Browser.grantPermissions', {
|
||||
origin,
|
||||
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
|
||||
@@ -255,6 +307,23 @@ try {
|
||||
(await send('Target.getTargets')).result.targetInfos.filter(
|
||||
(target) => target.type === 'page',
|
||||
).length;
|
||||
const transferState = () =>
|
||||
evaluate(`(() => {
|
||||
const state = window.__artifactTransfer;
|
||||
return {
|
||||
downloads: state.downloads,
|
||||
objects: state.objects.map(({ url, type, size, name, bytes }) => ({ url, type, size, name, bytes })),
|
||||
shares: state.shares,
|
||||
};
|
||||
})()`);
|
||||
const waitForBytes = async (read) => {
|
||||
for (let i = 0; i < 40; i++) {
|
||||
const value = await read();
|
||||
if (value?.byteLength) return value;
|
||||
await delay(100);
|
||||
}
|
||||
throw new Error('Timed out waiting for transfer bytes');
|
||||
};
|
||||
const capture = async (name, width = 390, height = 844, scale = 1) => {
|
||||
await send('Emulation.setDeviceMetricsOverride', {
|
||||
width, height, deviceScaleFactor: scale, mobile: width < 600,
|
||||
@@ -591,6 +660,110 @@ try {
|
||||
await openScaled('cupertino', 'a1w-cupertino-2x-320');
|
||||
evidence.checks.push({ check: 'Cupertino 2x/320 keeps the recorded rows, facts and actions', value: true });
|
||||
|
||||
// File actions: a real same-origin artifact is fetched, then handed to the
|
||||
// host as a named file. Blob/anchor and navigator.share are intercepted above
|
||||
// so the evidence is the exact bytes, MIME type and download name, not a
|
||||
// screenshot of a promise.
|
||||
const sameBytes = (actual, expected) =>
|
||||
Array.isArray(actual) &&
|
||||
actual.length === expected.length &&
|
||||
actual.every((value, index) => value === expected[index]);
|
||||
const fileProbe = async (platform, prefix, extra = '') => {
|
||||
await send('Emulation.setDeviceMetricsOverride', {
|
||||
width: 390, height: 844, deviceScaleFactor: 1, mobile: true,
|
||||
}, session);
|
||||
const before = await targetCount();
|
||||
await navigate(galleryView(`&state=files${extra}`, platform), 'Artifacts');
|
||||
await clickExact('report.csv');
|
||||
await delay(500);
|
||||
if (!(await hasLabel('Download')) || !(await hasLabel('Share'))) {
|
||||
throw new Error(`${platform} selection lost its file actions: ${JSON.stringify(await labels())}`);
|
||||
}
|
||||
await capture(`${prefix}-files-selected`);
|
||||
|
||||
await clickExact('Download');
|
||||
let downloaded;
|
||||
try {
|
||||
downloaded = await waitFor(async () => {
|
||||
const state = await transferState();
|
||||
return state.objects[0]?.bytes ? state : null;
|
||||
}, `${platform} download bytes`);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${error.message}\nstate=${JSON.stringify(await transferState())}\nbody=${JSON.stringify(await bodyText())}`,
|
||||
);
|
||||
}
|
||||
const download = downloaded.downloads[0];
|
||||
const object = downloaded.objects[0];
|
||||
if (downloaded.downloads.length !== 1) {
|
||||
throw new Error(`${platform} did not request exactly one download: ${JSON.stringify(downloaded)}`);
|
||||
}
|
||||
if (download.name !== 'report.csv') {
|
||||
throw new Error(`${platform} download name was ${JSON.stringify(download.name)}`);
|
||||
}
|
||||
if (!download.href.startsWith('blob:')) {
|
||||
throw new Error(`${platform} download used a non-blob href: ${JSON.stringify(download.href)}`);
|
||||
}
|
||||
if (object.type !== 'text/csv' || object.size !== probeCsv.length) {
|
||||
throw new Error(`${platform} blob was ${object.type}/${object.size}, expected text/csv/${probeCsv.length}`);
|
||||
}
|
||||
if (!sameBytes(object.bytes, Array.from(probeCsv))) {
|
||||
throw new Error(`${platform} download bytes differed from the served artifact`);
|
||||
}
|
||||
if (!(await bodyText()).includes('Downloaded report.csv.')) {
|
||||
throw new Error(`${platform} download did not report its file: ${JSON.stringify(await bodyText())}`);
|
||||
}
|
||||
evidence.checks.push({
|
||||
check: `${platform} Download hands the host report.csv, text/csv, ${probeCsv.length} exact bytes`,
|
||||
value: true,
|
||||
name: download.name,
|
||||
mime: object.type,
|
||||
size: object.size,
|
||||
blob: download.href.startsWith('blob:'),
|
||||
});
|
||||
await capture(`${prefix}-files-downloaded`);
|
||||
|
||||
await clickExact('Share');
|
||||
let shared;
|
||||
try {
|
||||
shared = await waitFor(async () => {
|
||||
const state = await transferState();
|
||||
return state.shares[0]?.[0]?.bytes ? state : null;
|
||||
}, `${platform} share file`);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${error.message}\nstate=${JSON.stringify(await transferState())}\nbody=${JSON.stringify(await bodyText())}`,
|
||||
);
|
||||
}
|
||||
const file = shared.shares[0]?.[0];
|
||||
if (shared.shares.length !== 1 || shared.shares[0].length !== 1) {
|
||||
throw new Error(`${platform} share received ${JSON.stringify(shared.shares)}`);
|
||||
}
|
||||
if (file.name !== 'report.csv' || file.type !== 'text/csv' || file.size !== probeCsv.length) {
|
||||
throw new Error(`${platform} shared file was ${JSON.stringify(file)}`);
|
||||
}
|
||||
if (!sameBytes(file.bytes, Array.from(probeCsv))) {
|
||||
throw new Error(`${platform} shared bytes differed from the served artifact`);
|
||||
}
|
||||
if (!(await bodyText()).includes('File shared.')) {
|
||||
throw new Error(`${platform} share did not report success: ${JSON.stringify(await bodyText())}`);
|
||||
}
|
||||
if ((await targetCount()) !== before) {
|
||||
throw new Error(`${platform} file actions opened a browser tab`);
|
||||
}
|
||||
evidence.checks.push({
|
||||
check: `${platform} Share hands the host a report.csv, text/csv, ${probeCsv.length} byte file`,
|
||||
value: true,
|
||||
name: file.name,
|
||||
mime: file.type,
|
||||
size: file.size,
|
||||
});
|
||||
await capture(`${prefix}-files-shared`);
|
||||
};
|
||||
await fileProbe('material', 'a3-material');
|
||||
await fileProbe('cupertino', 'a3-cupertino');
|
||||
await fileProbe('cupertino', 'a3-cupertino-dark', '&theme=dark');
|
||||
|
||||
console.log(JSON.stringify(evidence, null, 2));
|
||||
} finally {
|
||||
ws?.close();
|
||||
|
||||
Reference in New Issue
Block a user