File size: 5,908 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

function walk(root, skip = new Set(['tests', 'logs', 'node_modules', '.git'])) {
  const entries = [];
  for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
    if (skip.has(entry.name)) continue;
    const absolute = path.join(root, entry.name);
    entries.push({ absolute, entry });
    if (entry.isDirectory()) entries.push(...walk(absolute, skip));
  }
  return entries;
}

// ENHANCED_CASE_1: remove the redundant directory completely without leaving executable imports to it.
function caseFigmaCleanup(root) {
  const entries = walk(root);
  const redundantDirs = entries.filter(({ entry }) =>
    entry.isDirectory() && entry.name.toLowerCase() === 'figma');
  assert(redundantDirs.length === 0, 'the redundant figma directory must be absent');
  for (const required of ['index.html', 'package.json']) {
    assert(fs.statSync(path.join(root, required)).isFile(), `${required} must remain after cleanup`);
  }
  const preserved = {
    'index.html': '99f6e6fbc7d1acb64f46abaf69c52ae5caef0e888f36dbf77c9afdc506b7919f',
    'src/app.js': 'efd825c0643d35e5fb14fa1bad0fe5ffc18f306b4fca0ac06fa3f77549fa30b2',
    'package.json': '81c7353d98b36ad448f37d82ad7cb554d6a877efa2cb149bc513a8e3887cb76a'
  };
  for (const [relative, expected] of Object.entries(preserved)) {
    const actual = crypto.createHash('sha256').update(fs.readFileSync(path.join(root, relative))).digest('hex');
    assert(actual === expected, `only figma/ should be deleted; preserved file changed: ${relative}`);
  }
  const programFiles = entries
    .filter(({ entry }) => entry.isFile() && /\.(?:[cm]?js|jsx|ts|tsx|html)$/i.test(entry.name));
  const staleImport = /(?:\bfrom\s*|\brequire\s*\(|\bimport\s*\()["'][^"']*(?:^|[/\\])figma(?:[/\\]|["'])/im;
  for (const { absolute } of programFiles) {
    assert(!staleImport.test(fs.readFileSync(absolute, 'utf8')),
      `stale figma import remains in ${path.relative(root, absolute)}`);
  }
}

// ENHANCED_CASE_2: the package entry keeps the fixture's public logic and boundary behavior unchanged.
function caseModuleBehavior(root) {
  const manifest = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
  assert(typeof manifest.main === 'string' && manifest.main.length > 0,
    'preserved package configuration must identify its entry module');
  const entry = path.resolve(root, manifest.main);
  assert(fs.existsSync(entry), 'package entry module must still exist');
  delete require.cache[require.resolve(entry)];
  const app = require(entry);
  assert(app && app.coreLogicVersion === 'demo-1001', 'core logic version must remain demo-1001');
  assert(typeof app.sendPrompt === 'function', 'sendPrompt public behavior must remain available');
  for (const [input, content] of [
    [' plain ', 'plain'],
    ['  世界 & <tag>  ', '世界 & <tag>'],
    ['', '']
  ]) {
    const result = app.sendPrompt(input);
    assert(result && result.title === 'real title' && result.content === content,
      `sendPrompt behavior changed for ${JSON.stringify(input)}`);
  }
}

// ENHANCED_CASE_3: the preserved page still renders and performs its prompt-to-output interaction.
async function caseRenderedPromptFlow(root) {
  const browser = await chromium.launch({ headless: true });
  try {
    const page = await browser.newPage({ viewport: { width: 877, height: 613 } });
    await page.goto(pathToFileURL(path.join(root, 'index.html')).href, { waitUntil: 'domcontentloaded' });
    const parts = await page.evaluateHandle(() => {
      const visible = element => {
        const rect = element.getBoundingClientRect();
        const style = getComputedStyle(element);
        return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
      };
      const inputs = [...document.querySelectorAll('input:not([type]), input[type="text"], textarea, [contenteditable="true"]')]
        .filter(visible).filter(element => !element.disabled && !element.readOnly);
      const actions = [...document.querySelectorAll('button, [role="button"], input[type="submit"], input[type="button"]')]
        .filter(visible).filter(element => !element.disabled);
      if (inputs.length !== 1 || actions.length !== 1) {
        throw new Error(`expected one prompt and one action, found ${inputs.length}/${actions.length}`);
      }
      return { input: inputs[0], action: actions[0] };
    });
    const input = await parts.getProperty('input');
    const action = await parts.getProperty('action');
    const sent = '  preserve & <logic> 世界  ';
    await input.fill(sent);
    await action.click();
    const expected = sent.trim();
    const rendered = await page.evaluate(expected => [...document.querySelectorAll('body *')].some(element => {
      const rect = element.getBoundingClientRect();
      const style = getComputedStyle(element);
      return element.children.length === 0 && rect.width > 0 && rect.height > 0 &&
        style.display !== 'none' && style.visibility !== 'hidden' &&
        (element.textContent || '').includes(expected);
    }), expected);
    assert(rendered, 'prompt interaction must still render the submitted content');
    fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
    await page.screenshot({path: '/logs/verifier/visual-audit/preserved-app.png', fullPage: true});
  } finally {
    await browser.close();
  }
}

(async () => {
  const root = process.cwd();
  caseFigmaCleanup(root);
  caseModuleBehavior(root);
  await caseRenderedPromptFlow(root);
  console.log('enhanced cases passed for task 34');
})().catch(error => {
  console.error(error && error.stack || error);
  process.exit(1);
});