Skip to content

Commit

Permalink
chore: apply auto lint fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafkorem committed Jan 20, 2025
1 parent 815e896 commit 3bf489f
Show file tree
Hide file tree
Showing 28 changed files with 2,411 additions and 1,922 deletions.
178 changes: 89 additions & 89 deletions src/Copilot.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Copilot } from '@/Copilot';
import { CopilotStepPerformer } from '@/actions/CopilotStepPerformer';
import { CopilotError } from '@/errors/CopilotError';
import { Config, ScreenCapturerResult } from '@/types';
import { mockCache, mockedCacheFile } from './test-utils/cache';
import { ScreenCapturer } from '@/utils/ScreenCapturer';
import { Copilot } from "@/Copilot";
import { CopilotStepPerformer } from "@/actions/CopilotStepPerformer";
import { CopilotError } from "@/errors/CopilotError";
import { Config, ScreenCapturerResult } from "@/types";
import { mockCache, mockedCacheFile } from "./test-utils/cache";
import { ScreenCapturer } from "@/utils/ScreenCapturer";
import {
bazCategory,
barCategory2,
barCategory1,
dummyContext,
} from './test-utils/APICatalogTestUtils';
} from "./test-utils/APICatalogTestUtils";

jest.mock('@/actions/CopilotStepPerformer');
jest.mock('@/utils/ScreenCapturer');
jest.mock('fs');
jest.mock("@/actions/CopilotStepPerformer");
jest.mock("@/utils/ScreenCapturer");
jest.mock("fs");

const INTENT = 'tap button';
const SNAPSHOT_DATA = 'snapshot_data';
const VIEW_HIERARCHY = 'hash';
const INTENT = "tap button";
const SNAPSHOT_DATA = "snapshot_data";
const VIEW_HIERARCHY = "hash";

