Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jan 7, 2025
1 parent bfb7b21 commit 0e6dc4d
Showing 1 changed file with 119 additions and 76 deletions.
195 changes: 119 additions & 76 deletions src/tests/twitter.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,144 @@ const tweetId = "1864386797788385455";
const userId = "[email protected]";
const username = "bracesproul";

describe.skip("Can use Arcade to call the Twitter API", () => {
const arcade = new Arcade({
apiKey: process.env.ARCADE_API_KEY,
});
describe.skip("Arcade", () => {
describe("Can use Arcade to call the Twitter API", () => {
const arcade = new Arcade({
apiKey: process.env.ARCADE_API_KEY,
});

it("Can load tweets by a user ID", async () => {
const result = await arcade.tools.execute({
tool_name: "X.SearchRecentTweetsByUsername",
inputs: {
username,
max_results: 1,
},
user_id: userId,
it("Can load tweets by a user ID", async () => {
const result = await arcade.tools.execute({
tool_name: "X.SearchRecentTweetsByUsername",
inputs: {
username,
max_results: 1,
},
user_id: userId,
});

console.log("Result\n");
console.dir(result, { depth: null });
expect(result).toBeDefined();
});

console.log("Result\n");
console.dir(result, { depth: null });
expect(result).toBeDefined();
it("Can load a single tweet by ID", async () => {
const result = await arcade.tools.execute({
tool_name: "X.LookupTweetById",
inputs: { tweet_id: tweetId },
user_id: userId,
});

console.log("Result\n");
console.dir(result, { depth: null });
expect(result).toBeDefined();
});
});

it("Can load a single tweet by ID", async () => {
const result = await arcade.tools.execute({
tool_name: "X.LookupTweetById",
inputs: { tweet_id: tweetId },
user_id: userId,
describe("Can use the Twitter Client with Arcade", () => {
const twitterTokens = {
twitterToken: process.env.TWITTER_USER_TOKEN || "",
twitterTokenSecret: process.env.TWITTER_USER_TOKEN_SECRET || "",
};

it("Can upload a tweet", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const tweetText = "test 123 hello world!";
const result = await client.uploadTweet({
text: tweetText,
});

expect(result.errors).not.toBeDefined();
expect(result.data).toBeDefined();
expect(result.data.id).toBeDefined();
expect(result.data.text).toBe(tweetText);
});

console.log("Result\n");
console.dir(result, { depth: null });
expect(result).toBeDefined();
it("Can upload media", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageBuffer = await fs.readFile(
"src/tests/data/langchain_logo.png",
);

const result = await client.uploadMedia(imageBuffer, "image/png");
console.log("Media ID", result);
expect(result).toBeDefined();
});

it("Can upload a tweet with media", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageBuffer = await fs.readFile(
"src/tests/data/langchain_logo.png",
);
const tweetText = "test 123 hello world! (with image)";

const result = await client.uploadTweet({
text: tweetText,
media: {
media: imageBuffer,
mimeType: "image/png",
},
});

expect(result).toBeDefined();
});

it("Can upload this image", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageBuffer = await fs.readFile(
"src/tests/data/langchain_logo_2.png",
);
const imageBase64 = imageBuffer.toString("base64");

const imageMimeType = extractMimeTypeFromBase64(imageBase64);
if (!imageMimeType) {
throw new Error("Could not determine image mime type");
}
const result = await client.uploadMedia(imageBuffer, imageMimeType);
console.log("result", result);
});

it("Can upload image from URL", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageUrl =
"https://miro.medium.com/v2/resize:fit:1200/1*-PlFCd_VBcALKReO3ZaOEg.png";

const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}

const imageBuffer = Buffer.from(await response.arrayBuffer());
const contentType = response.headers.get("content-type") || "image/jpeg";
console.log("contentType", contentType);
const result = await client.uploadMedia(imageBuffer, contentType);
console.log("result", result);
});
});
});

describe("TwitterClient wrapper", () => {
const twitterTokens = {
twitterToken: process.env.TWITTER_USER_TOKEN || "",
twitterTokenSecret: process.env.TWITTER_USER_TOKEN_SECRET || "",
};
describe("Basic Twitter Auth", () => {
const client = TwitterClient.fromBasicTwitterAuth();

it("Can read a tweet from ID", async () => {
const tweet = await client.getTweet(tweetId);
expect(tweet).toBeDefined();
console.log("Tweet\n");
console.dir(tweet, { depth: null });
});

it("Can upload a tweet", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const tweetText = "test 123 hello world!";
it("Can post a text only tweet", async () => {
const result = await client.uploadTweet({
text: tweetText,
text: "test 123 hello world!",
});

expect(result.errors).not.toBeDefined();
expect(result.data).toBeDefined();
expect(result.data.id).toBeDefined();
expect(result.data.text).toBe(tweetText);
expect(result.data.text).toBe("test 123 hello world!");
});

it("Can upload media", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageBuffer = await fs.readFile("src/tests/data/langchain_logo.png");

const result = await client.uploadMedia(imageBuffer, "image/png");
console.log("Media ID", result);
expect(result).toBeDefined();
});

it("Can upload a tweet with media", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
it("Can post a text and media tweet", async () => {
const imageBuffer = await fs.readFile("src/tests/data/langchain_logo.png");
const tweetText = "test 123 hello world! (with image)";

Expand All @@ -84,36 +159,4 @@ describe("TwitterClient wrapper", () => {

expect(result).toBeDefined();
});

it("Can upload this image", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageBuffer = await fs.readFile(
"src/tests/data/langchain_logo_2.png",
);
const imageBase64 = imageBuffer.toString("base64");

const imageMimeType = extractMimeTypeFromBase64(imageBase64);
if (!imageMimeType) {
throw new Error("Could not determine image mime type");
}
const result = await client.uploadMedia(imageBuffer, imageMimeType);
console.log("result", result);
});

it("Can upload image from URL", async () => {
const client = await TwitterClient.fromArcade(userId, twitterTokens);
const imageUrl =
"https://miro.medium.com/v2/resize:fit:1200/1*-PlFCd_VBcALKReO3ZaOEg.png";

const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}

const imageBuffer = Buffer.from(await response.arrayBuffer());
const contentType = response.headers.get("content-type") || "image/jpeg";
console.log("contentType", contentType);
const result = await client.uploadMedia(imageBuffer, contentType);
console.log("result", result);
});
});

0 comments on commit 0e6dc4d

Please sign in to comment.