generated from NEARBuilders/project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from elliotBraem/feat/edge-cases
handles edge cases and better styles
- Loading branch information
Showing
21 changed files
with
10,137 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {import('jest').Config} */ | ||
export default { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
extensionsToTreatAsEsm: [".ts"], | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
transform: { | ||
"^.+\\.tsx?$": [ | ||
"ts-jest", | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
setupFiles: ["<rootDir>/src/__tests__/setup.ts"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Tweet } from "agent-twitter-client"; | ||
|
||
export class MockScraper { | ||
private mockTweets: Tweet[] = []; | ||
private _isLoggedIn = false; | ||
private cookies: string[] = []; | ||
|
||
// Method to add mock tweets for testing | ||
public addMockTweet(tweet: Tweet) { | ||
this.mockTweets.push(tweet); | ||
} | ||
|
||
// Method to clear mock tweets | ||
public clearMockTweets() { | ||
this.mockTweets = []; | ||
} | ||
|
||
async login( | ||
username: string, | ||
password: string, | ||
email: string, | ||
): Promise<void> { | ||
this._isLoggedIn = true; | ||
} | ||
|
||
async logout(): Promise<void> { | ||
this._isLoggedIn = false; | ||
} | ||
|
||
async isLoggedIn(): Promise<boolean> { | ||
return this._isLoggedIn; | ||
} | ||
|
||
async setCookies(cookies: string[]): Promise<void> { | ||
this.cookies = cookies; | ||
} | ||
|
||
async getCookies(): Promise<string[]> { | ||
return this.cookies; | ||
} | ||
|
||
async getUserIdByScreenName(screenName: string): Promise<string> { | ||
return `mock-user-id-${screenName}`; | ||
} | ||
|
||
async fetchSearchTweets( | ||
query: string, | ||
count: number, | ||
mode: any, | ||
cursor?: string, | ||
): Promise<{ tweets: Tweet[] }> { | ||
// If cursor is provided, simulate pagination by returning tweets after that ID | ||
if (cursor) { | ||
const cursorIndex = this.mockTweets.findIndex((t) => t.id === cursor); | ||
if (cursorIndex !== -1) { | ||
return { | ||
tweets: this.mockTweets.slice( | ||
cursorIndex + 1, | ||
cursorIndex + 1 + count, | ||
), | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
tweets: this.mockTweets.slice(0, count), | ||
}; | ||
} | ||
|
||
async getTweet(tweetId: string): Promise<Tweet | null> { | ||
return this.mockTweets.find((t) => t.id === tweetId) || null; | ||
} | ||
|
||
async sendTweet(message: string, replyToId?: string): Promise<Response> { | ||
const mockResponse = { | ||
json: async () => ({ | ||
data: { | ||
create_tweet: { | ||
tweet_results: { | ||
result: { | ||
rest_id: `mock-reply-${Date.now()}`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}; | ||
return mockResponse as Response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { beforeAll, mock } from "bun:test"; | ||
|
||
// Mock environment variables | ||
process.env.PORT = "3000"; | ||
process.env.NODE_ENV = "test"; | ||
|
||
// Mock Bun server | ||
const mockServer = { | ||
upgrade: mock(() => true), | ||
fetch: mock(() => new Response()), | ||
publish: mock(() => {}), | ||
unsubscribe: mock(() => {}), | ||
reload: mock(() => {}), | ||
pendingWebSockets: new Set(), | ||
stop: mock(() => {}), | ||
}; | ||
|
||
const mockFile = { | ||
exists: mock(() => Promise.resolve(true)), | ||
text: mock(() => Promise.resolve("")), | ||
stream: mock(() => new ReadableStream()), | ||
size: 0, | ||
type: "text/plain", | ||
}; | ||
|
||
// Create a proxy to handle server initialization | ||
const serverProxy = new Proxy(mockServer, { | ||
get: (target, prop) => { | ||
if (prop === "upgrade") { | ||
return (req: Request) => true; | ||
} | ||
return target[prop as keyof typeof target]; | ||
}, | ||
}); | ||
|
||
globalThis.Bun = { | ||
serve: mock(() => serverProxy), | ||
file: mock((path: string) => mockFile), | ||
} as any; | ||
|
||
// Mock logger | ||
const mockLogger = { | ||
info: mock(() => {}), | ||
error: mock(() => {}), | ||
debug: mock(() => {}), | ||
}; | ||
|
||
const mockSpinner = { | ||
startSpinner: mock(() => {}), | ||
succeedSpinner: mock(() => {}), | ||
failSpinner: mock(() => {}), | ||
cleanup: mock(() => {}), | ||
}; | ||
|
||
// Mock modules | ||
import { logger } from "../utils/logger"; | ||
Object.assign(logger, mockLogger); | ||
Object.assign(logger, mockSpinner); | ||
|
||
// Mock config | ||
import config from "../config/config"; | ||
Object.assign(config, { | ||
twitter: { | ||
username: "test_user", | ||
password: "test_pass", | ||
email: "[email protected]", | ||
}, | ||
}); | ||
|
||
// Mock ADMIN_ACCOUNTS | ||
import { ADMIN_ACCOUNTS } from "../config/admins"; | ||
(ADMIN_ACCOUNTS as any) = ["admin"]; | ||
|
||
// Mock server functions | ||
import { main, broadcastUpdate } from "../index"; | ||
Object.assign( | ||
main, | ||
mock(() => Promise.resolve()), | ||
); | ||
Object.assign( | ||
broadcastUpdate, | ||
mock(() => {}), | ||
); |
Oops, something went wrong.