firebase / firebase-functions-test

MIT License
235 stars 53 forks source link

makeMessage is incompatible with wrapV2 #235

Open csimmons0 opened 5 months ago

csimmons0 commented 5 months ago

Version info

firebase-functions-test: 3.3.0

firebase-functions: 5.0.1

firebase-admin: 12.1.1

Test case

import {makeMessage} from "firebase-functions-test/lib/providers/pubsub";
import {onMessagePublished} from "firebase-functions/v2/pubsub"
import {wrapV2} from "firebase-functions-test/lib/v2";
import {expect} from "chai";

describe("example", () => {
    it("makeMessage breaks PubSub testing",
        async () => {
            const helloBase64 = "aGVsbG8="
            const rawPartial = {data: {message: {data: helloBase64}}}
            const partialUsingMakeMessage= {data: {message: makeMessage(helloBase64)}}
            const getEventData = onMessagePublished(
                "custom-test-topic",
                (event) => {
                    return event.data.message.data
                }
            )

            /*
                The raw partial looks like:
                {
                  data: {
                    message: { data: 'aGVsbG8=' }
                  }
                }
             */
            console.log("rawPartial: ", rawPartial)

            /*
                The makeMessage partial looks like:
                {
                  data: {
                    message: Message { data: 'aGVsbG8=', attributes: {}, _json: undefined }
                  }
                }
             */
            console.log("partialUsingMakeMessage: ", partialUsingMakeMessage)

            const eventDataUsingRawPartial = wrapV2(getEventData)(rawPartial)
            expect(eventDataUsingRawPartial).equals(helloBase64)

            // This will throw an error: SyntaxError: Unexpected token 'h', "hello" is not valid JSON.
            const eventDataUsingMakeMessage = wrapV2(getEventData)(partialUsingMakeMessage)

            // This line won't be reached.
            expect(eventDataUsingMakeMessage).equals(helloBase64)
        })
})

Steps to reproduce

Just run the code as a mocha test.

Expected behavior

If I wrap a v2 cloud function that consumes PubSub events with wrapV2 and then invoke the wrapped function on {data: {message: makeMessage(someBase64Data}, the cloud function gets invoked, and the event passed to it has a data.message.data field that matches someBase64Data.

Actual behavior

Some code decodes the base64 data and then tries to parse it as JSON, which results in a SyntaxError.

Additional thoughts