Skip to content

Commit

Permalink
✅ Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cermakjiri committed Nov 15, 2024
1 parent 5a977ff commit 791d7ab
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 76 deletions.
19 changes: 15 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": ["oclif", "oclif-typescript", "prettier"],
"extends": [
"oclif",
"oclif-typescript",
"prettier"
],
"rules": {
"unicorn/prefer-node-protocol": "off",
"node/no-extraneous-import": "off",
Expand All @@ -16,7 +20,14 @@
],
"no-warning-comments": [
"warn",
{ "terms": ["fixme", "xxx"], "location": "start" }
]
{
"terms": [
"fixme",
"xxx"
],
"location": "start"
}
],
"@typescript-eslint/explicit-module-boundary-types": "off"
}
}
}
16 changes: 0 additions & 16 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import * as dedent from "dedent";

export class FatalError extends Error {}

export class MissingAuthError extends FatalError {
public name: string;

constructor() {
super(
dedent`
Cannot authenticate to fetch Spreadsheet data.
Provide either Service account credentials or API key 🔑 See detail info at https://ackeecz.github.io/lokse/en/authentication/
`
);
this.name = "MissingAuthError";
}
}

export class KeyColumnNotFound extends Error {
public key: string;

Expand Down
37 changes: 13 additions & 24 deletions packages/core/src/reader/__tests__/spreadsheet-reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import { GoogleSpreadsheet } from "google-spreadsheet";
import SpreadsheetReader from "../spreadsheet-reader";
import WorksheetReader from "../worksheet-reader";
import {
LangColumnNotFound,
KeyColumnNotFound,
MissingAuthError,
} from "../../errors";
import { LangColumnNotFound, KeyColumnNotFound } from "../../errors";
import Line from "../../line";
import Worksheet from "../worksheet";
import { PluginsRunner } from "../../plugins";
Expand Down Expand Up @@ -46,9 +42,9 @@ describe("SpreadsheetReader", () => {
});

it("uses service account if available", async () => {
expect.assertions(2);
const private_key = "this-is-dummy-private-key";
const client_email = "this-is@dummy-email";

process.env.LOKSE_SERVICE_ACCOUNT_EMAIL = client_email;
process.env.LOKSE_PRIVATE_KEY = private_key;

Expand All @@ -57,19 +53,16 @@ describe("SpreadsheetReader", () => {
new WorksheetReader("*"),
noPlugins
);
await reader.authenticate();

const useServiceAccountAuthMock =
GoogleSpreadsheetMock.mock.instances[0].useServiceAccountAuth;
expect(useServiceAccountAuthMock).toHaveBeenCalledTimes(1);
expect(useServiceAccountAuthMock).toHaveBeenLastCalledWith({
client_email,
private_key,
});

const client = await reader.authenticate();

// @ts-expect-error private property
expect(client._clientId).toEqual(client_email);
// @ts-expect-error private property
expect(client._clientSecret).toEqual(private_key);
});

it("uses api key if available", async () => {
expect.assertions(2);
const dummyApiKey = "dummy-api-key";
process.env.LOKSE_API_KEY = dummyApiKey;

Expand All @@ -78,17 +71,13 @@ describe("SpreadsheetReader", () => {
new WorksheetReader("*"),
noPlugins
);
await reader.authenticate();

const useApiKeyMock = GoogleSpreadsheetMock.mock.instances[0].useApiKey;
expect(useApiKeyMock).toHaveBeenCalledTimes(1);
expect(useApiKeyMock).toHaveBeenLastCalledWith(dummyApiKey);
const client = await reader.authenticate();

expect(client.apiKey).toEqual(dummyApiKey);
});

it("throw if service account nor api key found", async () => {
expect.assertions(1);
const expectedError = new MissingAuthError();

const reader = new SpreadsheetReader(
"test-sheet-id",
new WorksheetReader("*"),
Expand All @@ -97,7 +86,7 @@ describe("SpreadsheetReader", () => {

await expect(reader.authenticate()).rejects.toHaveProperty(
"message",
expectedError.message
"Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information."
);
});
});
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/reader/__tests__/worksheet-reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ describe("WorksheetReader - filter normalization", () => {
});

it("throws when unsupported filter type provided", () => {
expect(() => new WorksheetReader(true as any)).toThrowError(
InvalidFilterError
);
expect(() => new WorksheetReader(3 as any)).toThrowError(
InvalidFilterError
);
expect(() => new WorksheetReader(new Date() as any)).toThrowError(
expect(() => new WorksheetReader(true as any)).toThrow(InvalidFilterError);
expect(() => new WorksheetReader(3 as any)).toThrow(InvalidFilterError);
expect(() => new WorksheetReader(new Date() as any)).toThrow(
InvalidFilterError
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/reader/__tests__/worksheet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { KeyColumnNotFound, LangColumnNotFound } from "../../errors";
import { PluginsRunner } from "../../plugins";
import Worksheet from "../worksheet";

const createRow = (rowIndex: number, values: { [key: string]: any }) =>
export const createRow = (rowIndex: number, values: { [key: string]: any }) =>
({
rowIndex,
...values,
Expand Down
21 changes: 9 additions & 12 deletions packages/core/src/reader/spreadsheet-reader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GoogleSpreadsheet } from "google-spreadsheet";
import { AnyAuthClient, GoogleAuth, JWT } from "google-auth-library";
import { GoogleAuth, JWT } from "google-auth-library";

import Line from "../line";
import { FatalError, getErrorMessage } from "../errors";
Expand Down Expand Up @@ -35,7 +35,7 @@ export class SpreadsheetReader {
this.worksheets = null;
}

async authenticate(): Promise<AnyAuthClient> {
createAuthClient() {
const { LOKSE_API_KEY, LOKSE_SERVICE_ACCOUNT_EMAIL, LOKSE_PRIVATE_KEY } =
process.env;

Expand All @@ -47,33 +47,30 @@ export class SpreadsheetReader {
clientSecret: LOKSE_PRIVATE_KEY,
});

const auth = new GoogleAuth();

auth.setGapicJWTValues(jwt);

return auth.getClient();
return new GoogleAuth({ authClient: jwt });
}

if (LOKSE_API_KEY) {
this.logger.log("🔑 Authenticating with API key...");
const auth = new GoogleAuth({
apiKey: LOKSE_API_KEY,
});

return auth.getClient();
return new GoogleAuth({ apiKey: LOKSE_API_KEY });
}

this.logger.log(
"🔑 Authenticating with Application Default Credentials..."
);

const auth = new GoogleAuth({
return new GoogleAuth({
scopes: [
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
],
});
}

async authenticate() {
const auth = this.createAuthClient();

return auth.getClient();
}
Expand Down
29 changes: 19 additions & 10 deletions packages/plugin-fallback/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Line } from "@lokse/core";
import fallbackPluginFactory from "..";
import type { PluginOptions } from "..";

export const createRow = (rowIndex: number, values: { [key: string]: any }) =>
({
rowIndex,
...values,
get: (key: string) => values[key],
save: () => null,
delete: () => null,
} as unknown as GoogleSpreadsheetRow);

describe("Fallback plugin", () => {
const logger = { warn: jest.fn(), log: jest.fn() };
const factoryMeta = { languages: ["cs", "mng"] };
Expand Down Expand Up @@ -36,11 +45,11 @@ describe("Fallback plugin", () => {

it("should keep translation as it is when filled", async () => {
const line = new Line("test.key", "Ukama bugama");
const row = {
const row = createRow(0, {
key: "test.key",
mng: "Ukama bugama",
cs: "Nejakej nesmysl",
} as unknown as GoogleSpreadsheetRow;
});
const meta = { row, key: line.key, language: "mng" };

await expect(plugin.readTranslation(line, meta)).resolves.toHaveProperty(
Expand All @@ -52,11 +61,11 @@ describe("Fallback plugin", () => {

it("should log missing fallback translation in default", async () => {
const line = new Line("test.key", "");
const row = {
const row = createRow(0, {
key: "test.key",
mng: "",
cs: "",
} as unknown as GoogleSpreadsheetRow;
});
const meta = { row, key: line.key, language: "mng" };

await expect(plugin.readTranslation(line, meta)).resolves.toHaveProperty(
Expand All @@ -77,11 +86,11 @@ describe("Fallback plugin", () => {
factoryMeta
);
const line = new Line("test.key", "");
const row = {
const row = createRow(0, {
key: "test.key",
mng: "",
cs: "",
} as unknown as GoogleSpreadsheetRow;
});
const meta = { row, key: line.key, language: "mng" };

await expect(plugin2.readTranslation(line, meta)).resolves.toHaveProperty(
Expand All @@ -93,11 +102,11 @@ describe("Fallback plugin", () => {

it("should fallback to default language translation when translation is empty", async () => {
const line = new Line("test.key", "");
const row = {
const row = createRow(0, {
key: "test.key",
mng: "",
cs: "Nejakej nesmysl",
} as unknown as GoogleSpreadsheetRow;
});
const meta = { row, key: line.key, language: "mng" };

await expect(plugin.readTranslation(line, meta)).resolves.toHaveProperty(
Expand All @@ -109,11 +118,11 @@ describe("Fallback plugin", () => {

it("should fallback to default language case insensivly", async () => {
const line = new Line("test.key", "");
const row = {
const row = createRow(0, {
key: "test.key",
MNG: "",
CS: "Nejakej nesmysl",
} as unknown as GoogleSpreadsheetRow;
});
const meta = { row, key: line.key, language: "mng" };

await expect(plugin.readTranslation(line, meta)).resolves.toHaveProperty(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-fallback/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function (
Object.keys(meta.row).find((key) => isDefaultLang(key)) ??
NOT_FOUND_KEY;

const fallbackLanguageValue = meta.row[defaultLanguageKey] ?? "";
const fallbackLanguageValue = meta.row.get(defaultLanguageKey) ?? "";

if (logMissingFallback && !fallbackLanguageValue) {
logger.warn(`Fallback translation of key "${meta.key}" not found`);
Expand Down
2 changes: 1 addition & 1 deletion vscode-extension/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"dist",
"**/*.d.ts"
]
}
}

0 comments on commit 791d7ab

Please sign in to comment.