Let bots set a picture from a file they can see
A coordinator asked to set an uploaded logo as a workspace picture could not: the picture tools take a media id, and nothing ever gave the bot one. Attachments reached the model as "[Attached image \"logo.png\"]", every file tool answers with a [file:ID] ref, and an SVG is deliberately stored as a document rather than an image. - copyMediaAsPicture copies an approved image into a new picture row (bot-image/workspace-image). The source keeps its row and bytes, so a message that attached it still loads and changing a picture can never take a file away from a conversation. - grabPictureSource resolves a picture by [file:ID] (the same scope gate every file tool uses) or by the user's own upload; parseFileRef reads the [file:12] form. - The new set_picture tool ships on every run: a bot sets its own face, the coordinator can also dress a worker. update_workspace gained picture_file_id, and setBotPicture/setProfileImage share the copy and the delete. - removeReplacedPicture deletes a replaced picture only when it really is one, never a chat upload. - uploadAndRecordMedia writes images in their own format: PNG stays PNG with its transparency, WebP stays WebP, GIF becomes PNG, everything else JPEG, and the row records the real mime type (it used to say jpeg for every image). - Pictures change live: BotUpdatedData carries image_media_id and a new server:workspace-updated pushes workspace pictures, relayed between processes. - Attachments are labelled with their media id, and BotFilesNote tells a bot that a [file:ID] can become a picture. - grabRunScopes is now the single source of a run's reachable folders, shared by the file tools, imports and the picture tools.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import removeReplacedPicture from "./remove-replaced-picture";
|
||||
|
||||
function spy({ is_picture = true }: { is_picture?: boolean } = {}) {
|
||||
const asked: unknown[] = [];
|
||||
const removed: unknown[] = [];
|
||||
|
||||
return {
|
||||
asked,
|
||||
removed,
|
||||
isPicture: async (params: { media_id: number | string; target: "bot" | "workspace" }) => {
|
||||
asked.push(params);
|
||||
return is_picture;
|
||||
},
|
||||
remove: async (params: unknown) => {
|
||||
removed.push(params);
|
||||
return { success: true };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("removeReplacedPicture", () => {
|
||||
test("deletes the picture a bot no longer shows", async () => {
|
||||
const deps = spy();
|
||||
|
||||
await removeReplacedPicture({
|
||||
previous: 4,
|
||||
next: 9,
|
||||
user_id: 3,
|
||||
isPicture: deps.isPicture,
|
||||
remove: deps.remove,
|
||||
});
|
||||
|
||||
expect(deps.asked).toEqual([{ media_id: 4, target: "bot" }]);
|
||||
expect(deps.removed).toEqual([{ id: 4, user_id: 3 }]);
|
||||
});
|
||||
|
||||
test("never deletes a row that isn't a picture, such as a chat upload", async () => {
|
||||
const deps = spy({ is_picture: false });
|
||||
|
||||
await removeReplacedPicture({
|
||||
previous: 4,
|
||||
next: 9,
|
||||
user_id: 3,
|
||||
isPicture: deps.isPicture,
|
||||
remove: deps.remove,
|
||||
});
|
||||
|
||||
expect(deps.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("leaves a bot that had no picture alone", async () => {
|
||||
const deps = spy();
|
||||
|
||||
await removeReplacedPicture({ previous: null, next: 9, user_id: 3, isPicture: deps.isPicture, remove: deps.remove });
|
||||
|
||||
expect(deps.asked).toHaveLength(0);
|
||||
expect(deps.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("keeps the picture that is still in use, e.g. naming it twice", async () => {
|
||||
const deps = spy();
|
||||
|
||||
await removeReplacedPicture({ previous: 4, next: 4, user_id: 3, isPicture: deps.isPicture, remove: deps.remove });
|
||||
await removeReplacedPicture({ previous: "4", next: 4, user_id: 3, isPicture: deps.isPicture, remove: deps.remove });
|
||||
|
||||
expect(deps.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("asks for the right kind of picture, a workspace's or a bot's", async () => {
|
||||
const deps = spy();
|
||||
|
||||
await removeReplacedPicture({
|
||||
previous: 4,
|
||||
next: null,
|
||||
user_id: 3,
|
||||
target: "workspace",
|
||||
isPicture: deps.isPicture,
|
||||
remove: deps.remove,
|
||||
});
|
||||
|
||||
expect(deps.asked).toEqual([{ media_id: 4, target: "workspace" }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user