Skip to content

Commit a78853b

Browse files
committed
fix(integration_tests): fix tasks v1
1 parent 68ebad0 commit a78853b

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

integration_test_declarative/tests/utils.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,42 @@ export async function retry<T>(fn: () => Promise<T>, options?: RetryOptions): Pr
3838
throw new Error(`Max retries exceeded: result = ${result}`);
3939
}
4040

41-
export async function createTask(queueName: string, testId: string): Promise<string> {
42-
const GCLOUD_PROJECT = process.env.GCLOUD_PROJECT || "functions-integration-tests";
43-
const REGION = "us-central1";
44-
const cloudTasksClient = new CloudTasksClient();
41+
export async function createTask(
42+
project: string,
43+
queue: string,
44+
location: string,
45+
url: string,
46+
payload: Record<string, any>
47+
): Promise<string> {
48+
const client = new CloudTasksClient();
49+
const parent = client.queuePath(project, location, queue);
50+
51+
const serviceAccountPath =
52+
process.env.GOOGLE_APPLICATION_CREDENTIALS ||
53+
"/Users/jacob/firebase-functions/integration_test_declarative/sa.json";
54+
if (!serviceAccountPath) {
55+
throw new Error("Environment configured incorrectly.");
56+
}
57+
const serviceAccount = await import(serviceAccountPath);
4558

46-
const parent = cloudTasksClient.queuePath(GCLOUD_PROJECT, REGION, queueName);
4759
const task = {
4860
httpRequest: {
4961
httpMethod: "POST" as const,
50-
url: `https://${REGION}-${GCLOUD_PROJECT}.cloudfunctions.net/${queueName}`,
62+
url,
5163
oidcToken: {
52-
serviceAccountEmail: `${GCLOUD_PROJECT}@appspot.gserviceaccount.com`,
64+
serviceAccountEmail: serviceAccount.client_email,
65+
},
66+
headers: {
67+
"Content-Type": "application/json",
5368
},
54-
body: Buffer.from(JSON.stringify({ testId })).toString("base64"),
55-
headers: { "Content-Type": "application/json" },
69+
body: Buffer.from(JSON.stringify(payload)).toString("base64"),
5670
},
5771
};
5872

59-
const request = { parent, task };
60-
const [response] = await cloudTasksClient.createTask(request);
73+
const [response] = await client.createTask({ parent, task });
74+
if (!response) {
75+
throw new Error("Unable to create task");
76+
}
6177
return response.name || "";
6278
}
6379

@@ -88,7 +104,7 @@ async function fetchDefaultDevice(accessToken: string): Promise<AndroidDevice> {
88104
if (!resp.ok) {
89105
throw new Error(resp.statusText);
90106
}
91-
const data = await resp.json() as any;
107+
const data = (await resp.json()) as any;
92108
const models = data?.androidDeviceCatalog?.models || [];
93109
const defaultModels = models.filter(
94110
(m: any) =>
@@ -161,4 +177,4 @@ async function createTestMatrix(
161177
throw new Error(resp.statusText);
162178
}
163179
return;
164-
}
180+
}

integration_test_declarative/tests/v1/tasks.test.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,53 @@ describe("Firebase Tasks (v1)", () => {
1818

1919
describe("task queue onDispatch trigger", () => {
2020
let loggedContext: admin.firestore.DocumentData | undefined;
21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2122
let taskId: string;
2223

2324
beforeAll(async () => {
2425
// Function name becomes the queue name in v1, no separators needed
2526
const queueName = `tasksOnDispatchTests${testId}`;
27+
const projectId = process.env.GCLOUD_PROJECT || "functions-integration-tests";
28+
const region = "us-central1";
29+
const url = `https://${region}-${projectId}.cloudfunctions.net/${queueName}`;
2630

27-
taskId = await createTask(queueName, testId);
31+
// Use Google Cloud Tasks SDK to get proper Cloud Tasks event context
32+
taskId = await createTask(projectId, queueName, region, url, { data: { testId } });
2833

29-
loggedContext = await retry(() =>
30-
admin
31-
.firestore()
32-
.collection("tasksOnDispatchTests")
33-
.doc(testId)
34-
.get()
35-
.then((logSnapshot) => logSnapshot.data())
34+
loggedContext = await retry(
35+
() => {
36+
console.log(`🔍 Checking Firestore for document: tasksOnDispatchTests/${testId}`);
37+
return admin
38+
.firestore()
39+
.collection("tasksOnDispatchTests")
40+
.doc(testId)
41+
.get()
42+
.then((logSnapshot) => {
43+
const data = logSnapshot.data();
44+
console.log(`📄 Firestore data:`, data);
45+
return data;
46+
});
47+
},
48+
{ maxRetries: 30, checkForUndefined: true }
3649
);
3750
});
3851

39-
it("should have the right eventType", () => {
40-
expect(loggedContext?.eventType).toEqual("google.cloud.tasks.queue.v2.task.dispatch");
52+
it("should have correct event id", () => {
53+
expect(loggedContext?.id).toBeDefined();
4154
});
4255

43-
it("should have eventId", () => {
44-
expect(loggedContext?.eventId).toBeDefined();
56+
it("should have queue name", () => {
57+
expect(loggedContext?.queueName).toEqual(`tasksOnDispatchTests${testId}`);
4558
});
4659

47-
it("should have timestamp", () => {
48-
expect(loggedContext?.timestamp).toBeDefined();
60+
it("should have retry count", () => {
61+
expect(loggedContext?.retryCount).toBeDefined();
62+
expect(typeof loggedContext?.retryCount).toBe("number");
4963
});
5064

51-
it("should have resource", () => {
52-
expect(loggedContext?.resource).toBeDefined();
53-
expect(loggedContext?.resource?.service).toEqual("cloudtasks.googleapis.com");
54-
expect(loggedContext?.resource?.name).toContain(taskId);
65+
it("should have execution count", () => {
66+
expect(loggedContext?.executionCount).toBeDefined();
67+
expect(typeof loggedContext?.executionCount).toBe("number");
5568
});
5669
});
57-
});
70+
});

0 commit comments

Comments
 (0)