import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { hasRecentOwnPublicHtmlReference, materializeMissingPublicHtmlWrites, } from '../mindspace-public-finish-sync.mjs'; const root = path.resolve(new URL('..', import.meta.url).pathname); const runtimeServer = path.join(root, '.runtime', 'portal', 'server.mjs'); const currentUser = { id: 'a6fb1e97-2b0f-447b-b138-4561d8e5c53e', username: 'john' }; const toolRequestOnlyMessages = [ { role: 'assistant', content: [ { type: 'toolRequest', toolCall: { value: { name: 'write', arguments: { path: 'public/guarded-release.html', content: 'Guarded release', }, }, }, }, ], }, ...Array.from({ length: 12 }, (_, index) => ({ role: index % 2 === 0 ? 'assistant' : 'user', content: [{ type: 'text', text: `validation step ${index + 1}` }], })), ]; assert.equal( hasRecentOwnPublicHtmlReference(toolRequestOnlyMessages, currentUser), true, 'finish sync must detect textless write tool requests beyond the old eight-message window', ); const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'public-finish-release-guard-')); try { const result = materializeMissingPublicHtmlWrites({ messages: toolRequestOnlyMessages, publishDir, }); assert.deepEqual(result.materialized, ['public/guarded-release.html']); assert.equal( fs.readFileSync(path.join(publishDir, 'public/guarded-release.html'), 'utf8'), 'Guarded release', ); } finally { fs.rmSync(publishDir, { recursive: true, force: true }); } if (!fs.existsSync(runtimeServer)) { throw new Error(`runtime server bundle is missing: ${runtimeServer}`); } const runtimeSource = fs.readFileSync(runtimeServer, 'utf8'); const requiredRuntimeSnippets = [ 'normalizedName === "write"', 'normalizedName.endsWith("__write")', 'normalizedName === "edit_file"', 'applyPublicHtmlEdit', 'recentCount = 80', 'if (extractPublicHtmlWriteArtifacts([message], { publishDir }).length > 0) {', ]; for (const snippet of requiredRuntimeSnippets) { assert.ok( runtimeSource.includes(snippet), `runtime server bundle is missing public finish sync guard: ${snippet}`, ); } console.log('public finish sync runtime guard ok');