diff --git a/etc/firebase-admin.api.md b/etc/firebase-admin.api.md index ec1830df25..1340ad4fca 100644 --- a/etc/firebase-admin.api.md +++ b/etc/firebase-admin.api.md @@ -398,73 +398,46 @@ export namespace instanceId { } // @public -export function machineLearning(app?: app.App): machineLearning.MachineLearning; +export function machineLearning(app?: App): machineLearning.MachineLearning; // @public (undocumented) export namespace machineLearning { + // Warning: (ae-forgotten-export) The symbol "AutoMLTfliteModelOptions" needs to be exported by the entry point default-namespace.d.ts + // // (undocumented) - export interface AutoMLTfliteModelOptions extends ModelOptionsBase { - // (undocumented) - tfliteModel: { - automlModel: string; - }; - } + export type AutoMLTfliteModelOptions = AutoMLTfliteModelOptions; + // Warning: (ae-forgotten-export) The symbol "GcsTfliteModelOptions" needs to be exported by the entry point default-namespace.d.ts + // // (undocumented) - export interface GcsTfliteModelOptions extends ModelOptionsBase { - // (undocumented) - tfliteModel: { - gcsTfliteUri: string; - }; - } - export interface ListModelsOptions { - filter?: string; - pageSize?: number; - pageToken?: string; - } - export interface ListModelsResult { - readonly models: Model[]; - readonly pageToken?: string; - } - export interface MachineLearning { - app: app.App; - createModel(model: ModelOptions): Promise; - deleteModel(modelId: string): Promise; - getModel(modelId: string): Promise; - listModels(options?: ListModelsOptions): Promise; - publishModel(modelId: string): Promise; - unpublishModel(modelId: string): Promise; - updateModel(modelId: string, model: ModelOptions): Promise; - } - export interface Model { - readonly createTime: string; - readonly displayName: string; - readonly etag: string; - readonly locked: boolean; - readonly modelHash?: string; - readonly modelId: string; - readonly published: boolean; - readonly tags?: string[]; - readonly tfliteModel?: TFLiteModel; - toJSON(): { - [key: string]: any; - }; - readonly updateTime: string; - readonly validationError?: string; - waitForUnlocked(maxTimeMillis?: number): Promise; - } + export type GcsTfliteModelOptions = GcsTfliteModelOptions; + // Warning: (ae-forgotten-export) The symbol "ListModelsOptions" needs to be exported by the entry point default-namespace.d.ts + // // (undocumented) - export type ModelOptions = ModelOptionsBase | GcsTfliteModelOptions | AutoMLTfliteModelOptions; - export interface ModelOptionsBase { - // (undocumented) - displayName?: string; - // (undocumented) - tags?: string[]; - } - export interface TFLiteModel { - readonly automlModel?: string; - readonly gcsTfliteUri?: string; - readonly sizeBytes: number; - } + export type ListModelsOptions = ListModelsOptions; + // Warning: (ae-forgotten-export) The symbol "ListModelsResult" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type ListModelsResult = ListModelsResult; + // Warning: (ae-forgotten-export) The symbol "MachineLearning" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type MachineLearning = MachineLearning; + // Warning: (ae-forgotten-export) The symbol "Model" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type Model = Model; + // Warning: (ae-forgotten-export) The symbol "ModelOptions" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type ModelOptions = ModelOptions; + // Warning: (ae-forgotten-export) The symbol "ModelOptionsBase" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type ModelOptionsBase = ModelOptionsBase; + // Warning: (ae-forgotten-export) The symbol "TFLiteModel" needs to be exported by the entry point default-namespace.d.ts + // + // (undocumented) + export type TFLiteModel = TFLiteModel; } // @public diff --git a/etc/firebase-admin.machine-learning.api.md b/etc/firebase-admin.machine-learning.api.md new file mode 100644 index 0000000000..9f1204e7e2 --- /dev/null +++ b/etc/firebase-admin.machine-learning.api.md @@ -0,0 +1,131 @@ +## API Report File for "firebase-admin.machine-learning" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts + +import { Agent } from 'http'; + +// @public (undocumented) +export interface AutoMLTfliteModelOptions extends ModelOptionsBase { + // (undocumented) + tfliteModel: { + automlModel: string; + }; +} + +// @public (undocumented) +export interface GcsTfliteModelOptions extends ModelOptionsBase { + // (undocumented) + tfliteModel: { + gcsTfliteUri: string; + }; +} + +// Warning: (ae-forgotten-export) The symbol "App" needs to be exported by the entry point index.d.ts +// +// @public (undocumented) +export function getMachineLearning(app?: App): MachineLearning; + +// @public +export interface ListModelsOptions { + filter?: string; + pageSize?: number; + pageToken?: string; +} + +// @public +export interface ListModelsResult { + readonly models: Model[]; + readonly pageToken?: string; +} + +// @public +export class MachineLearning { + get app(): App; + createModel(model: ModelOptions): Promise; + deleteModel(modelId: string): Promise; + getModel(modelId: string): Promise; + listModels(options?: ListModelsOptions): Promise; + publishModel(modelId: string): Promise; + unpublishModel(modelId: string): Promise; + updateModel(modelId: string, model: ModelOptions): Promise; +} + +// @public +export function machineLearning(app?: App): machineLearning.MachineLearning; + +// @public (undocumented) +export namespace machineLearning { + // (undocumented) + export type AutoMLTfliteModelOptions = AutoMLTfliteModelOptions; + // (undocumented) + export type GcsTfliteModelOptions = GcsTfliteModelOptions; + // (undocumented) + export type ListModelsOptions = ListModelsOptions; + // (undocumented) + export type ListModelsResult = ListModelsResult; + // (undocumented) + export type MachineLearning = MachineLearning; + // (undocumented) + export type Model = Model; + // (undocumented) + export type ModelOptions = ModelOptions; + // (undocumented) + export type ModelOptionsBase = ModelOptionsBase; + // (undocumented) + export type TFLiteModel = TFLiteModel; +} + +// @public +export class Model { + // (undocumented) + get createTime(): string; + // (undocumented) + get displayName(): string; + // (undocumented) + get etag(): string; + get locked(): boolean; + // (undocumented) + get modelHash(): string | undefined; + // (undocumented) + get modelId(): string; + // (undocumented) + get published(): boolean; + // (undocumented) + get tags(): string[]; + // (undocumented) + get tfliteModel(): TFLiteModel | undefined; + // (undocumented) + toJSON(): { + [key: string]: any; + }; + // (undocumented) + get updateTime(): string; + // (undocumented) + get validationError(): string | undefined; + waitForUnlocked(maxTimeMillis?: number): Promise; +} + +// @public (undocumented) +export type ModelOptions = ModelOptionsBase | GcsTfliteModelOptions | AutoMLTfliteModelOptions; + +// @public +export interface ModelOptionsBase { + // (undocumented) + displayName?: string; + // (undocumented) + tags?: string[]; +} + +// @public +export interface TFLiteModel { + readonly automlModel?: string; + readonly gcsTfliteUri?: string; + readonly sizeBytes: number; +} + + +// (No @packageDocumentation comment for this package) + +``` diff --git a/generate-reports.js b/generate-reports.js index 8f31443727..4d92f64563 100644 --- a/generate-reports.js +++ b/generate-reports.js @@ -40,6 +40,7 @@ const entryPoints = { 'firebase-admin/firestore': './lib/firestore/index.d.ts', 'firebase-admin/instance-id': './lib/instance-id/index.d.ts', 'firebase-admin/messaging': './lib/messaging/index.d.ts', + 'firebase-admin/machine-learning': './lib/machine-learning/index.d.ts', 'firebase-admin/project-management': './lib/project-management/index.d.ts', 'firebase-admin/security-rules': './lib/security-rules/index.d.ts', 'firebase-admin/storage': './lib/storage/index.d.ts', diff --git a/gulpfile.js b/gulpfile.js index 042b44560f..07184a93f9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -84,20 +84,7 @@ gulp.task('compile', function() { const configuration = [ 'lib/**/*.js', - 'lib/**/index.d.ts', - 'lib/firebase-namespace-api.d.ts', - 'lib/core.d.ts', - 'lib/app/*.d.ts', - 'lib/auth/*.d.ts', - 'lib/database/*.d.ts', - 'lib/firestore/*.d.ts', - 'lib/instance-id/*.d.ts', - 'lib/messaging/*.d.ts', - 'lib/project-management/*.d.ts', - 'lib/security-rules/*.d.ts', - 'lib/storage/*.d.ts', - 'lib/remote-config/*.d.ts', - '!lib/utils/index.d.ts', + 'lib/**/*.d.ts', ]; workflow = workflow.pipe(filter(configuration)); diff --git a/src/app/firebase-app.ts b/src/app/firebase-app.ts index 5bbcf4ad1b..23ca6001c7 100644 --- a/src/app/firebase-app.ts +++ b/src/app/firebase-app.ts @@ -24,7 +24,7 @@ import { FirebaseNamespaceInternals } from './firebase-namespace'; import { AppErrorCodes, FirebaseAppError } from '../utils/error'; import { Auth } from '../auth/index'; -import { MachineLearning } from '../machine-learning/machine-learning'; +import { MachineLearning } from '../machine-learning/index'; import { Messaging } from '../messaging/index'; import { Storage } from '../storage/index'; import { Database } from '../database/index'; @@ -328,11 +328,8 @@ export class FirebaseApp implements app.App { * @return The Machine Learning service instance of this app */ public machineLearning(): MachineLearning { - return this.ensureService_('machine-learning', () => { - const machineLearningService: typeof MachineLearning = - require('../machine-learning/machine-learning').MachineLearning; - return new machineLearningService(this); - }); + const fn = require('../machine-learning/index').getMachineLearning; + return fn(this); } /** diff --git a/src/machine-learning/index.ts b/src/machine-learning/index.ts index 7e5a8078cd..c12da2b832 100644 --- a/src/machine-learning/index.ts +++ b/src/machine-learning/index.ts @@ -14,7 +14,46 @@ * limitations under the License. */ -import { app } from '../firebase-namespace-api'; +import { App, getApp } from '../app'; +import { FirebaseApp } from '../app/firebase-app'; +import { MachineLearning } from './machine-learning'; + +export { + MachineLearning, + ListModelsResult, + Model, + TFLiteModel, +} from './machine-learning'; +export { + AutoMLTfliteModelOptions, + GcsTfliteModelOptions, + ListModelsOptions, + ModelOptions, + ModelOptionsBase, +} from './machine-learning-api-client'; + +export function getMachineLearning(app?: App): MachineLearning { + if (typeof app === 'undefined') { + app = getApp(); + } + + const firebaseApp: FirebaseApp = app as FirebaseApp; + return firebaseApp.getOrInitService('machineLearning', (app) => new MachineLearning(app)); +} + +import { + ListModelsResult as TListModelsResult, + MachineLearning as TMachineLearning, + Model as TModel, + TFLiteModel as TTFLiteModel, +} from './machine-learning'; +import { + AutoMLTfliteModelOptions as TAutoMLTfliteModelOptions, + GcsTfliteModelOptions as TGcsTfliteModelOptions, + ListModelsOptions as TListModelsOptions, + ModelOptions as TModelOptions, + ModelOptionsBase as TModelOptionsBase, +} from './machine-learning-api-client'; /** * Gets the {@link machineLearning.MachineLearning `MachineLearning`} service for the @@ -45,238 +84,18 @@ import { app } from '../firebase-namespace-api'; * @return The default `MachineLearning` service if no app is provided or the * `MachineLearning` service associated with the provided app. */ -export declare function machineLearning(app?: app.App): machineLearning.MachineLearning; +export declare function machineLearning(app?: App): machineLearning.MachineLearning; /* eslint-disable @typescript-eslint/no-namespace */ export namespace machineLearning { - /** - * Firebase ML Model input objects - */ - export interface ModelOptionsBase { - displayName?: string; - tags?: string[]; - } - - export interface GcsTfliteModelOptions extends ModelOptionsBase { - tfliteModel: { - gcsTfliteUri: string; - }; - } - - export interface AutoMLTfliteModelOptions extends ModelOptionsBase { - tfliteModel: { - automlModel: string; - }; - } - - export type ModelOptions = ModelOptionsBase | GcsTfliteModelOptions | AutoMLTfliteModelOptions; - - /** - * A TensorFlow Lite Model output object - * - * One of either the `gcsTfliteUri` or `automlModel` properties will be - * defined. - */ - export interface TFLiteModel { - /** The size of the model. */ - readonly sizeBytes: number; - - /** The URI from which the model was originally provided to Firebase. */ - readonly gcsTfliteUri?: string; - /** - * The AutoML model reference from which the model was originally provided - * to Firebase. - */ - readonly automlModel?: string; - } - - /** - * A Firebase ML Model output object - */ - export interface Model { - /** The ID of the model. */ - readonly modelId: string; - - /** - * The model's name. This is the name you use from your app to load the - * model. - */ - readonly displayName: string; - - /** - * The model's tags, which can be used to group or filter models in list - * operations. - */ - readonly tags?: string[]; - - /** The timestamp of the model's creation. */ - readonly createTime: string; - - /** The timestamp of the model's most recent update. */ - readonly updateTime: string; - - /** Error message when model validation fails. */ - readonly validationError?: string; - - /** True if the model is published. */ - readonly published: boolean; - - /** - * The ETag identifier of the current version of the model. This value - * changes whenever you update any of the model's properties. - */ - readonly etag: string; - - /** - * The hash of the model's `tflite` file. This value changes only when - * you upload a new TensorFlow Lite model. - */ - readonly modelHash?: string; - - /** - * True if the model is locked by a server-side operation. You can't make - * changes to a locked model. See {@link waitForUnlocked `waitForUnlocked()`}. - */ - readonly locked: boolean; - - /** - * Wait for the model to be unlocked. - * - * @param {number} maxTimeMillis The maximum time in milliseconds to wait. - * If not specified, a default maximum of 2 minutes is used. - * - * @return {Promise} A promise that resolves when the model is unlocked - * or the maximum wait time has passed. - */ - waitForUnlocked(maxTimeMillis?: number): Promise; - - /** - * Return the model as a JSON object. - */ - toJSON(): {[key: string]: any}; - - /** Metadata about the model's TensorFlow Lite model file. */ - readonly tfliteModel?: TFLiteModel; - } - - /** - * Interface representing options for listing Models. - */ - export interface ListModelsOptions { - /** - * An expression that specifies how to filter the results. - * - * Examples: - * - * ``` - * display_name = your_model - * display_name : experimental_* - * tags: face_detector AND tags: experimental - * state.published = true - * ``` - * - * See https://firebase.google.com/docs/ml/manage-hosted-models#list_your_projects_models - */ - filter?: string; - - /** The number of results to return in each page. */ - pageSize?: number; - - /** A token that specifies the result page to return. */ - pageToken?: string; - } - - /** Response object for a listModels operation. */ - export interface ListModelsResult { - /** A list of models in your project. */ - readonly models: Model[]; - - /** - * A token you can use to retrieve the next page of results. If null, the - * current page is the final page. - */ - readonly pageToken?: string; - } - - - /** - * The Firebase `MachineLearning` service interface. - * - * Do not call this constructor directly. Instead, use - * [`admin.machineLearning()`](admin.machineLearning#machineLearning). - */ - export interface MachineLearning { - /** - * The {@link app.App} associated with the current `MachineLearning` - * service instance. - */ - app: app.App; - - /** - * Creates a model in the current Firebase project. - * - * @param {ModelOptions} model The model to create. - * - * @return {Promise} A Promise fulfilled with the created model. - */ - createModel(model: ModelOptions): Promise; - - /** - * Updates a model's metadata or model file. - * - * @param {string} modelId The ID of the model to update. - * @param {ModelOptions} model The model fields to update. - * - * @return {Promise} A Promise fulfilled with the updated model. - */ - updateModel(modelId: string, model: ModelOptions): Promise; - - /** - * Publishes a Firebase ML model. - * - * A published model can be downloaded to client apps. - * - * @param {string} modelId The ID of the model to publish. - * - * @return {Promise} A Promise fulfilled with the published model. - */ - publishModel(modelId: string): Promise; - - /** - * Unpublishes a Firebase ML model. - * - * @param {string} modelId The ID of the model to unpublish. - * - * @return {Promise} A Promise fulfilled with the unpublished model. - */ - unpublishModel(modelId: string): Promise; - - /** - * Gets the model specified by the given ID. - * - * @param {string} modelId The ID of the model to get. - * - * @return {Promise} A Promise fulfilled with the model object. - */ - getModel(modelId: string): Promise; - - /** - * Lists the current project's models. - * - * @param {ListModelsOptions} options The listing options. - * - * @return {Promise} A promise that - * resolves with the current (filtered) list of models and the next page - * token. For the last page, an empty list of models and no page token - * are returned. - */ - listModels(options?: ListModelsOptions): Promise; - - /** - * Deletes a model from the current project. - * - * @param {string} modelId The ID of the model to delete. - */ - deleteModel(modelId: string): Promise; - } + export type ListModelsResult = TListModelsResult; + export type MachineLearning = TMachineLearning; + export type Model = TModel; + export type TFLiteModel = TTFLiteModel; + + export type AutoMLTfliteModelOptions = TAutoMLTfliteModelOptions; + export type GcsTfliteModelOptions = TGcsTfliteModelOptions; + export type ListModelsOptions = TListModelsOptions; + export type ModelOptions = TModelOptions; + export type ModelOptionsBase = TModelOptionsBase; } diff --git a/src/machine-learning/machine-learning-api-client.ts b/src/machine-learning/machine-learning-api-client.ts index 18a53180cd..d2923a3143 100644 --- a/src/machine-learning/machine-learning-api-client.ts +++ b/src/machine-learning/machine-learning-api-client.ts @@ -14,19 +14,64 @@ * limitations under the License. */ +import { App } from '../app'; +import { FirebaseApp } from '../app/firebase-app'; import { HttpRequestConfig, HttpClient, HttpError, AuthorizedHttpClient, ExponentialBackoffPoller } from '../utils/api-request'; import { PrefixedFirebaseError } from '../utils/error'; -import { FirebaseMachineLearningError, MachineLearningErrorCode } from './machine-learning-utils'; import * as utils from '../utils/index'; import * as validator from '../utils/validator'; -import { FirebaseApp } from '../app/firebase-app'; -import { machineLearning } from './index'; +import { FirebaseMachineLearningError, MachineLearningErrorCode } from './machine-learning-utils'; + +/** + * Firebase ML Model input objects + */ +export interface ModelOptionsBase { + displayName?: string; + tags?: string[]; +} + +export interface GcsTfliteModelOptions extends ModelOptionsBase { + tfliteModel: { + gcsTfliteUri: string; + }; +} + +export interface AutoMLTfliteModelOptions extends ModelOptionsBase { + tfliteModel: { + automlModel: string; + }; +} -import GcsTfliteModelOptions = machineLearning.GcsTfliteModelOptions; -import ListModelsOptions = machineLearning.ListModelsOptions; -import ModelOptions = machineLearning.ModelOptions; +export type ModelOptions = ModelOptionsBase | GcsTfliteModelOptions | AutoMLTfliteModelOptions; + +/** + * Interface representing options for listing Models. + */ +export interface ListModelsOptions { + /** + * An expression that specifies how to filter the results. + * + * Examples: + * + * ``` + * display_name = your_model + * display_name : experimental_* + * tags: face_detector AND tags: experimental + * state.published = true + * ``` + * + * See https://firebase.google.com/docs/ml/manage-hosted-models#list_your_projects_models + */ + filter?: string; + + /** The number of results to return in each page. */ + pageSize?: number; + + /** A token that specifies the result page to return. */ + pageToken?: string; +} const ML_V1BETA2_API = 'https://firebaseml.googleapis.com/v1beta2'; const FIREBASE_VERSION_HEADER = { @@ -92,13 +137,14 @@ export interface OperationResponse { /** * Class that facilitates sending requests to the Firebase ML backend API. * - * @private + * @internal */ export class MachineLearningApiClient { + private readonly httpClient: HttpClient; private projectIdPrefix?: string; - constructor(private readonly app: FirebaseApp) { + constructor(private readonly app: App) { if (!validator.isNonNullObject(app) || !('options' in app)) { throw new FirebaseMachineLearningError( 'invalid-argument', @@ -106,7 +152,7 @@ export class MachineLearningApiClient { + 'Firebase app instance.'); } - this.httpClient = new AuthorizedHttpClient(app); + this.httpClient = new AuthorizedHttpClient(app as FirebaseApp); } public createModel(model: ModelOptions): Promise { diff --git a/src/machine-learning/machine-learning.ts b/src/machine-learning/machine-learning.ts index 986c0b852e..c150c89e61 100644 --- a/src/machine-learning/machine-learning.ts +++ b/src/machine-learning/machine-learning.ts @@ -14,37 +14,63 @@ * limitations under the License. */ -import { FirebaseApp } from '../app/firebase-app'; -import { - MachineLearningApiClient, ModelResponse, ModelUpdateOptions, isGcsTfliteModelOptions -} from './machine-learning-api-client'; +import { App } from '../app'; +import { getStorage } from '../storage/index'; import { FirebaseError } from '../utils/error'; import * as validator from '../utils/validator'; -import { FirebaseMachineLearningError } from './machine-learning-utils'; import { deepCopy } from '../utils/deep-copy'; import * as utils from '../utils'; -import { machineLearning } from './index'; +import { + MachineLearningApiClient, ModelResponse, ModelUpdateOptions, isGcsTfliteModelOptions, + ListModelsOptions, ModelOptions, +} from './machine-learning-api-client'; +import { FirebaseMachineLearningError } from './machine-learning-utils'; -import ListModelsOptions = machineLearning.ListModelsOptions; -import ListModelsResult = machineLearning.ListModelsResult; -import MachineLearningInterface = machineLearning.MachineLearning; -import ModelInterface = machineLearning.Model; -import ModelOptions = machineLearning.ModelOptions; -import TFLiteModel = machineLearning.TFLiteModel; +/** Response object for a listModels operation. */ +export interface ListModelsResult { + /** A list of models in your project. */ + readonly models: Model[]; + + /** + * A token you can use to retrieve the next page of results. If null, the + * current page is the final page. + */ + readonly pageToken?: string; +} + +/** + * A TensorFlow Lite Model output object + * + * One of either the `gcsTfliteUri` or `automlModel` properties will be + * defined. + */ +export interface TFLiteModel { + /** The size of the model. */ + readonly sizeBytes: number; + + /** The URI from which the model was originally provided to Firebase. */ + readonly gcsTfliteUri?: string; + /** + * The AutoML model reference from which the model was originally provided + * to Firebase. + */ + readonly automlModel?: string; +} /** * The Firebase Machine Learning class */ -export class MachineLearning implements MachineLearningInterface { +export class MachineLearning { private readonly client: MachineLearningApiClient; - private readonly appInternal: FirebaseApp; + private readonly appInternal: App; /** - * @param {FirebaseApp} app The app for this ML service. + * @param app The app for this ML service. * @constructor + * @internal */ - constructor(app: FirebaseApp) { + constructor(app: App) { if (!validator.isNonNullObject(app) || !('options' in app)) { throw new FirebaseError({ code: 'machine-learning/invalid-argument', @@ -60,9 +86,9 @@ export class MachineLearning implements MachineLearningInterface { /** * Returns the app associated with this ML instance. * - * @return {FirebaseApp} The app associated with this ML instance. + * @return The app associated with this ML instance. */ - public get app(): FirebaseApp { + public get app(): App { return this.appInternal; } @@ -207,7 +233,7 @@ export class MachineLearning implements MachineLearningInterface { } const bucketName = matches[1]; const blobName = matches[2]; - const bucket = this.appInternal.storage().bucket(bucketName); + const bucket = getStorage(this.app).bucket(bucketName); const blob = bucket.file(blobName); return blob.getSignedUrl({ action: 'read', @@ -219,10 +245,13 @@ export class MachineLearning implements MachineLearningInterface { /** * A Firebase ML Model output object. */ -export class Model implements ModelInterface { +export class Model { private model: ModelResponse; private readonly client?: MachineLearningApiClient; + /** + * @internal + */ constructor(model: ModelResponse, client: MachineLearningApiClient) { this.model = Model.validateAndClone(model); this.client = client; @@ -308,7 +337,6 @@ export class Model implements ModelInterface { return jsonModel; } - /** * Wait for the active operations on the model to complete. * @param maxTimeMillis The number of milliseconds to wait for the model to be unlocked. If unspecified, diff --git a/test/unit/index.spec.ts b/test/unit/index.spec.ts index 8c8862c826..b6bded8145 100644 --- a/test/unit/index.spec.ts +++ b/test/unit/index.spec.ts @@ -51,6 +51,7 @@ import './messaging/messaging.spec'; import './messaging/batch-requests.spec'; // Machine Learning +import './machine-learning/index.spec'; import './machine-learning/machine-learning.spec'; import './machine-learning/machine-learning-api-client.spec'; diff --git a/test/unit/machine-learning/index.spec.ts b/test/unit/machine-learning/index.spec.ts new file mode 100644 index 0000000000..1937f5387d --- /dev/null +++ b/test/unit/machine-learning/index.spec.ts @@ -0,0 +1,75 @@ +/*! + * @license + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +import * as chai from 'chai'; +import * as sinonChai from 'sinon-chai'; +import * as chaiAsPromised from 'chai-as-promised'; + +import * as mocks from '../../resources/mocks'; +import { App } from '../../../src/app/index'; +import { getMachineLearning, MachineLearning } from '../../../src/machine-learning/index'; + +chai.should(); +chai.use(sinonChai); +chai.use(chaiAsPromised); + +const expect = chai.expect; + +describe('MachineLearning', () => { + let mockApp: App; + let mockCredentialApp: App; + + const noProjectIdError = 'Failed to determine project ID. Initialize the SDK ' + + 'with service account credentials, or set project ID as an app option. Alternatively, set the ' + + 'GOOGLE_CLOUD_PROJECT environment variable.'; + + beforeEach(() => { + mockApp = mocks.app(); + mockCredentialApp = mocks.mockCredentialApp(); + }); + + describe('getMachineLearning()', () => { + it('should throw when default app is not available', () => { + expect(() => { + return getMachineLearning(); + }).to.throw('The default Firebase app does not exist.'); + }); + + it('should reject given an invalid credential without project ID', () => { + // Project ID not set in the environment. + delete process.env.GOOGLE_CLOUD_PROJECT; + delete process.env.GCLOUD_PROJECT; + const client = getMachineLearning(mockCredentialApp); + return client.getModel('test') + .should.eventually.rejectedWith(noProjectIdError); + }); + + it('should not throw given a valid app', () => { + expect(() => { + return getMachineLearning(mockApp); + }).not.to.throw(); + }); + + it('should return the same instance for a given app instance', () => { + const client1: MachineLearning = getMachineLearning(mockApp); + const client2: MachineLearning = getMachineLearning(mockApp); + expect(client1).to.equal(client2); + }); + }); +}); diff --git a/test/unit/machine-learning/machine-learning-api-client.spec.ts b/test/unit/machine-learning/machine-learning-api-client.spec.ts index 1dc8a0df35..01f6936869 100644 --- a/test/unit/machine-learning/machine-learning-api-client.spec.ts +++ b/test/unit/machine-learning/machine-learning-api-client.spec.ts @@ -27,10 +27,7 @@ import { FirebaseAppError } from '../../../src/utils/error'; import { FirebaseApp } from '../../../src/app/firebase-app'; import { getSdkVersion } from '../../../src/utils/index'; import { MachineLearningApiClient } from '../../../src/machine-learning/machine-learning-api-client'; -import { machineLearning } from '../../../src/machine-learning/index'; - -import ListModelsOptions = machineLearning.ListModelsOptions; -import ModelOptions = machineLearning.ModelOptions; +import { ListModelsOptions, ModelOptions } from '../../../src/machine-learning/index'; const expect = chai.expect; diff --git a/test/unit/machine-learning/machine-learning.spec.ts b/test/unit/machine-learning/machine-learning.spec.ts index 9c8314fc03..8791ca9c87 100644 --- a/test/unit/machine-learning/machine-learning.spec.ts +++ b/test/unit/machine-learning/machine-learning.spec.ts @@ -21,7 +21,6 @@ import * as chai from 'chai'; import * as sinon from 'sinon'; import { FirebaseApp } from '../../../src/app/firebase-app'; import * as mocks from '../../resources/mocks'; -import { MachineLearning, Model } from '../../../src/machine-learning/machine-learning'; import { MachineLearningApiClient, StatusErrorResponse, @@ -30,9 +29,7 @@ import { } from '../../../src/machine-learning/machine-learning-api-client'; import { FirebaseMachineLearningError } from '../../../src/machine-learning/machine-learning-utils'; import { deepCopy } from '../../../src/utils/deep-copy'; -import { machineLearning } from '../../../src/machine-learning/index'; - -import ModelOptions = machineLearning.ModelOptions; +import { MachineLearning, Model, ModelOptions } from '../../../src/machine-learning/index'; const expect = chai.expect;