Files
shumai/packages/core/src/watermark/watermark.test.ts
T
Yiling-J 03aea2cb36 feat(sharelink): add sharelink watermark support (#270)
* feat(sharelink): add sharelink watermark support

* fix(sharelink): harden watermark feature against races, crashes, and tenant leaks

Addresses review findings on the sharelink watermark feature:

- fix(watermark): read back sharelink via transaction client so the
  updateShareLinkWatermark response reflects the just-committed state
  (global-client reads on a pooled connection miss uncommitted changes)
- fix(watermark): use prisma upsert on the config hash instead of
  find-then-create to avoid P2002 races on concurrent enables
- fix(watermark): resolve nested symlinks when walking the share tree and
  batch the walk level-by-level instead of one query per node
- fix(watermark): keep watermark tasks in flight from being purged by
  orphan-config GC (avoids FK violations in completing activities)
- fix(transcode): re-transcode stale 'processing' watermark rows instead
  of silently skipping (crash recovery); downscale embedded block images;
  use pino structured logging; drop unused any casts
- fix(api): scope watermark-template list/create to the caller's teams,
  reject teamless template creation, and make teamless templates
  read-only; validate watermark config required when enabling (400);
  map sharelink-not-found to 404
- fix(api): resolve symlink target ids once in public share children
  endpoint instead of twice per item
- refactor(db): dedicated WatermarkFileStatus enum (migration included)
- test: activity unit tests (init crash recovery, image + video transcode
  paths), concurrent upsert, nested-symlink resolution, API scoping and
  validation, e2e video watermark path

* refactor(transcode): move sharp usage out of watermark activity into transcodeService

The transcode package does not declare sharp as a dependency (only
@shumai/core does), so importing it directly in the watermark activity
broke CI typecheck for the transcode package.

- add transcodeService.renderSvgToPng, downscaleImageToPng and
  compositeOverlayToWebpFile encapsulating the sharp logic
- watermark activity now calls these methods instead of importing sharp
- activity tests mock the new methods (no sharp/real fixture needed)
- add unit tests for the new transcodeService helpers

* feat(watermark): restrict watermark transcode to existing image and video transcodes

* refactor(transcode): reuse transcodeVideo in watermark activity

* refactor(watermark): use Bun.hash instead of node crypto for watermark config hash

* refactor(watermark): move computeWatermarkConfigHash from dtos to core watermark-hash module

* refactor(api): delegate watermark DB queries from public-share route to watermarkService

* refactor(share): replace dynamic watermarkService import with static import

* feat(watermark): add waitForWatermarkFileActivity for in-flight watermark transcoding tasks

* refactor(asset): replace dynamic watermarkService import with static import

* test(watermark): harden purgeOrphanWatermarkConfigs coverage

Replace the single happy-path GC test with a dedicated suite covering:

- orphan config with proxies: purged, watermarkFiles removed, all four
  proxy keys deleted from S3 while the original key is preserved
- config still referenced by a share link is not purged
- config with pending/processing transcode_watermark tasks is not purged
  (in-flight guard), but completed tasks do not block purging
- pending tasks of other types (transcode_video) do not protect a config
- an S3 delete failure is non-fatal: remaining keys still deleted and the
  config is still purged
- mixed run: only the orphan config is purged while a referenced one
  survives (exact count assertion)

* feat(webui): add sharelink watermark editor and sidebar controls

* style(webui): enlarge watermark editor dialog and allocate 2/3 width to preview column

* fix(webui): override sm:max-w-lg in DialogContent to allow full 1400px width

* fix(webui): stop block click propagation and remove preview mode toggles

* fix(webui): store watermark block size as fraction of canvas width

The editor stored block.size in pixels (slider 10-120px, preview
fontSize: `${block.size}px`) while the SVG generator
(generateWatermarkSvg) interprets size as a fraction (0..1) of canvas
width. A 36px block therefore rendered as a ~36x-canvas glyph covering
the entire image.

- Keep backend fraction semantics (canvasWidth * size) so watermarks
  scale proportionally with output resolution across proxies
- Editor: defaults (text 5%, image 15%), slider ranges (1-12% / 2-50%)
  and number input now work in percent, storing a fraction
- Preview: scales blocks by the measured canvas width so WYSIWYG
- DTO schema: size constrained to (0..1) to enforce the contract
- Update DTO/editor tests to fraction sizes

* feat(webui): add single-row aspect ratio selector and 5 predefined background colors to watermark editor preview

* feat(webui): update aspect ratio to multi-toggle group and enable dot-grid pattern for all background colors

* fix(webui): update background swatches to distinct colors and share PRESET_BACKGROUND_OPTIONS array

* fix(webui): hide redundant absolute close button on watermark editor dialog header

* fix(transcode): skip raw video transcode when generating watermark proxies

The raw marker entry (isRaw: true) in videoTranscodes points at the
original source file and is only used to offer the 'Original' playback
option. It is not a transcoded proxy, so watermarking it produced a
redundant full-resolution transcode (the most expensive pass) plus an
unneeded S3 upload.

Skip entries with isRaw: true in transcodeWatermarkMediaActivity, and
cover the behavior with a unit regression test plus an e2e assertion
that exactly one watermark proxy is produced for a video asset.

* feat(webui): replace watermark image asset ID with 1MB file uploader

* fix(webui): correct watermark editor preview block sizing

The preview canvas width was measured in a useEffect([open]) that ran
before the Radix Dialog had mounted its content, so canvasRef.current was
still null, canvasWidthPx stayed 0, and every block silently fell back to
the hardcoded 640px width (block.size * (canvasWidthPx || 640)). A 100%
size therefore rendered at ~640px on an ~876px canvas (~72%), making the
preview smaller than the real watermarked output for both image and text
blocks.

- Measure the canvas via a ref callback (fires exactly when the node
  mounts, regardless of dialog mount timing) and keep a ResizeObserver
  attached for aspect-ratio/window changes.
- Use clientWidth (padding-box width) instead of getBoundingClientRect(),
  which includes the dialog's zoom-in-95 animation transform.
- Drop max-w-full/max-h-full on the preview <img> so logos scale up to
  the block size like the backend SVG rendering does, instead of being
  capped at their natural pixel size.

* fix(webui): keep watermark editor preview aspect ratio and restore config scroll

- Compute the preview canvas size with a JS contain-fit (ResizeObserver on the
  frame) instead of relying on CSS aspect-ratio + w-full/max-h-full, which
  broke the selected ratio on wide-and-short (or tall-and-narrow) windows.
- Add min-h-0 to the config ScrollArea so it shrinks to the column's remaining
  height instead of growing to its content height, restoring the scroll range.

* fix(webui): remove toast notification on template load

* fix(share): serve per-file watermark proxies in public share APIs

Stop gating watermark replacement on the share-link-wide watermarkStatus.
Public share endpoints now resolve each asset's own completed watermark
proxy and:

- replace videoTranscodes/imageTranscodes with the watermarked entries
  when the watermark file is completed;
- empty those arrays when no completed proxy exists yet (still
  transcoding or failed) so original, unwatermarked media is never
  exposed while a newly added file is being processed;
- leave non-video/image media (pdf, audio, ...) untouched, since they
  never receive watermark transcodes.

This removes the previous behavior where adding a file to a ready
watermark share flipped the whole share link to processing and served
original media for every file until the new transcode finished.

Adds API tests for detail/children endpoints (ready, in-flight, pdf, and
watermark-disabled cases) and a workflow E2E test for adding an asset to
an already-ready watermark sharelink.
2026-08-07 21:36:20 +08:00

544 lines
18 KiB
TypeScript

import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { watermarkService } from '@shumai/core/src/watermark/watermark'
import { generateWatermarkSvg } from '@shumai/core/src/watermark/watermark-svg'
import { shareService } from '@shumai/core/src/share/share'
import { teamService } from '@shumai/core/src/team/team'
import { s3Service } from '@shumai/core/src/s3/s3'
import { computeWatermarkConfigHash } from '@shumai/core/src/watermark/watermark-hash'
import type { WatermarkConfigSpec } from '@shumai/dtos'
import { beforeEach, describe, expect, it, vi } from 'vitest'
describe('WatermarkService', () => {
setupTestDbHooks()
const sampleConfig: WatermarkConfigSpec = {
blocks: [
{
id: 'block-1',
type: 'text',
x: 0.5,
y: 0.5,
opacity: 0.8,
rotation: -30,
text: 'CONFIDENTIAL',
size: 0.2,
color: '#FF0000',
},
],
}
it('should compute deterministic hash ignoring block id and block order', () => {
const config1: WatermarkConfigSpec = {
blocks: [
{
id: 'b1',
type: 'text',
x: 0.5,
y: 0.5,
opacity: 0.5,
rotation: 0,
text: 'A',
size: 0.1,
color: '#FFFFFF',
},
{
id: 'b2',
type: 'text',
x: 0.1,
y: 0.1,
opacity: 0.9,
rotation: 15,
text: 'B',
size: 0.2,
color: '#000000',
},
],
}
const config2: WatermarkConfigSpec = {
blocks: [
{
id: 'other-id-2',
type: 'text',
x: 0.1,
y: 0.1,
opacity: 0.9,
rotation: 15,
text: 'B',
size: 0.2,
color: '#000000',
},
{
id: 'other-id-1',
type: 'text',
x: 0.5,
y: 0.5,
opacity: 0.5,
rotation: 0,
text: 'A',
size: 0.1,
color: '#FFFFFF',
},
],
}
const hash1 = computeWatermarkConfigHash(config1)
const hash2 = computeWatermarkConfigHash(config2)
expect(hash1).toBe(hash2)
})
it('should generate SVG overlay XML', () => {
const svg = generateWatermarkSvg(sampleConfig, 1000, 500)
expect(svg).toContain('<svg')
expect(svg).toContain('CONFIDENTIAL')
expect(svg).toContain('fill="#FF0000"')
})
it('should upsert watermark config by hash', async () => {
const config1 = await watermarkService.upsertConfig(sampleConfig)
expect(config1.id).toBeDefined()
const config2 = await watermarkService.upsertConfig(sampleConfig)
expect(config2.id).toBe(config1.id)
})
it('should not race when upserting the same config concurrently', async () => {
const results = await Promise.all(
Array.from({ length: 10 }, () => watermarkService.upsertConfig(sampleConfig)),
)
const ids = new Set(results.map((r) => r.id))
expect(ids.size).toBe(1)
})
it('should manage watermark templates (CRUD)', async () => {
const user = await prisma.user.create({
data: { name: 'Test User', email: 'test-wm-tpl@example.com' },
})
const team = await teamService.createTeam(user, { name: 'Watermark Team' })
const created = await watermarkService.createTemplate(team.id, 'Draft Preset', sampleConfig)
expect(created.id).toBeDefined()
expect(created.name).toBe('Draft Preset')
const list = await watermarkService.listTemplates(team.id)
expect(list.length).toBe(1)
const updated = await watermarkService.updateTemplate(created.id, 'Final Preset')
expect(updated.name).toBe('Final Preset')
const fetched = await watermarkService.getTemplate(created.id)
expect(fetched.name).toBe('Final Preset')
await watermarkService.deleteTemplate(created.id)
const emptyList = await watermarkService.listTemplates(team.id)
expect(emptyList.length).toBe(0)
})
it('should update sharelink watermark and dispatch transcode tasks', async () => {
const user = await prisma.user.create({
data: { name: 'Share User', email: 'share-wm@example.com' },
})
const team = await teamService.createTeam(user, { name: 'Share Team' })
const projectFolder = await prisma.asset.create({
data: { name: 'root', type: 'root', status: 'processed' },
})
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id, rootFolderId: projectFolder.id },
})
const shareLink = await shareService.createShareLink(project.id, { name: 'My Share' }, user.id)
// Add a file asset to project and share link
const asset = await prisma.asset.create({
data: {
name: 'sample.mp4',
type: 'file',
mediaType: 'video/mp4',
status: 'processed',
projectId: project.id,
},
})
await shareService.addAssetToShare(shareLink.id, { assetIds: [asset.id] })
// Update sharelink watermark -> enable
const updated = await watermarkService.updateShareLinkWatermark(
shareLink.id,
true,
sampleConfig,
)
expect(updated.watermarkStatus).toBe('processing')
expect(updated.watermarkConfigId).toBeDefined()
// Verify WorkflowTask was created
const tasks = await prisma.workflowTask.findMany({
where: { assetId: asset.id, type: 'transcode_watermark' },
})
expect(tasks.length).toBeGreaterThan(0)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((tasks[0].payload as any)?.watermark?.watermarkConfigId).toBe(updated.watermarkConfigId)
// Attempting update while status is processing should throw conflict error
await expect(
watermarkService.updateShareLinkWatermark(shareLink.id, true, sampleConfig),
).rejects.toThrow('Watermark transcoding is currently in progress')
// Simulate completion of transcoding
await prisma.shareLink.update({
where: { id: shareLink.id },
data: { watermarkStatus: 'ready' },
})
// Disable watermark after ready
const disabled = await watermarkService.updateShareLinkWatermark(shareLink.id, false)
expect(disabled.watermarkStatus).toBe('disabled')
expect(disabled.watermarkConfigId).toBeNull()
})
it('should resolve media assets including nested symlinks in shared folders', async () => {
const user = await prisma.user.create({
data: { name: 'Nested User', email: 'nested-wm@example.com' },
})
const team = await teamService.createTeam(user, { name: 'Nested Team' })
const projectFolder = await prisma.asset.create({
data: { name: 'root', type: 'root', status: 'processed' },
})
const project = await prisma.project.create({
data: { name: 'Nested Project', teamId: team.id, rootFolderId: projectFolder.id },
})
const shareLink = await shareService.createShareLink(
project.id,
{ name: 'Nested Share' },
user.id,
)
// Shared folder -> inner folder -> symlink -> media file
const sharedFolder = await prisma.asset.create({
data: { name: 'shared', type: 'folder', status: 'processed', projectId: project.id },
})
const innerFolder = await prisma.asset.create({
data: { name: 'inner', type: 'folder', status: 'processed', projectId: project.id },
})
const mediaFile = await prisma.asset.create({
data: {
name: 'clip.mp4',
type: 'file',
mediaType: 'video/mp4',
status: 'processed',
projectId: project.id,
parentId: innerFolder.id,
},
})
// symlink inside the shared folder points at the inner folder
await prisma.asset.create({
data: {
name: 'inner-link',
type: 'symlink',
status: 'processed',
projectId: project.id,
parentId: sharedFolder.id,
targetId: innerFolder.id,
},
})
// root symlink for the share points at the shared folder
await prisma.asset.create({
data: {
name: 'shared-link',
type: 'symlink',
status: 'processed',
projectId: project.id,
parentId: shareLink.rootFolderId,
targetId: sharedFolder.id,
},
})
const mediaIds = await watermarkService.getMediaAssetIdsInShareLink(shareLink.rootFolderId)
expect(mediaIds).toContain(mediaFile.id)
})
describe('purgeOrphanWatermarkConfigs', () => {
const bucket = 'shumai-gc-test-bucket'
beforeEach(() => {
vi.restoreAllMocks()
process.env.S3_BUCKET = bucket
})
// Media with every proxy slot the GC should delete from S3 (plus an
// `original` key that must survive).
function proxyMedia(): PrismaJson.MediaInfo {
return {
duration: 0,
filesize: 100,
frames: 0,
proxyType: 'video',
videoTranscodes: [
{ key: 'files/gc/wm.mp4', width: 1920, height: 1080, resolution: '1080p' },
],
imageTranscodes: [
{ key: 'files/gc/wm.webp', width: 1280, height: 720, quality: 90, format: 'webp' },
],
videoPreview: { key: 'files/gc/preview.mp4', width: 1280, height: 720 },
thumbnail: {
key: 'files/gc/thumb.webp',
width: 320,
height: 180,
quality: 90,
format: 'webp',
},
finishedAt: new Date().toISOString(),
metadata: null,
original: { key: 'files/gc/original.mp4', downloadUrl: '', filesizeInBytes: 0, codec: '' },
}
}
async function seedProject() {
const team = await prisma.team.create({ data: { name: 'GC Team' } })
const projectFolder = await prisma.asset.create({
data: { name: 'root', type: 'root', status: 'processed' },
})
return prisma.project.create({
data: { name: 'GC Project', teamId: team.id, rootFolderId: projectFolder.id },
})
}
async function seedConfigWithWatermarkFile(config: WatermarkConfigSpec = sampleConfig) {
const asset = await prisma.asset.create({
data: { name: 'gc-asset.mp4', type: 'file', mediaType: 'video/mp4', status: 'processed' },
})
const wmConfig = await watermarkService.upsertConfig(config)
const watermarkFile = await prisma.watermarkFile.create({
data: {
assetId: asset.id,
watermarkConfigId: wmConfig.id,
status: 'completed',
media: proxyMedia(),
},
})
return { asset, config: wmConfig, watermarkFile }
}
async function seedWatermarkTask(
assetId: string,
projectId: string,
configId: string,
status: 'pending' | 'processing' | 'completed',
type: 'transcode_watermark' | 'transcode_video' = 'transcode_watermark',
) {
return prisma.workflowTask.create({
data: {
assetId,
projectId,
type,
status,
payload: {
projectId,
watermark: { watermarkConfigId: configId },
},
},
})
}
it('should purge an orphan config, delete its S3 proxies and keep the original key', async () => {
const { config, watermarkFile } = await seedConfigWithWatermarkFile()
const deleteSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(1)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).toBeNull()
expect(await prisma.watermarkFile.findUnique({ where: { id: watermarkFile.id } })).toBeNull()
// All proxy slots deleted, but the original file is never touched
for (const key of [
'files/gc/wm.mp4',
'files/gc/wm.webp',
'files/gc/preview.mp4',
'files/gc/thumb.webp',
]) {
expect(deleteSpy).toHaveBeenCalledWith(bucket, key)
}
expect(deleteSpy).not.toHaveBeenCalledWith(bucket, 'files/gc/original.mp4')
expect(deleteSpy).toHaveBeenCalledTimes(4)
})
it('should NOT purge a config still referenced by a share link', async () => {
const { config } = await seedConfigWithWatermarkFile()
const project = await seedProject()
const shareFolder = await prisma.asset.create({
data: { name: 'share-folder', type: 'share', status: 'processed', projectId: project.id },
})
await prisma.shareLink.create({
data: {
name: 'GC Share',
projectId: project.id,
rootFolderId: shareFolder.id,
watermarkConfigId: config.id,
},
})
const deleteSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(0)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).not.toBeNull()
expect(deleteSpy).not.toHaveBeenCalled()
})
it.each(['pending', 'processing'] as const)(
'should NOT purge a config with an in-flight %s watermark task',
async (status) => {
const { asset, config } = await seedConfigWithWatermarkFile()
const project = await seedProject()
await seedWatermarkTask(asset.id, project.id, config.id, status)
const deleteSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(0)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).not.toBeNull()
expect(deleteSpy).not.toHaveBeenCalled()
},
)
it('should purge a config once its watermark tasks are all completed', async () => {
const { asset, config } = await seedConfigWithWatermarkFile()
const project = await seedProject()
await seedWatermarkTask(asset.id, project.id, config.id, 'completed')
vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(1)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).toBeNull()
})
it('should ignore pending tasks of other types when deciding to purge', async () => {
const { asset, config } = await seedConfigWithWatermarkFile()
const project = await seedProject()
// A pending transcode_video task must not protect the watermark config
await seedWatermarkTask(asset.id, project.id, config.id, 'pending', 'transcode_video')
vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(1)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).toBeNull()
})
it('should keep purging other configs and S3 keys when one S3 delete fails', async () => {
const { config } = await seedConfigWithWatermarkFile()
const deleteSpy = vi
.spyOn(s3Service, 'deleteObject')
.mockRejectedValueOnce(new Error('s3 down'))
.mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(1)
expect(await prisma.watermarkConfig.findUnique({ where: { id: config.id } })).toBeNull()
// All 4 proxy keys were attempted even though the first one failed
expect(deleteSpy).toHaveBeenCalledTimes(4)
})
it('should purge only the orphan config when another config is still referenced', async () => {
const otherConfig: WatermarkConfigSpec = {
blocks: [
{
id: 'other-block',
type: 'text',
x: 0.3,
y: 0.3,
opacity: 0.4,
rotation: 10,
text: 'OTHER TEAM',
size: 0.1,
color: '#00FF00',
},
],
}
const orphan = await seedConfigWithWatermarkFile()
const referenced = await seedConfigWithWatermarkFile(otherConfig)
const project = await seedProject()
const shareFolder = await prisma.asset.create({
data: { name: 'share-folder', type: 'share', status: 'processed', projectId: project.id },
})
await prisma.shareLink.create({
data: {
name: 'GC Share',
projectId: project.id,
rootFolderId: shareFolder.id,
watermarkConfigId: referenced.config.id,
},
})
vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(0)
const purgedCount = await watermarkService.purgeOrphanWatermarkConfigs()
expect(purgedCount).toBe(1)
expect(
await prisma.watermarkConfig.findUnique({ where: { id: orphan.config.id } }),
).toBeNull()
expect(
await prisma.watermarkConfig.findUnique({ where: { id: referenced.config.id } }),
).not.toBeNull()
})
})
it('should fetch completed watermark media map for asset IDs', async () => {
const config = await watermarkService.upsertConfig(sampleConfig)
const asset1 = await prisma.asset.create({
data: { name: 'asset1.png', type: 'file', status: 'processed' },
})
const asset2 = await prisma.asset.create({
data: { name: 'asset2.png', type: 'file', status: 'processed' },
})
const sampleMedia: PrismaJson.MediaInfo = {
duration: 0,
filesize: 100,
frames: 0,
proxyType: 'image',
imageTranscodes: [
{ key: 'watermarked.webp', width: 100, height: 100, quality: 90, format: 'webp' },
],
videoTranscodes: [],
finishedAt: new Date().toISOString(),
metadata: null,
original: null,
}
await prisma.watermarkFile.create({
data: {
assetId: asset1.id,
watermarkConfigId: config.id,
status: 'completed',
media: sampleMedia,
},
})
// asset2 has pending status
await prisma.watermarkFile.create({
data: {
assetId: asset2.id,
watermarkConfigId: config.id,
status: 'pending',
},
})
const map = await watermarkService.getCompletedWatermarkMediaMap(
[asset1.id, asset2.id],
config.id,
)
expect(map.size).toBe(1)
expect(map.get(asset1.id)).toBeDefined()
expect(map.get(asset1.id)?.imageTranscodes[0].key).toBe('watermarked.webp')
expect(map.get(asset2.id)).toBeUndefined()
})
})