fix(desktop): accept full native template cloud backups

Allow V3 native projects up to 128 screens and 300000 pixels in total height while preserving web draft limits. Cover the full-canvas boundary and both existing 40-item warning lists with focused contract tests.
This commit is contained in:
WuYongJie 2026-11-06 16:43:42 +08:00
parent 5617ec205e
commit 52c5fc8e28
3 changed files with 102 additions and 2 deletions

View File

@ -26,10 +26,11 @@ export function desktopPortal(value) {
export function desktopProjectBody(input) {
desktopAssert(input && typeof input === 'object' && JSON.stringify(input).length <= 1_500_000, '项目内容过大')
const doc = input.document
desktopAssert([1,2,3].includes(doc?.version) && typeof doc.name === 'string' && doc.name.length <= 120 && Array.isArray(doc.screens) && doc.screens.length > 0 && doc.screens.length <= 12, '客户端项目格式无效')
const native = doc?.version === 3
desktopAssert([1,2,3].includes(doc?.version) && typeof doc.name === 'string' && doc.name.length <= 120 && Array.isArray(doc.screens) && doc.screens.length > 0 && doc.screens.length <= (native ? 128 : 12), '客户端项目格式无效')
if (doc.version === 2) Object.assign(doc, validateCreation(doc))
const width = doc.screens[0].width
desktopAssert([750, 1440].includes(width) && doc.screens.every(s => s.width === width && Number.isInteger(s.height) && s.height >= 400 && s.height <= 4608 && (doc.version === 3 || Array.isArray(s.layers) && s.layers.length <= 100)) && doc.screens.reduce((sum,s) => sum + s.height,0) <= 30000, '项目画布超出限制')
desktopAssert([750, 1440].includes(width) && doc.screens.every(s => s.width === width && Number.isInteger(s.height) && s.height >= 400 && s.height <= 4608 && (native || Array.isArray(s.layers) && s.layers.length <= 100)) && doc.screens.reduce((sum,s) => sum + s.height,0) <= (native ? 300000 : 30000), '项目画布超出限制')
desktopAssert(['draft', 'active', 'completed'].includes(input.status) && Number.isInteger(input.revision) && input.revision >= 0, '项目状态或版本无效')
const conversation = input.conversation || []
desktopAssert(Array.isArray(conversation) && conversation.length <= 200 && conversation.every(m => typeof m.id === 'string' && ['user','assistant'].includes(m.role) && typeof m.text === 'string' && m.text.length <= 16000), '对话记录无效')

View File

@ -26,3 +26,23 @@ test('native draft contract preserves chat and unsent input with bounded 1440px
assert.throws(()=>desktopProjectBody({...value,revision:-1}))
assert.throws(()=>desktopProjectBody({...value,composer:'x'.repeat(6001)}))
})
test('native template projects preserve the complete long canvas while web draft limits stay unchanged',()=>{
const file=(path,sha)=>({path,sha256:sha.repeat(64),source:'native:'+sha.repeat(64),size:512})
const native={format:'photocraft',schemaVersion:1,documentSource:'native:'+'a'.repeat(64),files:[file('canvas.pcraft','a'),file('context.json','b'),file('session-backup.json','c')]}
const heights=Array.from({length:22},(_,i)=>i===21?2830:2100)
const document={version:3,id:'long-template',name:'完整详情模板',native,screens:heights.map((height,i)=>({id:'s'+i,width:1440,height,previewSource:'local:screen-'+i}))}
const input={document,revision:1,status:'draft'}
const accepted=desktopProjectBody(input).document
assert.equal(accepted.screens.length,22)
assert.equal(accepted.screens.reduce((sum,s)=>sum+s.height,0),46930)
const full=Array.from({length:128},(_,i)=>({id:'s'+i,width:1440,height:i===127?2820:2340,previewSource:'local:screen-'+i}))
assert.equal(desktopProjectBody({...input,document:{...document,screens:full}}).document.screens.length,128)
assert.equal(full.reduce((sum,s)=>sum+s.height,0),300000)
assert.throws(()=>desktopProjectBody({...input,document:{...document,screens:[...full,{id:'s128',width:1440,height:400}]}}))
assert.throws(()=>desktopProjectBody({...input,document:{...document,screens:full.map((s,i)=>i===127?{...s,height:2821}:s)}}))
for(const version of [1,2])assert.throws(()=>desktopProjectBody({...input,document:{...document,version}}))
assert.throws(()=>desktopProjectBody({...input,document:{...document,screens:Array.from({length:101},(_,i)=>({id:'s'+i,width:1440,height:3000}))}}))
assert.throws(()=>desktopProjectBody({...input,document:{...document,native:{...native,files:[...native.files,file('outside.json','d')]}}}))
assert.throws(()=>desktopProjectBody({...input,document:{...document,native:{...native,files:native.files.map((f,i)=>i?f:{...f,size:512*1024*1024+1})}}}))
})

View File

@ -0,0 +1,79 @@
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { emptyCreation, validateCreation } from '../lib/desktop-client/creation.mjs'
function fixture(warnings) {
const value = emptyCreation()
value.brief.name = '真实合同形状的产品资料'
value.facts = {
status: 'ready',
items: [{ text: '测试仅验证已存资料合同,不派发模型请求', source: '本地合成合同样例' }],
missing: Array.from({ length: 12 }, (_, index) => `事实待确认 ${index + 1}`),
}
value.sellingPoints.batches = [{
id: 'batch-1', direction: '', items: ['产品外观表达'], factsHash: 'a'.repeat(64), warnings: [...warnings],
}]
value.plan = {
...value.plan,
status: 'needs-confirm',
screens: [{ id: 'S01', name: '首屏', module: '产品介绍', image: true, title: '产品', body: '', imagePrompt: '仅验证数据结构' }],
warnings: [...warnings],
}
return value
}
function storedUnion(each) {
return [
...Array.from({ length: each }, (_, index) => `事实待确认 ${index + 1}`),
...Array.from({ length: each }, (_, index) => `模型待确认 ${index + 1}`),
]
}
test('the stored 12 factual plus 12 model warnings survive in both batches and the plan', () => {
const warnings = storedUnion(12), value = fixture(warnings), clean = validateCreation(value)
assert.equal(warnings.length, 24)
assert.deepEqual(clean.sellingPoints.batches[0].warnings, warnings)
assert.deepEqual(clean.plan.warnings, warnings)
assert.deepEqual(clean.facts, value.facts)
})
test('all 40 merged warnings retain their order and text instead of being truncated', () => {
const warnings = storedUnion(20), clean = validateCreation(fixture(warnings))
assert.equal(warnings.length, 40)
assert.deepEqual(clean.sellingPoints.batches[0].warnings, warnings)
assert.deepEqual(clean.plan.warnings, warnings)
})
test('41 stored warnings fail independently in the batch and plan fields', () => {
const warnings = Array.from({ length: 41 }, (_, index) => `待确认 ${index + 1}`)
const batch = fixture([]); batch.sellingPoints.batches[0].warnings = warnings
assert.throws(() => validateCreation(batch), /卖点待确认项超出限制/)
const plan = fixture([]); plan.plan.warnings = warnings
assert.throws(() => validateCreation(plan), /方案待确认项超出限制/)
})
test('the existing 500-character string boundary still applies to every merged warning', () => {
const allowed = fixture(['字'.repeat(500)])
assert.equal(validateCreation(allowed).plan.warnings[0].length, 500)
for (const target of ['batch', 'plan']) {
const value = fixture([])
if (target === 'batch') value.sellingPoints.batches[0].warnings = ['字'.repeat(501)]
else value.plan.warnings = ['字'.repeat(501)]
assert.throws(() => validateCreation(value), /待确认项格式无效/)
}
})
test('older 20-item, empty and omitted warning fields remain compatible', () => {
const old = Array.from({ length: 20 }, (_, index) => `原有待确认 ${index + 1}`)
const clean = validateCreation(fixture(old))
assert.deepEqual(clean.sellingPoints.batches[0].warnings, old)
assert.deepEqual(clean.plan.warnings, old)
const empty = validateCreation(fixture([]))
assert.deepEqual(empty.sellingPoints.batches[0].warnings, [])
assert.deepEqual(empty.plan.warnings, [])
const legacy = fixture([])
delete legacy.sellingPoints.batches[0].warnings; delete legacy.plan.warnings
const upgraded = validateCreation(legacy)
assert.equal(Object.hasOwn(upgraded.sellingPoints.batches[0], 'warnings'), false)
assert.equal(Object.hasOwn(upgraded.plan, 'warnings'), false)
})