Initial Daily Campaign Agent release
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { access, mkdtemp, stat } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { SkillCatalog } from "../src/skills.js";
|
||||
import { MemoryModelConnectionStore, validateBaseUrl } from "../src/model-store.js";
|
||||
import { adaptPiEvent } from "../src/pi-event-adapter.js";
|
||||
import { MemoryEventStore } from "../src/event-store.js";
|
||||
|
||||
const projectRoot=path.resolve(import.meta.dirname,"../../..");
|
||||
describe("skill catalog", () => {
|
||||
it("exposes only the vendored daily campaign skill", async () => { const catalog=await SkillCatalog.create(projectRoot,await mkdtemp(path.join(os.tmpdir(),"asm-agent-")),path.join(os.tmpdir(),"must-not-load")); expect(catalog.list().map((s)=>s.name)).toEqual(["daily-campaign-triage"]); expect(catalog.list()[0]?.source).toBe("built-in"); });
|
||||
it("matches only the built-in canonical SKILL path", async () => { const catalog=await SkillCatalog.create(projectRoot,await mkdtemp(path.join(os.tmpdir(),"asm-agent-")),""); const matched=await catalog.matchRead(path.join(projectRoot,".agents/skills/daily-campaign-triage/SKILL.md"),projectRoot); expect(matched?.name).toBe("daily-campaign-triage"); expect(await catalog.matchRead(path.join(projectRoot,".agents/skills/ads-analysis/SKILL.md"),projectRoot)).toBeUndefined(); });
|
||||
});
|
||||
describe("base URL guard", () => {
|
||||
it("allows https public URLs", async () => expect((await validateBaseUrl("https://example.com/v1")).hostname).toBe("example.com"));
|
||||
it("rejects credentials, dangerous protocols and metadata", async () => { await expect(validateBaseUrl("https://user:pass@example.com")).rejects.toThrow(/用户信息/); await expect(validateBaseUrl("file:///etc/passwd")).rejects.toThrow(/HTTPS/); await expect(validateBaseUrl("https://169.254.169.254/latest")).rejects.toThrow(/metadata|内网/); });
|
||||
});
|
||||
describe("persistent model config",()=>{
|
||||
it("survives store recreation, preserves a blank key and clears only explicitly",async()=>{ const root=await mkdtemp(path.join(os.tmpdir(),"asm-model-")); const file=path.join(root,"private","model.json"); const first=new MemoryModelConnectionStore(false,file); await first.configure("s",{providerId:"openai",api:"openai-responses",apiKey:"sk-persistent-secret",modelId:"gpt-5",supportsImages:false}); expect((await stat(file)).mode&0o777).toBe(0o600); const second=new MemoryModelConnectionStore(false,file); await second.loadPersistent(); expect((await second.applyPersistent("next"))?.configured).toBe(true); await second.configure("next",{providerId:"openai",api:"openai-responses",modelId:"gpt-5.1",supportsImages:false}); expect(second.getRedacted("next")?.configured).toBe(true); await second.clearCredentials("next"); await expect(access(file)).rejects.toThrow(); });
|
||||
});
|
||||
describe("Pi event adapter", () => {
|
||||
it("coalesces deltas and ignores unknown events", () => { const store=new MemoryEventStore(); const context={sessionId:"s",runId:()=>"r",eventStore:store,assistantText:new Map<string,string>()}; adaptPiEvent({type:"unknown"},context); adaptPiEvent({type:"message_update",messageId:"m",assistantMessageEvent:{type:"text_delta",delta:"你"}},context); adaptPiEvent({type:"message_update",messageId:"m",assistantMessageEvent:{type:"text_delta",delta:"好"}},context); adaptPiEvent({type:"message_end",message:{id:"m",role:"assistant",content:[{type:"text",text:"你好"}],stopReason:"stop"}},context); expect(store.listAfter("s")).toHaveLength(2); expect(context.assistantText.get("m")).toBe("你好"); });
|
||||
it("assigns distinct fallback IDs to assistant turns around tools",()=>{ const store=new MemoryEventStore(); const context={sessionId:"s",runId:()=>"r",eventStore:store,assistantText:new Map<string,string>(),assistantState:{nextMessageNumber:1,pendingDelta:""}}; for(const text of ["准备分析","最终报告"]){ adaptPiEvent({type:"message_update",assistantMessageEvent:{type:"text_delta",delta:text}},context); adaptPiEvent({type:"message_end",message:{role:"assistant",content:[{type:"text",text}],stopReason:"stop"}},context); } const completed=store.listAfter("s").filter((event)=>event.type==="assistant.completed"); expect(completed.map((event)=>event.payload.messageId)).toEqual(["assistant_r_1","assistant_r_2"]); });
|
||||
it("recovers final text and exposes provider errors",()=>{ const store=new MemoryEventStore(); const errors:string[]=[]; const context={sessionId:"s",runId:()=>"r",eventStore:store,assistantText:new Map<string,string>(),onModelError:(message:string)=>errors.push(message)}; adaptPiEvent({type:"message_end",message:{role:"assistant",content:[{type:"text",text:"完整回复"}],stopReason:"stop"}},context); adaptPiEvent({type:"message_end",message:{role:"assistant",content:[],stopReason:"error",errorMessage:"Bad model id"}},context); expect(store.listAfter("s").some((event)=>event.type==="assistant.delta"&&event.payload.delta==="完整回复")).toBe(true); expect(errors).toEqual(["Bad model id"]); });
|
||||
it("treats an empty final response as a visible model error",()=>{ const errors:string[]=[]; adaptPiEvent({type:"message_end",message:{role:"assistant",content:[],stopReason:"stop"}},{sessionId:"s",runId:()=>"r",eventStore:new MemoryEventStore(),assistantText:new Map(),onModelError:(message)=>errors.push(message)}); expect(errors[0]).toMatch(/空响应/); });
|
||||
it("sanitizes tool arguments", () => { const store=new MemoryEventStore(); adaptPiEvent({type:"tool_execution_start",toolCallId:"t",toolName:"x",args:{apiKey:"secret"}},{sessionId:"s",runId:()=>"r",eventStore:store,assistantText:new Map()}); expect(JSON.stringify(store.listAfter("s"))).not.toContain("secret"); });
|
||||
});
|
||||
Reference in New Issue
Block a user