|
| 1 | +// Microsoft Teams process coverage protects real SDK CommonJS import ordering. |
| 2 | +import { execFile } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { promisify } from "node:util"; |
| 6 | +import { describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const execFileAsync = promisify(execFile); |
| 9 | + |
| 10 | +describe("sendMSTeamsActivityWithReference SDK import ordering", () => { |
| 11 | + it("keeps root exports intact when quoted behavior is the first SDK access", async () => { |
| 12 | + await execFileAsync( |
| 13 | + process.execPath, |
| 14 | + ["scripts/lib/plugin-npm-runtime-build.mjs", "extensions/msteams"], |
| 15 | + { cwd: process.cwd() }, |
| 16 | + ); |
| 17 | + const outDir = path.join(process.cwd(), "extensions/msteams/dist"); |
| 18 | + const proactiveArtifact = fs |
| 19 | + .readdirSync(outDir) |
| 20 | + .filter((entry) => entry.endsWith(".cjs")) |
| 21 | + .map((entry) => path.join(outDir, entry)) |
| 22 | + .find((entry) => |
| 23 | + fs |
| 24 | + .readFileSync(entry, "utf8") |
| 25 | + .includes('Object.defineProperty(exports, "sendMSTeamsActivityWithReference"'), |
| 26 | + ); |
| 27 | + if (!proactiveArtifact) { |
| 28 | + throw new Error("Microsoft Teams CommonJS runtime omitted the proactive send helper"); |
| 29 | + } |
| 30 | + const proactiveArtifactSource = fs.readFileSync(proactiveArtifact, "utf8"); |
| 31 | + expect(proactiveArtifactSource).toContain('import("@microsoft/teams.api")'); |
| 32 | + expect(proactiveArtifactSource).not.toContain("@microsoft/teams.api/dist/"); |
| 33 | + |
| 34 | + const fixture = ` |
| 35 | + import assert from "node:assert/strict"; |
| 36 | + import { createRequire } from "node:module"; |
| 37 | +
|
| 38 | + const require = createRequire(import.meta.url); |
| 39 | + const Module = require("node:module"); |
| 40 | + const originalLoad = Module._load; |
| 41 | + const universalStub = new Proxy( |
| 42 | + function universalStub() { |
| 43 | + return universalStub; |
| 44 | + }, |
| 45 | + { |
| 46 | + apply: () => universalStub, |
| 47 | + construct: () => universalStub, |
| 48 | + get: () => universalStub, |
| 49 | + }, |
| 50 | + ); |
| 51 | + const hostSdkStub = new Proxy( |
| 52 | + {}, |
| 53 | + { |
| 54 | + get: (_target, key) => { |
| 55 | + if (key === "createLazyRuntimeModule") { |
| 56 | + return (importer) => { |
| 57 | + let pending; |
| 58 | + return () => (pending ??= importer()); |
| 59 | + }; |
| 60 | + } |
| 61 | + if (key === "getOrCreateGlobalSingleton") { |
| 62 | + return (_key, create) => create(); |
| 63 | + } |
| 64 | + return universalStub; |
| 65 | + }, |
| 66 | + }, |
| 67 | + ); |
| 68 | + // The built plugin expects an installed OpenClaw host. Stub unrelated host SDK exports so |
| 69 | + // this child isolates the emitted Teams loader and the real pinned Teams CommonJS package. |
| 70 | + Module._load = function load(request, parent, isMain) { |
| 71 | + if (request.startsWith("openclaw/plugin-sdk/")) { |
| 72 | + return hostSdkStub; |
| 73 | + } |
| 74 | + return originalLoad.call(this, request, parent, isMain); |
| 75 | + }; |
| 76 | +
|
| 77 | + const { sendMSTeamsActivityWithReference } = |
| 78 | + require(process.env.OPENCLAW_MSTEAMS_PROACTIVE_ARTIFACT); |
| 79 | + const quotedCreates = []; |
| 80 | + const posts = []; |
| 81 | + const app = { |
| 82 | + client: { |
| 83 | + request: async () => ({}), |
| 84 | + post: async (url, activity) => { |
| 85 | + posts.push({ url, activity }); |
| 86 | + return { data: { id: "normal-proactive" } }; |
| 87 | + }, |
| 88 | + }, |
| 89 | + api: { |
| 90 | + serviceUrl: "https://smba.trafficmanager.net/amer", |
| 91 | + conversations: { |
| 92 | + activities: () => ({ |
| 93 | + create: async (activity) => { |
| 94 | + quotedCreates.push(activity); |
| 95 | + return { id: "quoted-replay" }; |
| 96 | + }, |
| 97 | + update: async () => ({}), |
| 98 | + delete: async () => ({}), |
| 99 | + }), |
| 100 | + }, |
| 101 | + }, |
| 102 | + }; |
| 103 | + const reference = { |
| 104 | + agent: { id: "28:bot", role: "bot" }, |
| 105 | + user: { id: "29:user" }, |
| 106 | + conversation: { |
| 107 | + |
| 108 | + conversationType: "groupChat", |
| 109 | + }, |
| 110 | + }; |
| 111 | +
|
| 112 | + // The structural app selects the quote path without loading the full Teams app first. |
| 113 | + await sendMSTeamsActivityWithReference( |
| 114 | + app, |
| 115 | + { ...reference, serviceUrl: "https://smba.trafficmanager.net/amer" }, |
| 116 | + { type: "message", text: "Recovered reply" }, |
| 117 | + { quoteActivityId: "incoming-activity" }, |
| 118 | + ); |
| 119 | + assert.equal( |
| 120 | + quotedCreates[0].text, |
| 121 | + '<quoted messageId="incoming-activity"/> Recovered reply', |
| 122 | + ); |
| 123 | +
|
| 124 | + const api = await import("@microsoft/teams.api"); |
| 125 | + assert.equal(typeof api.Client, "function"); |
| 126 | + assert.equal(typeof api.MessageActivityInput, "function"); |
| 127 | + assert.equal(typeof api.toActivityParams, "function"); |
| 128 | + assert.doesNotThrow(() => |
| 129 | + api.toActivityParams({ type: "message", text: "Direct conversion" }), |
| 130 | + ); |
| 131 | +
|
| 132 | + await assert.doesNotReject(() => |
| 133 | + sendMSTeamsActivityWithReference( |
| 134 | + app, |
| 135 | + { ...reference, serviceUrl: "https://smba.trafficmanager.net/teams" }, |
| 136 | + { type: "message", text: "Normal send" }, |
| 137 | + ), |
| 138 | + ); |
| 139 | + assert.equal(posts.length, 1); |
| 140 | + assert.equal( |
| 141 | + posts[0].url, |
| 142 | + "https://smba.trafficmanager.net/teams/v3/conversations/19:[email protected]/activities", |
| 143 | + ); |
| 144 | + assert.equal(posts[0].activity.text, "Normal send"); |
| 145 | + process.stdout.write("root-sdk-ok"); |
| 146 | + `; |
| 147 | + |
| 148 | + const { stdout, stderr } = await execFileAsync( |
| 149 | + process.execPath, |
| 150 | + ["--import", "tsx", "--input-type=module", "--eval", fixture], |
| 151 | + { |
| 152 | + cwd: process.cwd(), |
| 153 | + env: { |
| 154 | + ...process.env, |
| 155 | + NODE_DISABLE_COMPILE_CACHE: "1", |
| 156 | + OPENCLAW_MSTEAMS_PROACTIVE_ARTIFACT: proactiveArtifact, |
| 157 | + VITEST: undefined, |
| 158 | + }, |
| 159 | + }, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(stderr).toBe(""); |
| 163 | + expect(stdout).toBe("root-sdk-ok"); |
| 164 | + }, 30_000); |
| 165 | +}); |
0 commit comments