fix(test): внедрять позднее изменение GRE после COMMIT аудита намерения
Устаревший тест, не дефект продукта. Аудит configuration.intent для RouterOS-записи пишется на транзакционном клиенте (requestAccountingWrite), поэтому перехват pool.query не срабатывал: поздняя пауза, защита или изменение ревизии не внедрялись, и восстановление законно завершалось applied. Фикстур afterConfigurationIntentCommit принимает предикат по payload (method), тест GRE использует его; ожидания (unknown, без записи в RouterOS) не менялись. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
95b9d1cd52
commit
d3a347a8ca
2 files changed
+8
-13
No files matched your search
+4
-2
@@ -24,13 +24,15 @@ const audit = (args: unknown[], action: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Trigger fixture drift after the intent is durable and before the native call. */
|
/** Trigger fixture drift after the intent is durable and before the native call. */
|
||||||
export function afterConfigurationIntentCommit(kind: string, action: () => Promise<unknown>) {
|
export function afterConfigurationIntentCommit(kind: string | ((payload: Record<string, unknown>) => boolean), action: () => Promise<unknown>) {
|
||||||
let fired = false
|
let fired = false
|
||||||
|
const matches = typeof kind === "string" ? (payload: { object?: { kind?: unknown } }) => payload.object?.kind === kind : kind
|
||||||
const restore = proxyClientQueries(() => {
|
const restore = proxyClientQueries(() => {
|
||||||
let intent = false
|
let intent = false
|
||||||
return async (args, run) => {
|
return async (args, run) => {
|
||||||
const result = await run()
|
const result = await run()
|
||||||
if (audit(args, "configuration.intent")?.object?.kind === kind) intent = true
|
const payload = audit(args, "configuration.intent")
|
||||||
|
if (payload && matches(payload)) intent = true
|
||||||
if (args[0] === "COMMIT" && intent && !fired) { fired = true; await action() }
|
if (args[0] === "COMMIT" && intent && !fired) { fired = true; await action() }
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {canonicalGreSnapshot} from "../services/entity-snapshots.js"
|
|||||||
import {fingerprintPayload} from "../services/config-revisions.js"
|
import {fingerprintPayload} from "../services/config-revisions.js"
|
||||||
import greRoutes from "../routes/gre.js"
|
import greRoutes from "../routes/gre.js"
|
||||||
import {acceptGreReadCancellation} from "./mcp-gre-read-cancellation.js"
|
import {acceptGreReadCancellation} from "./mcp-gre-read-cancellation.js"
|
||||||
|
import {afterConfigurationIntentCommit} from "./fixtures/configuration-audit-commit.js"
|
||||||
|
|
||||||
type Collections=Record<string,Record<string,string>[]>
|
type Collections=Record<string,Record<string,string>[]>
|
||||||
interface Fixture{get:()=>Collections;set:(value:Collections)=>void;writes:()=>number;reads:()=>number;ignored:(v:boolean)=>void;lost:(v:boolean)=>void;afterCommand:(hook:(()=>Promise<void>)|undefined)=>void;dependencies:(value:Collections)=>void}
|
interface Fixture{get:()=>Collections;set:(value:Collections)=>void;writes:()=>number;reads:()=>number;ignored:(v:boolean)=>void;lost:(v:boolean)=>void;afterCommand:(hook:(()=>Promise<void>)|undefined)=>void;dependencies:(value:Collections)=>void}
|
||||||
@@ -113,21 +114,13 @@ export async function acceptGreRevisionRestore(actor:AuthUser,fixture:Fixture){
|
|||||||
fixture.set(method==="PUT"?{"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]}:seed())
|
fixture.set(method==="PUT"?{"/interface/gre":[],"/ip/address":[],"/ipv6/address":[]}:seed())
|
||||||
const payload=method==="PUT"?canonicalGreSnapshot({tunnels:[{...desired().tunnels.find(r=>r.name==="restore-new")!,innerAddresses:[],localInnerIp:""}]}):method==="DELETE"?{tunnels:[]}:canonicalGreSnapshot({tunnels:desired().tunnels.filter(r=>r.name==="restore-old")})
|
const payload=method==="PUT"?canonicalGreSnapshot({tunnels:[{...desired().tunnels.find(r=>r.name==="restore-new")!,innerAddresses:[],localInnerIp:""}]}):method==="DELETE"?{tunnels:[]}:canonicalGreSnapshot({tunnels:desired().tunnels.filter(r=>r.name==="restore-old")})
|
||||||
const id=await revision(payload),prepared=await plan(id);await approvePlan(prepared.planId,actor,true)
|
const id=await revision(payload),prepared=await plan(id);await approvePlan(prepared.planId,actor,true)
|
||||||
let fired=false
|
// Audits commit on transaction clients, so the drift hook wraps pool.connect rather than pool.query.
|
||||||
pool.query=((...args:unknown[])=>{
|
const late=afterConfigurationIntentCommit(payload=>payload.method===method,async()=>{
|
||||||
const sql=typeof args[0]==="string"?args[0]:(args[0]as{text?:string})?.text,params=args[1]as unknown[]|undefined
|
|
||||||
const pending=(query as(...args:unknown[])=>Promise<unknown>)(...args)
|
|
||||||
if(!fired&&sql?.startsWith("INSERT INTO mcp_audit")&¶ms?.[1]==="configuration.intent"&&String(params?.[3]).includes('"method":"'+method+'"')){
|
|
||||||
fired=true;return pending.then(async value=>{
|
|
||||||
if(mode==="PAUSE")await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1",[issued.connection.id])
|
if(mode==="PAUSE")await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"paused\"'::jsonb) WHERE id=$1",[issued.connection.id])
|
||||||
else if(mode==="REVISION")await query("UPDATE config_revisions SET payload='{}'::jsonb WHERE id=$1",[id])
|
else if(mode==="REVISION")await query("UPDATE config_revisions SET payload='{}'::jsonb WHERE id=$1",[id])
|
||||||
else await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"vlan",id:"*late",serverId,protection:"deny"}),actor.id])
|
else await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)",[protection,JSON.stringify({kind:"vlan",id:"*late",serverId,protection:"deny"}),actor.id])
|
||||||
return value
|
|
||||||
})
|
})
|
||||||
}
|
try{const count=fixture.writes(),op=await applyPlan(principal,prepared.planId,randomUUID());assert.equal(await wait(op.id),"unknown");assert.equal(late.fired(),true);assert.equal(fixture.writes(),count)}finally{late.restore();await query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"active\"'::jsonb) WHERE id=$1",[issued.connection.id])}
|
||||||
return pending
|
|
||||||
})as typeof pool.query
|
|
||||||
try{const count=fixture.writes(),op=await applyPlan(principal,prepared.planId,randomUUID());assert.equal(await wait(op.id),"unknown");assert.equal(fired,true);assert.equal(fixture.writes(),count)}finally{pool.query=query as typeof pool.query;await query("DELETE FROM mcp_protections WHERE id=$1",[protection]);await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{status}','\"active\"'::jsonb) WHERE id=$1",[issued.connection.id])}
|
|
||||||
}
|
}
|
||||||
for(const fault of ["ignored","lost","collateral","late-binding","wrong-default-key"]){
|
for(const fault of ["ignored","lost","collateral","late-binding","wrong-default-key"]){
|
||||||
fixture.set(seed());const prepared=await plan(selected),count=fixture.writes()
|
fixture.set(seed());const prepared=await plan(selected),count=fixture.writes()
|
||||||
|
|||||||
Reference in new issue
Block a user