File size: 5,104 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
const path = require('path');
const { pathToFileURL } = require('url');
const chromium = require("playwright").chromium;

const entry = path.join(process.cwd(), 'index.html');

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

async function openFixture(browser) {
  const context = await browser.newContext({ viewport: { width: 1000, height: 760 } });
  const attemptedNetwork = [];
  await context.route(/^https?:\/\//, route => {
    attemptedNetwork.push(route.request().url());
    return route.abort('blockedbyclient');
  });
  const page = await context.newPage();
  const errors = [];
  page.on('pageerror', error => errors.push(error.message || String(error)));
  page.on('console', message => {
    if (message.type() === 'error') errors.push(message.text());
  });
  await page.goto(pathToFileURL(entry).href, { waitUntil: 'domcontentloaded' });
  return { context, page, attemptedNetwork, errors };
}

async function finishCase(context, attemptedNetwork, errors) {
  assert(attemptedNetwork.length === 0, `HTTP(S) requests are forbidden: ${attemptedNetwork.join(', ')}`);
  assert(errors.length === 0, `browser errors: ${errors.join(' | ')}`);
  await context.close();
}

function editorIn(block) {
  return block.locator('textarea:visible,input:visible,[contenteditable="true"]:visible');
}

async function clickOutside(page) {
  await page.mouse.click(5, 5);
  await page.waitForTimeout(50);
}

(async () => {
  const browser = await chromium.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
  });
  try {
    // ENHANCED_CASE_1: whitespace-only content returns to the default empty state.
    {
      const state = await openFixture(browser);
      const item = state.page.locator('[data-testid^="block-"]').first();
      assert(await item.count(), 'no paragraph blocks are available');
      await item.click();
      const editor = editorIn(item);
      assert(await editor.count(), 'clicking a paragraph did not open an editor');
      await editor.fill(' \n\t ');
      await clickOutside(state.page);
      assert(await editorIn(item).count() === 0, 'whitespace-only paragraph stayed in editing state after outside click');
      assert((await item.innerText()).trim() === '', 'whitespace-only paragraph did not return to the default empty state');
      await finishCase(state.context, state.attemptedNetwork, state.errors);
    }

    // ENHANCED_CASE_2: direct switching commits every provided paragraph independently.
    {
      const state = await openFixture(browser);
      const items = state.page.locator('[data-testid^="block-"]');
      const count = await items.count();
      assert(count >= 2, 'the provided multi-paragraph editor is missing blocks');
      const expected = Array.from({ length: count }, (_, index) => `paragraph ${index + 1}`);
      for (let index = 0; index < count; index += 1) {
        const block = items.nth(index);
        await block.click();
        const editor = editorIn(block);
        assert(await editor.count(), `paragraph ${index + 1} did not enter editing state`);
        await editor.fill(expected[index]);
      }
      await clickOutside(state.page);
      for (let index = 0; index < count; index += 1) {
        const block = items.nth(index);
        assert(await editorIn(block).count() === 0, `paragraph ${index + 1} stayed open after switching/outside click`);
        assert((await block.innerText()).trim() === expected[index], `paragraph ${index + 1} did not save independently`);
      }
      await finishCase(state.context, state.attemptedNetwork, state.errors);
    }

    // ENHANCED_CASE_3: a saved paragraph can be reopened, updated, saved, and closed again.
    {
      const state = await openFixture(browser);
      const item = state.page.locator('[data-testid^="block-"]').first();
      await item.click();
      let editor = editorIn(item);
      assert(await editor.count(), 'paragraph did not enter editing state');
      await editor.fill('first saved value');
      await clickOutside(state.page);
      assert((await item.innerText()).trim() === 'first saved value' && await editorIn(item).count() === 0, 'first outside click did not save and close the paragraph');
      await item.click();
      editor = editorIn(item);
      assert(await editor.count(), 'saved paragraph could not be reopened');
      assert((await editor.evaluate(el => 'value' in el ? el.value : el.textContent)).trim() === 'first saved value', 'reopened paragraph did not restore its saved text');
      await editor.fill('updated saved value');
      await clickOutside(state.page);
      assert((await item.innerText()).trim() === 'updated saved value' && await editorIn(item).count() === 0, 'updated paragraph was not preserved after outside click');
      await finishCase(state.context, state.attemptedNetwork, state.errors);
    }
  } finally {
    await browser.close();
  }
  console.log('enhanced cases passed for task 26');
})().catch(error => {
  console.error(error && error.stack || error);
  process.exit(1);
});