Skip to content

Commit e8c3fbc

Browse files
authored
test(msteams): remove copied store and own state fixture cleanup (#134973)
1 parent 6dd1c03 commit e8c3fbc

4 files changed

Lines changed: 64 additions & 95 deletions

File tree

‎extensions/msteams/src/conversation-store-state.test.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
// Msteams tests cover conversation store state plugin behavior.
22
import crypto from "node:crypto";
33
import fs from "node:fs";
4-
import os from "node:os";
54
import path from "node:path";
65
import {
76
createPluginStateKeyedStoreForTests,
87
resetPluginStateStoreForTests,
98
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
10-
import { beforeEach, describe, expect, it } from "vitest";
9+
import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env";
10+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
1111
import { createMSTeamsConversationStoreState } from "./conversation-store-state.js";
1212
import type { StoredConversationReference } from "./conversation-store.js";
1313
import { setMSTeamsRuntime } from "./runtime.js";
1414
import { msteamsRuntimeStub } from "./test-support/runtime.js";
1515

16+
const tempDirs = useAutoCleanupTempDirTracker((cleanup) =>
17+
afterAll(() => {
18+
resetPluginStateStoreForTests();
19+
cleanup();
20+
}),
21+
);
22+
1623
function conversationStateKey(conversationId: string): string {
1724
return crypto.createHash("sha256").update(conversationId).digest("hex");
1825
}
@@ -24,7 +31,7 @@ describe("msteams conversation store (plugin state)", () => {
2431
});
2532

2633
it("filters expired SQLite entries while preserving entries without lastSeenAt", async () => {
27-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
34+
const stateDir = tempDirs.make("openclaw-msteams-store-");
2835
const env: NodeJS.ProcessEnv = {
2936
...process.env,
3037
OPENCLAW_STATE_DIR: stateDir,
@@ -82,7 +89,7 @@ describe("msteams conversation store (plugin state)", () => {
8289
});
8390

8491
it("ignores a stale legacy JSON file at runtime", async () => {
85-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
92+
const stateDir = tempDirs.make("openclaw-msteams-store-");
8693
const env: NodeJS.ProcessEnv = {
8794
...process.env,
8895
OPENCLAW_STATE_DIR: stateDir,
@@ -123,7 +130,7 @@ describe("msteams conversation store (plugin state)", () => {
123130
});
124131

125132
it("hashes external conversation ids before using plugin-state keys", async () => {
126-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
133+
const stateDir = tempDirs.make("openclaw-msteams-store-");
127134
const longConversationId = `a:${"x".repeat(900)}`;
128135
const store = createMSTeamsConversationStoreState({ stateDir });
129136

@@ -140,7 +147,7 @@ describe("msteams conversation store (plugin state)", () => {
140147
});
141148

142149
it("serializes concurrent upserts so sparse activities preserve independent fields", async () => {
143-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
150+
const stateDir = tempDirs.make("openclaw-msteams-store-");
144151
const store = createMSTeamsConversationStoreState({ stateDir });
145152

146153
await store.upsert("conv-race", {
@@ -174,7 +181,7 @@ describe("msteams conversation store (plugin state)", () => {
174181
});
175182

176183
it("keeps newest conversations by lastSeenAt at the row cap", async () => {
177-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
184+
const stateDir = tempDirs.make("openclaw-msteams-store-");
178185
const env: NodeJS.ProcessEnv = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
179186
const sqliteStore = createPluginStateKeyedStoreForTests<StoredConversationReference>(
180187
"msteams",
@@ -208,7 +215,7 @@ describe("msteams conversation store (plugin state)", () => {
208215
});
209216

210217
it("treats timestamp-less conversations as oldest during later cap pruning", async () => {
211-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
218+
const stateDir = tempDirs.make("openclaw-msteams-store-");
212219
const env: NodeJS.ProcessEnv = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
213220
const sqliteStore = createPluginStateKeyedStoreForTests<StoredConversationReference>(
214221
"msteams",

‎extensions/msteams/src/conversation-store.shared.test.ts‎

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,34 @@
11
// Msteams tests cover conversation store.shared plugin behavior.
2-
import fs from "node:fs";
3-
import os from "node:os";
4-
import path from "node:path";
52
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
6-
import { beforeEach, describe, expect, it, vi } from "vitest";
7-
import {
8-
findPreferredDmConversationByUserId,
9-
mergeStoredConversationReference,
10-
normalizeStoredConversationId,
11-
toConversationStoreEntries,
12-
} from "./conversation-store-helpers.js";
3+
import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env";
4+
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
135
import { createMSTeamsConversationStoreState } from "./conversation-store-state.js";
14-
import type {
15-
MSTeamsConversationStore,
16-
MSTeamsConversationStoreEntry,
17-
StoredConversationReference,
18-
} from "./conversation-store.js";
196
import { setMSTeamsRuntime } from "./runtime.js";
207
import { msteamsRuntimeStub } from "./test-support/runtime.js";
218

22-
type StoreFactory = {
23-
name: string;
24-
createStore: () => Promise<MSTeamsConversationStore>;
25-
};
26-
27-
function createMemoryConversationStore(
28-
initial: MSTeamsConversationStoreEntry[] = [],
29-
): MSTeamsConversationStore {
30-
const map = new Map<string, StoredConversationReference>();
31-
for (const { conversationId, reference } of initial) {
32-
map.set(normalizeStoredConversationId(conversationId), reference);
33-
}
34-
35-
const findPreferredDmByUserId = async (
36-
id: string,
37-
): Promise<MSTeamsConversationStoreEntry | null> =>
38-
findPreferredDmConversationByUserId(toConversationStoreEntries(map.entries()), id);
39-
40-
return {
41-
upsert: async (conversationId, reference) => {
42-
const normalizedId = normalizeStoredConversationId(conversationId);
43-
map.set(
44-
normalizedId,
45-
mergeStoredConversationReference(
46-
map.get(normalizedId),
47-
reference,
48-
new Date().toISOString(),
49-
),
50-
);
51-
},
52-
get: async (conversationId) => map.get(normalizeStoredConversationId(conversationId)) ?? null,
53-
list: async () => toConversationStoreEntries(map.entries()),
54-
remove: async (conversationId) => map.delete(normalizeStoredConversationId(conversationId)),
55-
findPreferredDmByUserId,
56-
};
9+
const tempDirs = useAutoCleanupTempDirTracker((cleanup) =>
10+
afterAll(() => {
11+
resetPluginStateStoreForTests();
12+
cleanup();
13+
}),
14+
);
15+
16+
function createStore() {
17+
const stateDir = tempDirs.make("openclaw-msteams-store-");
18+
return createMSTeamsConversationStoreState({
19+
env: { ...process.env, OPENCLAW_STATE_DIR: stateDir },
20+
ttlMs: 60_000,
21+
});
5722
}
5823

59-
const storeFactories: StoreFactory[] = [
60-
{
61-
name: "state",
62-
createStore: async () => {
63-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-store-"));
64-
return createMSTeamsConversationStoreState({
65-
env: { ...process.env, OPENCLAW_STATE_DIR: stateDir },
66-
ttlMs: 60_000,
67-
});
68-
},
69-
},
70-
{
71-
name: "memory",
72-
createStore: async () => createMemoryConversationStore(),
73-
},
74-
];
75-
76-
describe.each(storeFactories)("msteams conversation store ($name)", ({ createStore }) => {
24+
describe("msteams conversation store ('state')", () => {
7725
beforeEach(() => {
7826
resetPluginStateStoreForTests();
7927
setMSTeamsRuntime(msteamsRuntimeStub);
8028
});
8129

8230
it("normalizes conversation ids consistently", async () => {
83-
const store = await createStore();
31+
const store = createStore();
8432

8533
await store.upsert("conv-norm;messageid=123", {
8634
conversation: { id: "conv-norm" },
@@ -103,7 +51,7 @@ describe.each(storeFactories)("msteams conversation store ($name)", ({ createSto
10351
});
10452

10553
it("upserts, lists, removes, and resolves users by both AAD and Bot Framework ids", async () => {
106-
const store = await createStore();
54+
const store = createStore();
10755

10856
vi.useFakeTimers();
10957
try {
@@ -185,7 +133,7 @@ describe.each(storeFactories)("msteams conversation store ($name)", ({ createSto
185133
});
186134

187135
it("preserves existing timezone when upsert omits timezone", async () => {
188-
const store = await createStore();
136+
const store = createStore();
189137

190138
vi.useFakeTimers();
191139
try {
@@ -220,7 +168,7 @@ describe.each(storeFactories)("msteams conversation store ($name)", ({ createSto
220168
});
221169

222170
it("prefers the freshest personal conversation for repeated upserts of the same user", async () => {
223-
const store = await createStore();
171+
const store = createStore();
224172

225173
vi.useFakeTimers();
226174
try {

‎extensions/msteams/src/polls.test.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Msteams tests cover polls plugin behavior.
22
import crypto from "node:crypto";
33
import fs from "node:fs";
4-
import os from "node:os";
54
import path from "node:path";
65
import {
76
createPluginStateKeyedStoreForTests,
87
resetPluginStateStoreForTests,
98
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
10-
import { beforeEach, describe, expect, it } from "vitest";
9+
import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env";
10+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
1111
import {
1212
buildMSTeamsPollCard,
1313
createMSTeamsPollStoreState,
@@ -17,6 +17,13 @@ import {
1717
import { setMSTeamsRuntime } from "./runtime.js";
1818
import { msteamsRuntimeStub } from "./test-support/runtime.js";
1919

20+
const tempDirs = useAutoCleanupTempDirTracker((cleanup) =>
21+
afterAll(() => {
22+
resetPluginStateStoreForTests();
23+
cleanup();
24+
}),
25+
);
26+
2027
describe("msteams polls", () => {
2128
beforeEach(() => {
2229
resetPluginStateStoreForTests();
@@ -50,7 +57,7 @@ describe("msteams polls", () => {
5057
});
5158

5259
it("stores and records poll votes", async () => {
53-
const home = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
60+
const home = tempDirs.make("openclaw-msteams-polls-");
5461
const store = createMSTeamsPollStoreState({ homedir: () => home });
5562
await store.createPoll({
5663
id: "poll-2",
@@ -73,7 +80,7 @@ describe("msteams polls", () => {
7380
});
7481

7582
it("deduplicates selections before enforcing maxSelections", async () => {
76-
const home = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
83+
const home = tempDirs.make("openclaw-msteams-polls-");
7784
const store = createMSTeamsPollStoreState({ homedir: () => home });
7885
await store.createPoll({
7986
id: "poll-dedupe",
@@ -103,7 +110,7 @@ describe("state poll store", () => {
103110
});
104111

105112
it("ignores legacy JSON polls at runtime", async () => {
106-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
113+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
107114
const filePath = path.join(stateDir, "msteams-polls.json");
108115
await fs.promises.writeFile(
109116
filePath,
@@ -141,7 +148,7 @@ describe("state poll store", () => {
141148
});
142149

143150
it("hashes external poll ids before using plugin-state keys", async () => {
144-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
151+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
145152
const store = createMSTeamsPollStoreState({ stateDir });
146153
const longPollId = `poll-${"x".repeat(900)}`;
147154

@@ -165,7 +172,7 @@ describe("state poll store", () => {
165172
});
166173

167174
it("serializes concurrent votes for the same poll", async () => {
168-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
175+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
169176
const store = createMSTeamsPollStoreState({ stateDir });
170177
await store.createPoll({
171178
id: "poll-race",
@@ -193,7 +200,7 @@ describe("state poll store", () => {
193200
{ selections: ["0", "1x"], expected: ["0"] },
194201
{ selections: ["+0", "0x1", "1"], expected: ["0", "1"] },
195202
])("accepts only strict decimal poll selections", async ({ selections, expected }) => {
196-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
203+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
197204
const store = createMSTeamsPollStoreState({ stateDir });
198205
await store.createPoll({
199206
id: "poll-strict-selections",
@@ -214,7 +221,7 @@ describe("state poll store", () => {
214221
});
215222

216223
it("keeps large vote maps split across bounded rows", async () => {
217-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
224+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
218225
const store = createMSTeamsPollStoreState({ stateDir });
219226
const votes = Object.fromEntries(
220227
Array.from({ length: 500 }, (_, index) => [
@@ -239,7 +246,7 @@ describe("state poll store", () => {
239246
});
240247

241248
it("deletes vote buckets when pruning over the poll cap", async () => {
242-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-polls-"));
249+
const stateDir = tempDirs.make("openclaw-msteams-polls-");
243250
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
244251
const metadataStore = createPluginStateKeyedStoreForTests<Omit<MSTeamsPoll, "votes">>(
245252
"msteams",

‎extensions/msteams/src/sso-token-store.test.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
// Msteams tests cover sso token store plugin behavior.
22
import fs from "node:fs/promises";
3-
import os from "node:os";
43
import path from "node:path";
54
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
6-
import { beforeEach, describe, expect, it } from "vitest";
5+
import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env";
6+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
77
import { setMSTeamsRuntime } from "./runtime.js";
88
import { createMSTeamsSsoTokenStoreFs } from "./sso-token-store.js";
99
import { msteamsRuntimeStub } from "./test-support/runtime.js";
1010

11+
const tempDirs = useAutoCleanupTempDirTracker((cleanup) =>
12+
afterAll(() => {
13+
resetPluginStateStoreForTests();
14+
cleanup();
15+
}),
16+
);
17+
1118
describe("msteams sso token store (plugin state)", () => {
1219
beforeEach(() => {
1320
resetPluginStateStoreForTests();
1421
setMSTeamsRuntime(msteamsRuntimeStub);
1522
});
1623

1724
it("keeps distinct tokens when connectionName and userId contain the legacy delimiter", async () => {
18-
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-sso-"));
25+
const stateDir = tempDirs.make("openclaw-msteams-sso-");
1926
const storePath = path.join(stateDir, "msteams-sso-tokens.json");
2027
const store = createMSTeamsSsoTokenStoreFs({ storePath });
2128

@@ -45,7 +52,7 @@ describe("msteams sso token store (plugin state)", () => {
4552
});
4653

4754
it("ignores legacy flat-key token files at runtime", async () => {
48-
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-sso-legacy-"));
55+
const stateDir = tempDirs.make("openclaw-msteams-sso-legacy-");
4956
const storePath = path.join(stateDir, "msteams-sso-tokens.json");
5057
await fs.writeFile(
5158
storePath,
@@ -78,7 +85,7 @@ describe("msteams sso token store (plugin state)", () => {
7885
});
7986

8087
it("keeps plugin-state keys bounded for long Teams identifiers", async () => {
81-
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-sso-long-"));
88+
const stateDir = tempDirs.make("openclaw-msteams-sso-long-");
8289
const store = createMSTeamsSsoTokenStoreFs({ stateDir });
8390
const token = {
8491
connectionName: `conn-${"c".repeat(1000)}`,

0 commit comments

Comments
 (0)