跳至內容
Astera Technology

離線包齊不齊,要真正跑任務

一個任務要頁面、程序及本地資料,各項都跑齊。

下載完整個案資料及參考答案 ↓

訂判斷及所需輸入

題內任務載頁面及 JS,再讀種子2、3,顯示5。只有頁面及程序會出 no-seed,更正須連種子也有。瀏覽器真正設離線,路由只從明列記憶體集合供應。

已填本地測試工作表
情況輸入 JSON
原例測試{"cacheSeed":true,"values":[2,3]}
下載可編輯 CSV ↓

跑所附比較

真正瀏覽器資產請求在本地截住;未知路由中止,不向外續送。此有限發放測試不是通用 service worker,也未證明整套業務系統都可離線。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"offline":true,"text":"no-seed","missing":["/seed.json"],"externalTransport":0}
所需路徑{"offline":true,"text":"5","missing":[],"externalTransport":0}
下載可編輯 CSV ↓
Node24.14.1 程序;須 Playwright1.63 Chromium
import assert from 'node:assert/strict';
import {createRequire} from 'node:module';
const htmlEscape=s=>String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
async function withPage(action,options={}){
  const require=createRequire(import.meta.url);const {chromium}=require('playwright');
  const browser=await chromium.launch({headless:true});
  try{const context=await browser.newContext(options);const page=await context.newPage();
    const requests=[];await page.route('**/*',route=>{requests.push(route.request().url());return route.abort();});
    return await action(page,context,requests);
  }finally{await browser.close();}
}

const run=async function run(i,corrected=true){
    if(typeof i.cacheSeed!=='boolean')return {held:true};
    return withPage(async(page,context)=>{await page.unroute('**/*');await context.setOffline(true);const missing=[];
      const cache=new Map([['/app','<link rel="icon" href="data:,"><output>loading</output><script src="/app.js"></script>'],['/app.js','fetch("/seed.json").then(r=>r.json()).then(d=>document.querySelector("output").textContent=String(d.values.reduce((a,b)=>a+b,0))).catch(()=>document.querySelector("output").textContent="no-seed");']]);
      if(corrected&&i.cacheSeed)cache.set('/seed.json',JSON.stringify({values:i.values}));
      await page.route('**/*',route=>{const url=new URL(route.request().url());if(url.origin==='https://offline.example.invalid'&&cache.has(url.pathname))return route.fulfill({body:cache.get(url.pathname),contentType:url.pathname.endsWith('.json')?'application/json':url.pathname.endsWith('.js')?'text/javascript':'text/html'});missing.push(url.pathname);return route.abort();});
      await page.goto('https://offline.example.invalid/app');await page.waitForFunction(()=>document.querySelector('output').textContent!=='loading',{timeout:2000});
      return {offline:true,text:await page.locator('output').textContent(),missing,externalTransport:0};
    });
  };
const cases=[{"input":{"cacheSeed":true,"values":[2,3]},"corrected":false,"expected":{"offline":true,"text":"no-seed","missing":["/seed.json"],"externalTransport":0}},{"input":{"cacheSeed":true,"values":[2,3]},"corrected":true,"expected":{"offline":true,"text":"5","missing":[],"externalTransport":0}},{"input":{"cacheSeed":false,"values":[2,3]},"corrected":true,"expected":{"offline":true,"text":"no-seed","missing":["/seed.json"],"externalTransport":0}},{"input":{"cacheSeed":true,"values":[]},"corrected":true,"expected":{"offline":true,"text":"0","missing":[],"externalTransport":0}},{"input":{"cacheSeed":null,"values":[2,3]},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
下載可執行例子 ↓

按邊界依據作判斷

欠種子仍失敗;有空種子顯示0;集合清單未知便停。

  1. 列任務所需資產
  2. 跑獨立離線任務
  3. 核輸出及欠路由

不能用一次結果推整體離線功能。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
包確實欠資料種子{"cacheSeed":false,"values":[2,3]}{"offline":true,"text":"no-seed","missing":["/seed.json"],"externalTransport":0}
有明確空種子{"cacheSeed":true,"values":[]}{"offline":true,"text":"0","missing":[],"externalTransport":0}
本地集合清單未知{"cacheSeed":null,"values":[2,3]}{"held":true}
下載可編輯 CSV ↓

原例程序載到了,任務就完成嗎?

查看參考答案

不是,也需要種子。更正獨立執行顯示5,沒有欠路由。

相關資源

雲端部署與維護 →