Skip to content

Commit

Permalink
test(functions): add firebase-json tests, fix other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitMY committed Jan 10, 2024
1 parent 3641401 commit 34399d3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion functions/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transformIgnorePatterns: ['^.+\\.js$'],
testMatch: ['**.spec.ts'],
};
1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"firebase-functions-test": "3.1.0",
"firebase-tools": "13.0.3",
"jest": "29.7.0",
"minimatch": "^9.0.3",
"mock-express-request": "0.2.2",
"mock-express-response": "0.3.0",
"ts-jest": "29.1.1",
Expand Down
42 changes: 42 additions & 0 deletions functions/src/firebase-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {readFileSync} from 'fs';
import {minimatch} from 'minimatch';

describe('firebase.json', () => {
const filePath = '../firebase.json';
let fileContent: string;

beforeEach(() => {
fileContent = readFileSync(filePath, 'utf8');
});

// Prevent mistakes like - https://github.com/sign/translate/commit/ad0ef31a0a2b3cd989dff6a9dcec742157d9619d
it('firebase.json should be a valid JSON file', async () => {
expect(JSON.parse(fileContent)).toBeTruthy();
});

it('firebase.json should have rewrites', async () => {
const obj = JSON.parse(fileContent);
expect(obj.hosting.rewrites).toBeTruthy();
});

function resolveRewrite(path: string) {
const rewrites = JSON.parse(fileContent).hosting.rewrites;

for (const rewrite of rewrites) {
if (minimatch(path, rewrite.source)) {
return rewrite.function ?? rewrite.destination;
}
}
return path;
}

it('firebase.json should redirect "/random-path" to the index.html', async () => {
expect(resolveRewrite('/random-path')).toEqual('/index.html');
});

it('firebase.json should not redirect assets to the index.html', async () => {
// Fixed in https://github.com/sign/translate/commit/730546444bf1a35c2097230b1562783ae0dfda2a
// If even a single i18n asset is redirected instead of 404 error, Transloco reverts everything to English
expect(resolveRewrite('/assets/random-path')).toEqual('/assets/random-path');
});
});
8 changes: 8 additions & 0 deletions functions/src/firebase.extend-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export function setupFirebaseTestEnvironment(clearStorage = true): FirebaseTestE
file: (p: string) => {
const ref = storage.ref(p);
return {
exists: async () => {
return [
await ref
.getMetadata()
.then(() => true)
.catch(() => false),
];
},
save: (data: any) => {
if (typeof data === 'string') {
return ref.putString(data);
Expand Down
17 changes: 8 additions & 9 deletions functions/src/text-normalization/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TextNormalizationEndpoint} from './controller';
import {setupFirebaseTestEnvironment} from '../firebase.extend-spec';
import {StringParam} from 'firebase-functions/lib/params/types';
import {defineString} from 'firebase-functions/params';

const MockExpressRequest = require('mock-express-request');
const MockExpressResponse = require('mock-express-response');
Expand All @@ -10,9 +10,9 @@ describe('TextNormalizationEndpoint', () => {

let controller: TextNormalizationEndpoint;
beforeEach(() => {
const key = new StringParam('mock-key');
const key = defineString('mock-key');
controller = new TextNormalizationEndpoint(testEnvironment.database, key);
spyOn(controller, 'normalize').and.returnValue(Promise.resolve('mock normalized text'));
jest.spyOn(controller, 'normalize').mockResolvedValue('mock normalized text');
});

it('should error missing "lang"', async () => {
Expand All @@ -23,7 +23,7 @@ describe('TextNormalizationEndpoint', () => {
});
const mockRes = new MockExpressResponse({request: mockReq});

await expect(controller.request(mockReq, mockRes)).rejects.toThrow(new Error('Missing "from" query parameter'));
await expect(controller.request(mockReq, mockRes)).rejects.toThrow(new Error('Missing "lang" query parameter'));
});

it('should error missing "text"', async () => {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('TextNormalizationEndpoint', () => {

expect(response).toEqual({
lang: 'en',
text: 'mock normalized text,',
text: 'mock normalized text',
});
});

Expand All @@ -77,17 +77,16 @@ describe('TextNormalizationEndpoint', () => {
});

it('should respond with cached response if exists', async () => {
const helloMd5 = '5d41402abc4b2a76b9719d911017c592';
const ref = testEnvironment.database.ref('/normalizations/en/' + helloMd5);
const ref = controller.getDBRef('en', 'hello');
await ref.set({
input: 'hello',
output: 'fake translation',
output: 'fake response',
counter: 1,
timestamp: Date.now(),
});

const response = await normalizeExample();
expect(response.text).toEqual('fake translation');
expect(response.text).toEqual('fake response');

const snapshot = await ref.once('value');
expect(snapshot.exists()).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion functions/src/text-to-text/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('TextToTextTranslationEndpoint', () => {
direction: 'spoken-to-signed',
from: 'en',
to: 'ru',
text: 'Здравствуйте,',
text: 'Здравствуйте, здравствуйте',
});
});

Expand Down
2 changes: 1 addition & 1 deletion functions/src/text-to-text/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class TextToTextTranslationEndpoint {
translation = cache;
} else {
const model = await this.getModel(direction, from, to);
translation = await model.translate(text, from, to, 'SW');
translation = await model.translate(text, from, to);
await cache.set({
text,
translation,
Expand Down
18 changes: 12 additions & 6 deletions functions/src/text-to-text/model/model.extend-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ const STORAGE_PREFIX = 'models/browsermt/spoken-to-signed/en-ru/';

export function setupModelFiles(testEnvironment: FirebaseTestEnvironmentContext) {
beforeAll(async () => {
// Upload a model to storage
for (const file of MODEL_FILES) {
await testEnvironment.bucket.upload(__dirname + `/test-assets/enru/${file}`, {
destination: STORAGE_PREFIX + file,
});
}
await testEnvironment.env.withSecurityRulesDisabled(async () => {
// Upload a model to storage
for (const file of MODEL_FILES) {
const localFile = __dirname + `/test-assets/enru/${file}`;
const destination = STORAGE_PREFIX + file;
// check if file exists, speeds up tests
const [exists] = await testEnvironment.bucket.file(destination).exists();
if (!exists) {
await testEnvironment.bucket.upload(localFile, {destination});
}
}
});
});

return MODEL_FILES.map(f => STORAGE_PREFIX + f);
Expand Down
7 changes: 2 additions & 5 deletions functions/src/text-to-text/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ export class TextToTextTranslationModel {
return modelRegistry;
}

async translate(text: string, from?: string, to?: string, format?: string) {
async translate(text: string, from?: string, to?: string) {
const tags: string[] = [];
// Format is: $FORMAT$ $COUNTRY$ $ISO$? $LANGUAGE$ | text
if (format) {
tags.push(`$${format}$`);
}
// Format is: $LANGUAGE$ $ISO$ text
if (this.from === 'spoken') {
tags.push(`$${to}$`);
tags.push(`$${from}$`);
Expand Down

0 comments on commit 34399d3

Please sign in to comment.