eb0f12b8cf
A least-privilege key — PutObject / GetObject and no ListBucket — gets a 403
from HeadBucket, and reading that as "the bucket is missing" turned a working
key into a 503. The bucket may well be there and simply not be the key's
business, so a refusal to look at it is no longer a failure; the PUT that
follows is the honest test.
`scripts/check_storage.py` covers what a smoke test cannot, because these are
the rules whose failure is silent: the format read from the magic bytes rather
than the client's word, the key pattern the read route serves and nothing
outside it, a caption that cannot break out of the Markdown it is written into,
what is actually asked of MinIO on a write, and the refusals — unconfigured
storage, a key we never wrote, an object that is gone. boto3's client is a stub,
so it needs no credentials, no bucket and no network.
`frontend/scripts/check-image.mjs` is the third layer: bytes can be stored and
returned perfectly while the `<img>` draws nothing. It builds its own PNG
fixture, drives the page and measures what the browser did — the picture
decoded, the figure is centred on the column, `{width=50%}` is half the column,
the 图注 prints under the picture, an image inside a sentence stays inline, and
「插入图片」 ends with the picture in the paper. Run with `pnpm check:image`; it
needs Playwright, which stays out of the app's dependencies.
281 lines
11 KiB
JavaScript
281 lines
11 KiB
JavaScript
/**
|
|
* Does an inserted picture actually show?
|
|
*
|
|
* pnpm check:image # API on :8000 and the web app on :5173
|
|
* PAPER_WEB=http://… pnpm check:image
|
|
*
|
|
* This is the check `backend/scripts/smoke_images.py` cannot make. That one
|
|
* proves the endpoints store and return the bytes; this one proves the page
|
|
* *draws* them: that the `<img>` really decoded (`naturalWidth`), that a figure
|
|
* took a line of its own and is centred on the column, that `{width=50%}` came
|
|
* out at half the column, that the 图注 is printed under the picture rather than
|
|
* beside it, that an image inside a sentence stayed in that line, and that the
|
|
* editor's 「插入图片」 button ends with the picture in the paper after saving.
|
|
*
|
|
* It creates a throwaway paper for the run and deletes it afterwards, so it can
|
|
* be pointed at a database that holds real work.
|
|
*
|
|
* It needs Playwright, which is deliberately not a dependency of the app:
|
|
*
|
|
* pnpm add -D playwright && npx playwright install chromium
|
|
*
|
|
* An install that lives elsewhere can be pointed at instead, which is how this
|
|
* was verified on the machine that runs the paper:
|
|
*
|
|
* PLAYWRIGHT_MODULE=/path/to/node_modules/playwright/index.mjs pnpm check:image
|
|
*/
|
|
import { deflateSync } from 'node:zlib'
|
|
|
|
const WEB = process.env.PAPER_WEB ?? 'http://127.0.0.1:5173'
|
|
const API = process.env.PAPER_API ?? 'http://127.0.0.1:8000/api'
|
|
|
|
let chromium
|
|
try {
|
|
;({ chromium } = await import(process.env.PLAYWRIGHT_MODULE ?? 'playwright'))
|
|
} catch {
|
|
console.error(
|
|
'playwright is not installed. Either install it:\n' +
|
|
' pnpm add -D playwright && npx playwright install chromium\n' +
|
|
'or point at an existing install with PLAYWRIGHT_MODULE=…/playwright/index.mjs',
|
|
)
|
|
process.exit(2)
|
|
}
|
|
|
|
// --- a real PNG, built here so the script carries its own fixture ------------
|
|
|
|
const CRC_TABLE = Array.from({ length: 256 }, (_, index) => {
|
|
let value = index
|
|
for (let bit = 0; bit < 8; bit += 1) {
|
|
value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1
|
|
}
|
|
return value >>> 0
|
|
})
|
|
|
|
function crc32(buffer) {
|
|
let crc = 0xffffffff
|
|
for (const byte of buffer) crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8)
|
|
return (crc ^ 0xffffffff) >>> 0
|
|
}
|
|
|
|
function chunk(tag, payload) {
|
|
const body = Buffer.concat([Buffer.from(tag, 'ascii'), payload])
|
|
const length = Buffer.alloc(4)
|
|
length.writeUInt32BE(payload.length)
|
|
const crc = Buffer.alloc(4)
|
|
crc.writeUInt32BE(crc32(body))
|
|
return Buffer.concat([length, body, crc])
|
|
}
|
|
|
|
/** A gradient, so two fixtures differ and the size is honest. */
|
|
function makePng(width = 480, height = 300) {
|
|
const raw = Buffer.alloc(height * (1 + width * 3))
|
|
let offset = 0
|
|
for (let y = 0; y < height; y += 1) {
|
|
raw[offset] = 0 // filter: none
|
|
offset += 1
|
|
for (let x = 0; x < width; x += 1) {
|
|
raw[offset] = Math.round((x * 255) / Math.max(width - 1, 1))
|
|
raw[offset + 1] = Math.round((y * 255) / Math.max(height - 1, 1))
|
|
raw[offset + 2] = 128
|
|
offset += 3
|
|
}
|
|
}
|
|
|
|
const header = Buffer.alloc(13)
|
|
header.writeUInt32BE(width, 0)
|
|
header.writeUInt32BE(height, 4)
|
|
header[8] = 8 // bit depth
|
|
header[9] = 2 // truecolour
|
|
return Buffer.concat([
|
|
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
chunk('IHDR', header),
|
|
chunk('IDAT', deflateSync(raw, { level: 9 })),
|
|
chunk('IEND', Buffer.alloc(0)),
|
|
])
|
|
}
|
|
|
|
// --- the API, spoken with fetch ---------------------------------------------
|
|
|
|
async function api(method, path, body) {
|
|
const response = await fetch(`${API}${path}`, {
|
|
method,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
})
|
|
if (!response.ok && response.status !== 204) {
|
|
throw new Error(`${method} ${path} -> ${response.status}: ${await response.text()}`)
|
|
}
|
|
return response.status === 204 ? null : response.json()
|
|
}
|
|
|
|
const results = []
|
|
function check(label, ok, detail = '') {
|
|
results.push([label, ok])
|
|
console.log(`${ok ? ' ok ' : ' FAIL'}${label}${ok ? '' : ` — ${detail}`}`)
|
|
}
|
|
|
|
const png = makePng()
|
|
const uploadResponse = await fetch(`${API}/images`, {
|
|
method: 'POST',
|
|
body: (() => {
|
|
const form = new FormData()
|
|
form.append('file', new Blob([png], { type: 'image/png' }), '试验装置.png')
|
|
return form
|
|
})(),
|
|
})
|
|
if (!uploadResponse.ok) {
|
|
throw new Error(`upload -> ${uploadResponse.status}: ${await uploadResponse.text()}`)
|
|
}
|
|
const image = await uploadResponse.json()
|
|
console.log(`uploaded ${image.url} (${image.size} bytes)`)
|
|
|
|
const browser = await chromium.launch()
|
|
let paperId = null
|
|
try {
|
|
const context = await browser.newContext({ viewport: { width: 1600, height: 950 } })
|
|
const page = await context.newPage()
|
|
|
|
const template = (await api('GET', '/templates?page_size=1')).items[0]
|
|
paperId = (await api('POST', '/papers', { title: '插图浏览器测试', template_id: template.id })).id
|
|
const outline = await api('GET', `/templates/${template.id}`)
|
|
const sort = outline.placements[0].sort
|
|
|
|
// One paragraph with an image inside a sentence, and one figure half the
|
|
// column wide. Written through the API so the *rendering* is what is checked.
|
|
await api('PUT', `/papers/${paperId}/paragraphs/${sort}`, {
|
|
sentences: [
|
|
{ content: `试验装置如  所示,量测点布置在试件中部。` },
|
|
{ content: `{width=50%}`, line_break_before: true },
|
|
],
|
|
})
|
|
|
|
await page.goto(`${WEB}/papers/${paperId}`, { waitUntil: 'networkidle' })
|
|
await page.waitForSelector('.paragraph-body')
|
|
// `naturalWidth` is the proof it decoded; give the decode a moment.
|
|
await page.waitForTimeout(800)
|
|
|
|
const geometry = await page.evaluate(() => {
|
|
const body = document.querySelector('.paragraph-body')
|
|
const bodyBox = body.getBoundingClientRect()
|
|
const figure = document.querySelector('.sentence--figure')
|
|
const figureImage = figure.querySelector('img')
|
|
const figureBox = figureImage.getBoundingClientRect()
|
|
const caption = figure.querySelector('.figure-caption')
|
|
const inline = [...document.querySelectorAll('.sentence img')].find(
|
|
(node) => !node.closest('.sentence--figure'),
|
|
)
|
|
const inlineBox = inline.getBoundingClientRect()
|
|
|
|
return {
|
|
bodyWidth: Math.round(bodyBox.width),
|
|
bodyCenter: Math.round(bodyBox.left + bodyBox.width / 2),
|
|
figure: {
|
|
src: figureImage.getAttribute('src'),
|
|
natural: [figureImage.naturalWidth, figureImage.naturalHeight],
|
|
width: Math.round(figureBox.width),
|
|
center: Math.round(figureBox.left + figureBox.width / 2),
|
|
caption: caption.textContent.trim(),
|
|
gapBelowImage: Math.round(caption.getBoundingClientRect().top - figureBox.bottom),
|
|
siblings: figure.closest('.paragraph-line').querySelectorAll('.sentence').length,
|
|
},
|
|
inline: {
|
|
width: Math.round(inlineBox.width),
|
|
height: Math.round(inlineBox.height),
|
|
inFigure: Boolean(inline.closest('.sentence--figure')),
|
|
lineHasText: inline.closest('.paragraph-line').textContent.includes('量测点'),
|
|
},
|
|
}
|
|
})
|
|
|
|
const { figure, inline } = geometry
|
|
check('the picture decoded', figure.natural[0] === 480, JSON.stringify(figure.natural))
|
|
check('it is served from our own path', figure.src.startsWith('/api/images/'), figure.src)
|
|
check(
|
|
'{width=50%} is half the column',
|
|
Math.abs(figure.width - geometry.bodyWidth * 0.5) <= 2,
|
|
`${figure.width} vs ${geometry.bodyWidth * 0.5}`,
|
|
)
|
|
check(
|
|
'the figure is centred on the column',
|
|
Math.abs(figure.center - geometry.bodyCenter) <= 2,
|
|
`${figure.center} vs ${geometry.bodyCenter}`,
|
|
)
|
|
check('the 图注 is the text in the brackets', figure.caption === '试验装置示意图', figure.caption)
|
|
check('…and it is printed under the picture', figure.gapBelowImage > 0, `${figure.gapBelowImage}`)
|
|
check('the figure owns its line', figure.siblings === 1, `${figure.siblings} sentences`)
|
|
check(
|
|
'an image inside a sentence stays inline',
|
|
!inline.inFigure && inline.lineHasText,
|
|
JSON.stringify(inline),
|
|
)
|
|
check(
|
|
'the inline image keeps its aspect ratio',
|
|
Math.abs(inline.width / inline.height - 480 / 300) < 0.02,
|
|
`${inline.width}x${inline.height}`,
|
|
)
|
|
|
|
await page.locator('.sentence--figure img').click()
|
|
await page.waitForTimeout(400)
|
|
check(
|
|
'clicking opens the picture at full size',
|
|
await page.evaluate(() => Boolean(document.querySelector('.el-image-viewer__wrapper'))),
|
|
)
|
|
await page.keyboard.press('Escape')
|
|
await page.waitForTimeout(400)
|
|
|
|
// --- the editor's own 「插入图片」 -----------------------------------------
|
|
// Scoped to a paragraph header: 「编辑信息」 in the toolbar also contains 编辑.
|
|
await page.locator('.paragraph-actions button:has-text("编辑")').first().click()
|
|
await page.waitForSelector('.editor .rows', { timeout: 15000 })
|
|
await page.locator('.editor input[type=file]').setInputFiles({
|
|
name: '第二张图.png',
|
|
mimeType: 'image/png',
|
|
buffer: png,
|
|
})
|
|
|
|
// Wait for the *new* thumbnail: the dialog already shows the two the API
|
|
// wrote, so waiting for any `.row-image img` would return immediately.
|
|
await page.waitForFunction(
|
|
() =>
|
|
[...document.querySelectorAll('.row-image figcaption')].some(
|
|
(node) => node.textContent.trim() === '第二张图',
|
|
),
|
|
{ timeout: 20000 },
|
|
)
|
|
check(
|
|
'the editor shows the uploaded picture',
|
|
(await page.locator('.row-image img').count()) === 3,
|
|
`${await page.locator('.row-image img').count()} thumbnails`,
|
|
)
|
|
check(
|
|
'the row caption comes from the filename',
|
|
(await page.locator('.row-image figcaption', { hasText: '第二张图' }).first().textContent()).trim() ===
|
|
'第二张图',
|
|
)
|
|
|
|
await page.getByRole('button', { name: '保存段落' }).click()
|
|
await page.waitForSelector('.el-dialog', { state: 'detached', timeout: 15000 })
|
|
await page.waitForTimeout(800)
|
|
|
|
const saved = await page.evaluate(() => {
|
|
const figures = [...document.querySelectorAll('.sentence--figure')]
|
|
return {
|
|
count: figures.length,
|
|
captions: figures.map((node) => node.querySelector('.figure-caption').textContent.trim()),
|
|
decoded: figures.map((node) => node.querySelector('img').naturalWidth),
|
|
}
|
|
})
|
|
check('the inserted figure is in the paper', saved.count === 2, JSON.stringify(saved))
|
|
check('and it is the one that was uploaded', saved.captions.includes('第二张图'), saved.captions.join())
|
|
check('both pictures decoded', saved.decoded.every((width) => width === 480), saved.decoded.join())
|
|
} finally {
|
|
await browser.close()
|
|
if (paperId) {
|
|
await api('DELETE', `/papers/${paperId}`).catch((error) => console.error('cleanup:', error.message))
|
|
}
|
|
}
|
|
|
|
const failed = results.filter(([, ok]) => !ok)
|
|
console.log(`\n${results.length - failed.length}/${results.length} checks passed`)
|
|
process.exit(failed.length ? 1 : 0)
|