describe('Copilot', () => {
describe("Copilot", () => {
let mockConfig: Config;
let screenCapture: ScreenCapturerResult;

Expand All @@ -45,23 +45,25 @@ describe('Copilot', () => {
isSnapshotImageAttached: true,
};

jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, "error").mockImplementation(() => {});

ScreenCapturer.prototype.capture = jest.fn().mockResolvedValue(screenCapture);
ScreenCapturer.prototype.capture = jest
.fn()
.mockResolvedValue(screenCapture);
(CopilotStepPerformer.prototype.perform as jest.Mock).mockResolvedValue({
code: 'code',
code: "code",
result: true,
});
});

afterEach(() => {
jest.resetAllMocks();
(console.error as jest.Mock).mockRestore();
(Copilot as any)['instance'] = undefined;
(Copilot as any)["instance"] = undefined;
});

describe('getInstance', () => {
it('should return the same instance after initialization', () => {
describe("getInstance", () => {
it("should return the same instance after initialization", () => {
Copilot.init(mockConfig);

const instance1 = Copilot.getInstance();
Expand All @@ -70,49 +72,49 @@ describe('Copilot', () => {
expect(instance1).toBe(instance2);
});

it('should throw CopilotError if getInstance is called before init', () => {
it("should throw CopilotError if getInstance is called before init", () => {
expect(() => Copilot.getInstance()).toThrow(CopilotError);
expect(() => Copilot.getInstance()).toThrow(
'Copilot has not been initialized. Please call the `init()` method before using it.'
"Copilot has not been initialized. Please call the `init()` method before using it.",
);
});
});

describe('init', () => {
it('should create a new instance of Copilot', () => {
describe("init", () => {
it("should create a new instance of Copilot", () => {
Copilot.init(mockConfig);
expect(Copilot.getInstance()).toBeInstanceOf(Copilot);
});

it('should throw an error when trying to initialize Copilot multiple times', () => {
it("should throw an error when trying to initialize Copilot multiple times", () => {
Copilot.init(mockConfig);

expect(() => Copilot.init(mockConfig)).toThrow(
'Copilot has already been initialized. Please call the `init()` method only once.'
"Copilot has already been initialized. Please call the `init()` method only once.",
);
});

it('should throw an error if config is invalid', () => {
it("should throw an error if config is invalid", () => {
const invalidConfig = {} as Config;

expect(() => Copilot.init(invalidConfig)).toThrow();
});
});

describe('isInitialized', () => {
it('should return false before initialization', () => {
describe("isInitialized", () => {
it("should return false before initialization", () => {
expect(Copilot.isInitialized()).toBe(false);
});

it('should return true after initialization', () => {
it("should return true after initialization", () => {
Copilot.init(mockConfig);

expect(Copilot.isInitialized()).toBe(true);
});
});

describe('performStep', () => {
it('should call CopilotStepPerformer.perform with the given intent', async () => {
describe("performStep", () => {
it("should call CopilotStepPerformer.perform with the given intent", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();
Expand All @@ -125,7 +127,7 @@ describe('Copilot', () => {
);
});

it('should return the result from CopilotStepPerformer.perform', async () => {
it("should return the result from CopilotStepPerformer.perform", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();
Expand All @@ -135,12 +137,12 @@ describe('Copilot', () => {
expect(result).toBe(true);
});

it('should accumulate previous steps', async () => {
it("should accumulate previous steps", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();
const intent1 = 'tap button 1';
const intent2 = 'tap button 2';
const intent1 = "tap button 1";
const intent2 = "tap button 2";

await instance.performStep(intent1);
await instance.performStep(intent2);
Expand All @@ -150,22 +152,22 @@ describe('Copilot', () => {
[
{
step: intent1,
code: 'code',
code: "code",
result: true,
},
],
screenCapture
screenCapture,
);
});
});

describe('start', () => {
it('should clear previous steps', async () => {
describe("start", () => {
it("should clear previous steps", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();
const intent1 = 'tap button 1';
const intent2 = 'tap button 2';
const intent1 = "tap button 1";
const intent2 = "tap button 2";

await instance.performStep(intent1);
instance.end(true);
Expand All @@ -175,48 +177,48 @@ describe('Copilot', () => {
expect(CopilotStepPerformer.prototype.perform).toHaveBeenLastCalledWith(
intent2,
[],
screenCapture
screenCapture,
);
});
});

describe('start and end behavior', () => {
it('should not performStep before start', async () => {
describe("start and end behavior", () => {
it("should not performStep before start", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();

await expect(instance.performStep(INTENT)).rejects.toThrowError(
'Copilot is not running. Please call the `start()` method before performing any steps.'
"Copilot is not running. Please call the `start()` method before performing any steps.",
);
});

it('should not start without ending the previous flow (start->start)', async () => {
it("should not start without ending the previous flow (start->start)", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();

await instance.performStep(INTENT);

expect(() => instance.start()).toThrowError(
'Copilot was already started. Please call the `end()` method before starting a new test flow.'
"Copilot was already started. Please call the `end()` method before starting a new test flow.",
);
});

it('should not end without starting a new flow (end->end)', () => {
it("should not end without starting a new flow (end->end)", () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.start();

instance.end(true);

expect(() => instance.end(true)).toThrowError(
'Copilot is not running. Please call the `start()` method before ending the test flow.'
"Copilot is not running. Please call the `start()` method before ending the test flow.",
);
});
});

describe('end', () => {
it('end with isCacheDisabled=true should not save to cache', async () => {
describe("end", () => {
it("end with isCacheDisabled=true should not save to cache", async () => {
mockCache();

Copilot.init(mockConfig);
Expand All @@ -230,14 +232,14 @@ describe('Copilot', () => {
});
});

describe('extend API catalog', () => {
it('should extend the API catalog with a new category', () => {
describe("extend API catalog", () => {
it("should extend the API catalog with a new category", () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();

const spyCopilotStepPerformer = jest.spyOn(
instance['copilotStepPerformer'],
'extendJSContext'
instance["copilotStepPerformer"],
"extendJSContext",
);

instance.extendAPICatalog([barCategory1]);
Expand All @@ -248,13 +250,13 @@ describe('Copilot', () => {
expect(spyCopilotStepPerformer).not.toHaveBeenCalled();
});

it('should extend the API catalog with a new category and context', () => {
it("should extend the API catalog with a new category and context", () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();

const spyCopilotStepPerformer = jest.spyOn(
instance['copilotStepPerformer'],
'extendJSContext'
instance["copilotStepPerformer"],
"extendJSContext",
);

instance.extendAPICatalog([barCategory1], dummyContext);
Expand All @@ -265,28 +267,28 @@ describe('Copilot', () => {
expect(spyCopilotStepPerformer).toHaveBeenCalledWith(dummyContext);
});

it('should extend the API catalog with an existing category', () => {
it("should extend the API catalog with an existing category", () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();

const spyCopilotStepPerformer = jest.spyOn(
instance['copilotStepPerformer'],
'extendJSContext'
instance["copilotStepPerformer"],
"extendJSContext",
);

instance.extendAPICatalog([barCategory1]);
instance.extendAPICatalog([barCategory2], dummyContext);

expect(
mockConfig.frameworkDriver.apiCatalog.categories.length
).toEqual(1);
expect(
mockConfig.frameworkDriver.apiCatalog.categories[0].items
).toEqual([...barCategory1.items, ...barCategory2.items]);
expect(mockConfig.frameworkDriver.apiCatalog.categories.length).toEqual(
1,
);
expect(mockConfig.frameworkDriver.apiCatalog.categories[0].items).toEqual(
[...barCategory1.items, ...barCategory2.items],
);
expect(spyCopilotStepPerformer).toHaveBeenCalledWith(dummyContext);
});

it('should extend the API catalog with a new category', () => {
it("should extend the API catalog with a new category", () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();

Expand All @@ -300,38 +302,36 @@ describe('Copilot', () => {
});
});

describe('pilot', () => {
it('should perform an entire test flow using the provided goal', async () => {
describe("pilot", () => {
it("should perform an entire test flow using the provided goal", async () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
const goal = 'Test the login flow';
const goal = "Test the login flow";
const pilotOutputStep1 = {
thoughts: 'Step 1 thoughts',
action: 'Tap on GREAT button',
thoughts: "Step 1 thoughts",
action: "Tap on GREAT button",
};

const pilotOutputSuccess = {
thoughts: 'Completed successfully',
action: 'success',
thoughts: "Completed successfully",
action: "success",
};
jest
.spyOn(instance['pilotPerformer'], 'perform')
.mockResolvedValue({
steps: [
{ plan: pilotOutputStep1, code: 'code executed' },
{ plan: pilotOutputSuccess },
],
});
jest.spyOn(instance["pilotPerformer"], "perform").mockResolvedValue({
steps: [
{ plan: pilotOutputStep1, code: "code executed" },
{ plan: pilotOutputSuccess },
],
});

const result = await instance.pilot(goal);

expect(instance['pilotPerformer'].perform).toHaveBeenCalledWith(goal);
expect(instance["pilotPerformer"].perform).toHaveBeenCalledWith(goal);
expect(result).toEqual({
steps: [
{ plan: pilotOutputStep1, code: 'code executed' },
{ plan: pilotOutputSuccess },
],
{ plan: pilotOutputStep1, code: "code executed" },
{ plan: pilotOutputSuccess },
],
});
});
});
});
});
Loading

0 comments on commit 3bf489f

Please sign in to comment.