先核來源與已訂規則
本例只在新即時 movement 建自動通知行。歷史 M 標 historical1,可另附舊來源通知。沒門檻的插入觸發程序也會在搬歷史時執行,於 old:M 旁加 auto:M。更正 WHEN historical=0。
| 紀錄 | 輸入 JSON |
|---|---|
| 原例輸入 | {"id":"M","historical":1,"sourceNotice":"old:M"} |
比較兩條實際路徑
更正歷史只含 M、old:M;這些是本地資料表行,用來查間接結果,沒有執行真實接收者或供應商傳送。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"movements":[{"id":"M","historical":1}],"notices":[{"id":"auto:M"},{"id":"old:M"}]} |
| 更正路徑 | {"movements":[{"id":"M","historical":1}],"notices":[{"id":"old:M"}]} |
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
const db=new DatabaseSync(':memory:');try{db.exec('CREATE TABLE movement(id TEXT PRIMARY KEY,historical INTEGER CHECK(historical IN(0,1)));CREATE TABLE notice(id TEXT);CREATE TRIGGER notify AFTER INSERT ON movement '+(corrected?'WHEN NEW.historical=0 ':'')+"BEGIN INSERT INTO notice VALUES('auto:'||NEW.id);END;");
db.prepare('INSERT INTO movement VALUES(?,?)').run(i.id,i.historical);
if(i.sourceNotice!==null)db.prepare('INSERT INTO notice VALUES(?)').run(i.sourceNotice);
return {movements:rows(db,'SELECT * FROM movement'),notices:rows(db,'SELECT * FROM notice ORDER BY id')};
}finally{db.close();}
};
const cases=[{"label":"misleading base","input":{"id":"M","historical":1,"sourceNotice":"old:M"},"expected":{"movements":[{"id":"M","historical":1}],"notices":[{"id":"auto:M"},{"id":"old:M"}]},"corrected":false},{"label":"corrected base","input":{"id":"M","historical":1,"sourceNotice":"old:M"},"expected":{"movements":[{"id":"M","historical":1}],"notices":[{"id":"old:M"}]},"corrected":true},{"label":"new live input creates its live effect","input":{"id":"M","historical":0,"sourceNotice":null},"expected":{"movements":[{"id":"M","historical":0}],"notices":[{"id":"auto:M"}]},"corrected":true},{"label":"history has no old notice to import","input":{"id":"M","historical":1,"sourceNotice":null},"expected":{"movements":[{"id":"M","historical":1}],"notices":[]},"corrected":true}];
for(const test of cases){
const actual=run(test.input,test.corrected);
assert.deepEqual(actual,test.expected,test.label);
console.log(test.label,actual);
}
下次載入前核邊界
即時 M 建 auto:M;歷史 M 未附舊通知,按此規則就沒有通知行。
| 情況 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 新即時 movement | {"id":"M","historical":0,"sourceNotice":null} | {"movements":[{"id":"M","historical":0}],"notices":[{"id":"auto:M"}]} |
| 歷史未附舊通知 | {"id":"M","historical":1,"sourceNotice":null} | {"movements":[{"id":"M","historical":1}],"notices":[]} |
匯入 historical1 應否建立 auto:M?
查看參考答案
不應,保歷史 movement 及所附舊通知;自動即時結果只屬 historical0。