Skip to content

Commit

Permalink
fix: resolve TypeScript and linting issues in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mherod committed Jan 3, 2025
1 parent 1cfb71a commit 80e98a2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 85 deletions.
4 changes: 2 additions & 2 deletions scripts/test-safari-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function main(): void {
consola.info(`\nCookie ${index + 1}:`);
consola.info(` Name: ${cookie.name}`);
consola.info(` Domain: ${cookie.domain}`);
const valueStr = cookie.value;
const valueStr = cookie.value as string;
consola.info(
` Value: ${valueStr.slice(0, 50)}${valueStr.length > 50 ? "..." : ""}`,
` Value: ${valueStr.substring(0, 50)}${valueStr.length > 50 ? "..." : ""}`,
);
consola.info(` Expiry: ${new Date(cookie.expiry * 1000).toISOString()}`);
});
Expand Down
4 changes: 3 additions & 1 deletion src/cli/handlers/DefaultOutputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class DefaultOutputHandler implements OutputHandler {
* ```
*/
public handle(results: ExportedCookie[]): void {
const uniqueValues = new Set(results.map((result) => result.value));
const uniqueValues = new Set(
results.map((result) => result.value as string),
);
for (const value of uniqueValues) {
logger.log(value);
}
Expand Down
150 changes: 70 additions & 80 deletions src/core/browsers/__tests__/CompositeCookieQueryStrategy.test.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,112 @@
import type {
BrowserName,
CookieQueryStrategy,
ExportedCookie,
} from "../../../types/schemas";
import { CompositeCookieQueryStrategy } from "../CompositeCookieQueryStrategy";

class MockCookieQueryStrategy implements CookieQueryStrategy {
public constructor(
public readonly browserName: BrowserName,
private readonly mockCookies: ExportedCookie[] = [],
) {}
const createMockStrategy = (
browserType: "Chrome" | "Firefox" | "Safari" | "internal" | "unknown",
cookies: ExportedCookie[],
): CookieQueryStrategy => ({
browserName: browserType,
queryCookies: async (
_name: string,
_domain: string,
): Promise<ExportedCookie[]> => Promise.resolve(cookies),
});

public async queryCookies(): Promise<ExportedCookie[]> {
return Promise.resolve(this.mockCookies);
}
}
const createTestCookies = (browser: string): ExportedCookie[] => [
{
name: `${browser}_cookie1`,
value: `${browser}_value1`,
domain: "example.com",
expiry: 1234567890,
meta: {
browser,
decrypted: true,
file: `/path/to/${browser}/cookies`,
},
},
{
name: `${browser}_cookie2`,
value: `${browser}_value2`,
domain: "example.org",
expiry: 1234567890,
meta: {
browser,
decrypted: true,
file: `/path/to/${browser}/cookies`,
},
},
];

describe("CompositeCookieQueryStrategy", () => {
it("should combine results from multiple strategies", async () => {
const chromeCookies: ExportedCookie[] = [
{
name: "test-cookie",
value: "chrome-value",
domain: "example.com",
expiry: "Infinity",
meta: {
browser: "Chrome",
decrypted: true,
file: "/path/to/chrome/cookies",
},
},
];

const firefoxCookies: ExportedCookie[] = [
{
name: "test-cookie",
value: "firefox-value",
domain: "example.com",
expiry: new Date(Date.now() + 3600000),
meta: {
browser: "Firefox",
decrypted: false,
file: "/path/to/firefox/cookies",
},
},
];
const chromeCookies = createTestCookies("chrome");
const firefoxCookies = createTestCookies("firefox");
const safariCookies = createTestCookies("safari");

const chromeStrategy = new MockCookieQueryStrategy("Chrome", chromeCookies);
const firefoxStrategy = new MockCookieQueryStrategy(
"Firefox",
firefoxCookies,
);
const chromeStrategy = createMockStrategy("Chrome", chromeCookies);
const firefoxStrategy = createMockStrategy("Firefox", firefoxCookies);
const safariStrategy = createMockStrategy("Safari", safariCookies);

const composite = new CompositeCookieQueryStrategy([
chromeStrategy,
firefoxStrategy,
safariStrategy,
]);
const cookies = await composite.queryCookies("test-cookie", "example.com");

expect(cookies).toHaveLength(2);
expect(cookies).toEqual(
expect.arrayContaining([...chromeCookies, ...firefoxCookies]),
const results = await composite.queryCookies("test-cookie", "example.com");
expect(results).toHaveLength(6);
expect(results).toEqual(
expect.arrayContaining([
...chromeCookies,
...firefoxCookies,
...safariCookies,
]),
);
});

it("should handle empty results from strategies", async () => {
const chromeStrategy = new MockCookieQueryStrategy("Chrome", []);
const firefoxStrategy = new MockCookieQueryStrategy("Firefox", []);

const composite = new CompositeCookieQueryStrategy([
chromeStrategy,
firefoxStrategy,
createMockStrategy("Chrome", []),
createMockStrategy("Firefox", []),
]);
const cookies = await composite.queryCookies("test-cookie", "example.com");

expect(cookies).toEqual([]);
const results = await composite.queryCookies("test-cookie", "example.com");
expect(results).toHaveLength(0);
});

it("should handle errors from individual strategies", async () => {
const workingStrategy = new MockCookieQueryStrategy("Chrome", [
{
name: "test-cookie",
value: "chrome-value",
domain: "example.com",
expiry: "Infinity",
meta: {
browser: "Chrome",
decrypted: true,
file: "/path/to/chrome/cookies",
},
},
]);

const workingStrategy = createMockStrategy(
"Chrome",
createTestCookies("working"),
);
const failingStrategy: CookieQueryStrategy = {
browserName: "Firefox",
queryCookies: jest.fn().mockRejectedValue(new Error("Failed to query")),
queryCookies: (
_name: string,
_domain: string,
): Promise<ExportedCookie[]> => {
throw new Error("Strategy failed");
},
};

const composite = new CompositeCookieQueryStrategy([
workingStrategy,
failingStrategy,
]);
const cookies = await composite.queryCookies("test-cookie", "example.com");
const results = await composite.queryCookies("test-cookie", "example.com");

// Should still return results from working strategy
expect(cookies).toHaveLength(1);
expect(cookies[0]).toBeDefined();
expect(cookies[0]).toMatchObject({
meta: {
browser: "Chrome",
},
});
expect(results).toHaveLength(2);
expect(results).toEqual(
expect.arrayContaining(createTestCookies("working")),
);
});

it("should handle empty strategy list", async () => {
const composite = new CompositeCookieQueryStrategy([]);
const cookies = await composite.queryCookies("test-cookie", "example.com");

expect(cookies).toEqual([]);
const results = await composite.queryCookies("test-cookie", "example.com");
expect(results).toHaveLength(0);
});
});
4 changes: 2 additions & 2 deletions src/core/cookies/renderCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export function renderCookies(
}

if (format === "merged") {
return cookies.map((c) => c.value).join(separator);
return cookies.map((c) => c.value as string).join(separator);
}

const groupedByFile = groupBy(cookies, (c) => c.meta?.file ?? "unknown");
return Object.entries(groupedByFile).map(([file, fileCookies]) => {
const values = fileCookies.map((c) => c.value).join(separator);
const values = fileCookies.map((c) => c.value as string).join(separator);
return showFilePaths ? `${file}: ${values}` : values;
});
}

0 comments on commit 80e98a2

Please sign in to comment.