Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
formatMessage,
formatPerson,
getConversationDisplayName,
makeShortId,
parseRecordings,
stripHtml,
transcriptFilename,
Expand All @@ -51,12 +52,43 @@ function loadChatIndex(): FormattedChat[] {
return JSON.parse(readFileSync(LAST_CHATS_FILE, "utf-8"));
}

function resolveConversationId(idOrShort: string): string {
export function resolveConversationId(
idOrShort: string,
chats: FormattedChat[] = loadChatIndex(),
): string {
// Full conversation IDs (e.g. "19:...@thread.v2") pass through untouched.
if (idOrShort.includes(":")) return idOrShort;
const chats = loadChatIndex();
for (const c of chats) {
if (c.short_id === idOrShort) return c.id;

// Exact short-id hit. The chat index can hold several rows for the same
// thread (the conversations API returns duplicate rows), so dedupe by
// full ID before deciding.
const exactIds = new Set(
chats.filter((c) => c.short_id === idOrShort).map((c) => c.id),
);
if (exactIds.size === 1) return [...exactIds][0];

// Suffix fallback: `chats` re-derives short IDs on every run, so a short
// ID captured earlier may no longer be assigned. Any unique tail (>= 4
// characters, the same minimum `chats` uses) of the short ID or of the
// conversation ID body still resolves.
if (idOrShort.length >= 4) {
const suffixIds = new Set(
chats
.filter(
(c) =>
c.short_id.endsWith(idOrShort) ||
makeShortId(c.id).endsWith(idOrShort),
)
.map((c) => c.id),
);
if (suffixIds.size === 1) return [...suffixIds][0];
if (suffixIds.size > 1) {
console.error(`ID '${idOrShort}' matches multiple conversations:`);
for (const id of suffixIds) console.error(` ${id}`);
process.exit(1);
}
}

console.error(
`Short ID '${idOrShort}' not found. Run \`teams chats\` first.`,
);
Expand Down
80 changes: 80 additions & 0 deletions tests/resolve-conversation-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, expect, it, vi } from "vitest";
import { resolveConversationId } from "../src/cli.js";
import type { FormattedChat } from "../src/formatting.js";

function chat(id: string, short_id: string): FormattedChat {
return { short_id, name: `Chat ${short_id}`, type: "Chat", preview: "", time: "", id };
}

const OSAMA = "19:63d43783-f462-431d-9a35-02c4bc67344e_b5bb788f-e279-4f3d-acff-0790cbe8ea6f@unq.gbl.spaces";
const OTHER = "19:76559d43-ef26-489f-a90b-06467794080b_11111111-2222-3333-4444-555555555555@unq.gbl.spaces";

describe("resolveConversationId", () => {
it("passes through full conversation IDs", () => {
expect(resolveConversationId(OSAMA, [])).toBe(OSAMA);
});

it("resolves an exact short id", () => {
expect(resolveConversationId("ab12", [chat(OSAMA, "ab12"), chat(OTHER, "cd34")])).toBe(OSAMA);
});

it("resolves an exact short id when the thread has duplicate index rows", () => {
const idx = [chat(OSAMA, "ab12"), chat(OSAMA, "zz99"), chat(OTHER, "cd34")];
expect(resolveConversationId("ab12", idx)).toBe(OSAMA);
});

it("resolves a stale short id via unique-suffix fallback", () => {
// `chats` re-assigned the short id since it was captured; the old
// value is still a tail of the new assignment.
const idx = [chat(OSAMA, "01ab12"), chat(OTHER, "cd34")];
expect(resolveConversationId("ab12", idx)).toBe(OSAMA);
});

it("resolves a unique suffix of duplicate rows that share one thread", () => {
// The conversations API returns duplicate rows for the same thread
// with different short ids; a shared tail must still resolve.
const idx = [chat(OSAMA, "c3ea6f"), chat(OSAMA, "d7ea6f"), chat(OTHER, "cd34")];
expect(resolveConversationId("ea6f", idx)).toBe(OSAMA);
});

it("resolves a unique suffix of the conversation id body", () => {
const idx = [chat(OSAMA, "ab12"), chat(OTHER, "cd34")];
expect(resolveConversationId("cbe8ea6f", idx)).toBe(OSAMA);
});

it("lists candidates when a suffix matches different threads", () => {
const err = vi.spyOn(console, "error").mockImplementation(() => {});
const exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
try {
const idx = [chat(OSAMA, "c3ea6f"), chat(OTHER, "d7ea6f")];
resolveConversationId("ea6f", idx);
expect(exit).toHaveBeenCalledWith(1);
expect(err).toHaveBeenCalledWith(expect.stringContaining("matches multiple conversations"));
} finally {
err.mockRestore();
exit.mockRestore();
}
});

it("exits when nothing matches", () => {
const err = vi.spyOn(console, "error").mockImplementation(() => {});
const exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
try {
resolveConversationId("ffff", [chat(OSAMA, "ab12")]);
expect(exit).toHaveBeenCalledWith(1);
} finally {
err.mockRestore();
exit.mockRestore();
}
});

it("does not suffix-match inputs shorter than 4 characters", () => {
const exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
try {
resolveConversationId("f6f", [chat(OSAMA, "c3f6f")]);
expect(exit).toHaveBeenCalledWith(1);
} finally {
exit.mockRestore();
}
});
});