verify-live-S3-readback.mjs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Independent, read-only S3 acceptance. Never calls start/save/import.
  2. import fs from 'node:fs';
  3. import crypto from 'node:crypto';
  4. import assert from 'node:assert/strict';
  5. import {invoke,classRows} from '../server/scripts/platform-client.mjs';
  6. import {loadPublicBoard} from '../src/services/workshop-data.js';
  7. const sessionId='btrFqkz0yw',artifactId='oiNxX7o27j',outputSourceId='HGMauzWO5k';
  8. const s1Id='G8IrkkDG7X',s2Id='b7NOby5ys0';
  9. const template=JSON.parse(fs.readFileSync('work/first-live-S3-template.json','utf8'));
  10. const s1Template=JSON.parse(fs.readFileSync('work/first-live-S1-v2-template.json','utf8'));
  11. const s2Template=JSON.parse(fs.readFileSync('work/first-live-S2-template.json','utf8'));
  12. const outputFile='work/live-S3-readback-20261102.json';
  13. const saveRequestId='KSF-真实资料-20261102-01:S3:save:v1';
  14. const pointer=(className,objectId)=>({__type:'Pointer',className,objectId});
  15. const canonical=x=>x&&typeof x==='object'?Array.isArray(x)?'['+x.map(canonical).join(',')+']':'{'+Object.keys(x).sort().map(k=>JSON.stringify(k)+':'+canonical(x[k])).join(',')+'}':JSON.stringify(x);
  16. const sha=x=>crypto.createHash('sha256').update(x).digest('hex');
  17. const read=async(className,objectId)=>{
  18. const r=await invoke('ksfGetRecord',{className,objectId});
  19. assert.equal(r.http,200);assert.equal(r.body?.result?.ok,true);
  20. const row=r.body.result.data.record;assert.equal(row?.objectId,objectId);return row;
  21. };
  22. assert.equal(fs.existsSync(outputFile),false,'Preserve earlier acceptance');
  23. let check='template fingerprint and fixed scope';
  24. try{
  25. assert.equal(template.sessionId,null);assert.equal(template.taskKey,'KSF-真实资料-20261102-01:S3');
  26. assert.equal(template.stageCode,'S3');assert.equal(template.version,1);assert.equal(template.artifactType,'opportunity_card');
  27. assert.equal('opportunity' in template,false);assert.equal('projectId' in template,false);
  28. assert.deepEqual(template.sourceIds,['2mzBadmvws','Pi04eTwTQ3','m1OQx9aynA','QdeMcyTTjX']);
  29. assert.equal(Object.keys(template.report.evidence).length,4);assert.equal(template.pendingQuestions.length,8);
  30. assert.equal(template.rawText.length,2554);
  31. check='read saved records and operation history';
  32. const settled=await Promise.allSettled([
  33. read('KfSession',sessionId),read('KfArtifact',artifactId),read('KfArtifact',s1Id),read('KfArtifact',s2Id),
  34. read('KfSource',outputSourceId),
  35. classRows('KfArtifact',{workshopKey:'KSF-真实资料-20261102-01'}),
  36. classRows('KsfOperation',{operation:'workshop-task',requestId:saveRequestId}),
  37. classRows('KfOpportunity',{sessionRef:pointer('KfSession',sessionId)}),
  38. classRows('KfProject',{sessionRef:pointer('KfSession',sessionId)})
  39. ]);
  40. const failures=settled.flatMap((r,i)=>r.status==='rejected'?[i]:[]);
  41. assert.deepEqual(failures,[],'Read group failure indexes');
  42. const [session,artifact,s1,s2,generated,history,operations,opportunities,projects]=settled.map(r=>r.value);
  43. check='artifact fields and template';
  44. assert.equal(artifact.title,template.title);assert.equal(artifact.rawText,template.rawText);
  45. assert.equal(artifact.version,1);assert.equal(artifact.stageCode,'S3');assert.equal(artifact.artifactType,'opportunity_card');
  46. assert.equal(artifact.reportSeriesKey,template.taskKey);assert.equal(artifact.reportType,'workshop_auxiliary');
  47. assert.equal(artifact.schemaVersion,'ksf.v0.7');assert.equal(artifact.dataMode,'live');assert.equal(artifact.recordState,'active');
  48. assert.equal(artifact.dashboardState,'workshop');assert.equal(artifact.ownerSession,sessionId);
  49. assert.deepEqual(artifact.sessionRef,pointer('KfSession',sessionId));assert.deepEqual(artifact.sourceRef,pointer('KfSource',outputSourceId));
  50. assert.equal(artifact.projectRef??null,null);assert.equal(artifact.previousVersionRef??null,null);
  51. assert.equal(artifact.category??null,null);
  52. assert.deepEqual(artifact.tags,['拌面','课堂过程成果']);
  53. assert.equal(artifact.report.title,template.title);assert.ok(Number.isFinite(Date.parse(artifact.report.generatedAt)));
  54. assert.deepEqual(artifact.report.executive,[ '课堂过程成果,未经员工正式确认;待验证项不作为市场结论。',...template.report.executive ]);
  55. for(const field of ['chapters','evidence','subtitle','method','sources'])assert.deepEqual(artifact.report[field],template.report[field],field);
  56. assert.deepEqual(artifact.meta.workshop.sourceIds,template.sourceIds);
  57. assert.deepEqual(artifact.meta.workshop.pendingQuestions,template.pendingQuestions);
  58. assert.equal(artifact.meta.workshop.formalConfirmation,false);
  59. assert.equal(artifact.dashboardPublishedAt??null,null);assert.equal(artifact.contentConfirmedAt??null,null);
  60. assert.equal(generated.rawText,template.rawText);assert.equal(generated.dataMode,'live');
  61. check='inherited source evidence and quotes';
  62. assert.equal(artifact.evidenceCount,4);assert.equal(artifact.sources.length,4);
  63. for(const [key,entry]of Object.entries(template.report.evidence)){
  64. const origin=await read('KfSource',entry.sourceId);
  65. assert.ok(origin.rawText.includes(entry.quotes[0].text),key+' quote exists in source');
  66. assert.ok(template.rawText.includes(entry.quotes[0].text),key+' quote exists in report text');
  67. assert.equal(entry.quotes.length,1,key+' quote count');
  68. }
  69. check='session ownership and version history';
  70. assert.equal(session.objectId,sessionId);assert.equal(session.stageCode,'S3');assert.equal(session.step,'submitted');
  71. assert.deepEqual(session.meta.attribution,artifact.meta.attribution);
  72. assert.equal(artifact.agentName,'王刚001');assert.equal(artifact.agentId,session.meta.attribution.agentKey);
  73. for(const [row,prior]of [[s1,s1Template],[s2,s2Template]]){
  74. assert.deepEqual(row.meta.attribution,session.meta.attribution);
  75. for(const field of ['title','rawText','version','stageCode'])assert.deepEqual(row[field],prior[field]);
  76. assert.deepEqual(row.report.chapters,prior.report.chapters);assert.deepEqual(row.report.evidence,prior.report.evidence);
  77. assert.deepEqual(row.meta.workshop.sourceIds,prior.sourceIds);
  78. }
  79. assert.equal(s1.dashboardState,'workshop');assert.equal(s2.dashboardState,'workshop');
  80. const series=history.filter(x=>x.reportSeriesKey===template.taskKey);
  81. assert.equal(series.length,1);assert.equal(series[0].objectId,artifactId);assert.equal(series[0].version,1);
  82. assert.equal(history.filter(x=>x.stageCode==='S3'&&x.sessionRef?.objectId===sessionId).length,1);
  83. assert.equal(operations.length,1);assert.equal(operations[0].receipt.artifactId,artifactId);
  84. assert.equal(operations[0].receipt.sourceId,outputSourceId);
  85. assert.equal(operations[0].receipt.opportunityId,null);assert.equal(operations[0].receipt.projectId,null);
  86. assert.equal(opportunities.length,0);assert.equal(projects.length,0);
  87. check='anonymous public board projection';
  88. const board=await loadPublicBoard({url:'https://kangshifu.fmode.cn/api/functions',appId:'FKieRbIoGt_main'});
  89. const projected=board.artifact.find(x=>x.objectId===artifactId);assert.ok(projected);
  90. assert.equal(projected.dashboardState,'workshop');assert.equal(projected.dataMode,'live');
  91. assert.equal(projected.category??null,null);assert.deepEqual(projected.tags,artifact.tags);
  92. assert.deepEqual(projected.report.evidence,template.report.evidence);
  93. assert.deepEqual(projected.report.chapters,template.report.chapters);
  94. assert.ok(board.artifact.some(x=>x.objectId===s1Id));assert.ok(board.artifact.some(x=>x.objectId===s2Id));
  95. for(const value of [session.meta.attribution.participantAccount?.accountId,session.meta.attribution.agentAccount?.accountId,session.meta.attribution.conversationKey].filter(Boolean)){
  96. assert.equal(JSON.stringify(board).includes(value),false);
  97. }
  98. const result={
  99. ok:true,operation:'live-S3-independently-verified',at:new Date().toISOString(),readOnly:true,businessWrites:0,
  100. sessionId,artifactId,outputSourceId,reportUrl:'https://kangshifu.fmode.cn/report/'+artifactId,
  101. version:1,stageCode:'S3',dataMode:'live',dashboardState:'workshop',reportSeriesKey:template.taskKey,
  102. sourceIds:template.sourceIds,evidenceCount:artifact.evidenceCount,quoteCount:4,pendingQuestionCount:8,
  103. templateMatched:true,categoryPersistedAsTag:projected.tags.includes('拌面'),formalReportPublished:false,
  104. dashboardPublishedAt:null,employeeConfirmed:false,sessionOwnerMatched:true,s1AndS2Preserved:true,
  105. artifactSeriesCount:series.length,s3ArtifactCount:1,saveOperationCount:operations.length,opportunityCount:opportunities.length,projectCount:projects.length,
  106. publicBoardProjectionVerified:true,browserVerified:false
  107. };
  108. fs.writeFileSync(outputFile,JSON.stringify(result,null,2)+'\n',{flag:'wx'});
  109. console.log(JSON.stringify(result,null,2));
  110. }catch(error){
  111. const result={ok:false,operation:'live-S3-independent-readback-stopped',check,
  112. checkLine:error.stack?.match(/verify-live-S3-readback\.mjs:(\d+):/)?.[1]??null,readOnly:true,businessWrites:0};
  113. fs.writeFileSync(outputFile,JSON.stringify(result,null,2)+'\n',{flag:'wx'});
  114. console.error(JSON.stringify(result,null,2));process.exitCode=1;
  115. }