Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send package versions as bundle metadata #1354

Merged
merged 17 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pwa-kit-dev/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
...base,
coverageThreshold: {
global: {
branches: 67,
branches: 65,
yunakim714 marked this conversation as resolved.
Show resolved Hide resolved
functions: 67,
lines: 70,
statements: 70
Expand Down
81 changes: 76 additions & 5 deletions packages/pwa-kit-dev/src/utils/script-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import {mkdtemp, rm, writeFile, readJsonSync} from 'fs-extra'
import {mkdtemp, rm, writeFile, readJsonSync, readJson} from 'fs-extra'
import path from 'path'
import os from 'os'
import * as scriptUtils from './script-utils'
const pkg = readJsonSync(path.join(__dirname, '../../package.json'))

jest.mock('fs-extra', () => {
return {
...jest.requireActual('fs-extra'),
readJson: jest.fn()
}
})

describe('scriptUtils', () => {
const originalEnv = process.env
let tmpDir
Expand All @@ -23,6 +30,7 @@ describe('scriptUtils', () => {
afterEach(async () => {
process.env = originalEnv
tmpDir && (await rm(tmpDir, {recursive: true}))
jest.resetAllMocks()
})

test('glob() with no patterns matches nothing', () => {
Expand Down Expand Up @@ -88,6 +96,7 @@ describe('scriptUtils', () => {
})

test('getHeaders', async () => {
readJson.mockReturnValue(pkg)
const client = new scriptUtils.CloudAPIClient({credentials: {username, api_key}})
expect(await client.getHeaders()).toEqual({
'User-Agent': `${pkg.name}@${pkg.version}`,
Expand All @@ -96,11 +105,36 @@ describe('scriptUtils', () => {
})
})

test('getPkgJSON', async () => {
const pkg = await scriptUtils.getPkgJSON()
expect(pkg.name).toBe('@salesforce/pwa-kit-dev')
describe('getPkgJSON', () => {
test('should work', async () => {
readJson.mockReturnValue(pkg)
const pkgJson = await scriptUtils.getPkgJSON()
expect(pkgJson.name).toBe('@salesforce/pwa-kit-dev')
})

test('should return default package.json data when no valid file is found', async () => {
readJson.mockRejectedValue(Error)
const result = await scriptUtils.getPkgJSON()
expect(result).toEqual({name: '@salesforce/pwa-kit-dev', version: 'unknown'})
})
})

describe('getProjectPkg', () => {
test('should work', async () => {
readJson.mockReturnValue(pkg)
const pkgJson = await scriptUtils.getProjectPkg()
expect(pkgJson.name).toBe('@salesforce/pwa-kit-dev')
})

test('should throw', async () => {
readJson.mockRejectedValue(Error)
await expect(scriptUtils.getProjectPkg()).rejects.toThrow(
`Could not read project package at "${path.join(process.cwd(), 'package.json')}"`
)
})
})

jest.unmock('fs-extra')
describe('defaultMessage', () => {
test('works', async () => {
const mockGit = {branch: () => 'branch', short: () => 'short'}
Expand Down Expand Up @@ -142,6 +176,7 @@ describe('scriptUtils', () => {

describe('readCredentials', () => {
test('should work', async () => {
readJson.mockReturnValue({username: 'alice', api_key: 'xyz'})
const creds = {username: 'alice', api_key: 'xyz'}
const thePath = path.join(tmpDir, '.mobify.test')
await writeFile(thePath, JSON.stringify(creds), 'utf8')
Expand Down Expand Up @@ -186,6 +221,7 @@ describe('scriptUtils', () => {
})

test('should archive a bundle', async () => {
readJson.mockReturnValue(pkg)
const message = 'message'
const bundle = await scriptUtils.createBundle({
message,
Expand All @@ -198,7 +234,7 @@ describe('scriptUtils', () => {

expect(bundle.message).toEqual(message)
expect(bundle.encoding).toBe('base64')
expect(bundle.ssr_parameters).toEqual({})
expect(bundle.ssr_parameters).toHaveProperty('bundle_metadata.dependencies')
expect(bundle.ssr_only).toEqual(['ssr.js'])
expect(bundle.ssr_shared).toEqual(['ssr.js', 'static/favicon.ico'])

Expand Down Expand Up @@ -237,6 +273,7 @@ describe('scriptUtils', () => {
])(
'should push a built bundle and handle status codes (%p)',
async ({projectSlug, targetSlug, expectedURL, status}) => {
readJson.mockReturnValue(pkg)
const message = 'message'
const bundle = await scriptUtils.createBundle({
message,
Expand Down Expand Up @@ -296,4 +333,38 @@ describe('scriptUtils', () => {
}
)
})

describe('createLoggingToken', () => {
const username = 'user123'
const api_key = '123'
test('createLoggingToken passes', async () => {
readJson.mockReturnValue(pkg)
const projectSlug = 'project-slug'
const targetSlug = 'target-slug'

const text = () => Promise.resolve(JSON.stringify({token: 'token-value'}))
const json = () => Promise.resolve({token: 'token-value'})
const fetchMock = jest.fn(async () => ({status: 200, text, json}))

const client = new scriptUtils.CloudAPIClient({
credentials: {username, api_key},
fetch: fetchMock
})

const fn = async () => await client.createLoggingToken(projectSlug, targetSlug)

expect(await fn()).toBe('token-value')
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
'https://cloud.mobify.com/api/projects/project-slug/target/target-slug/jwt/',
expect.objectContaining({
method: 'POST',
headers: {
Authorization: expect.stringMatching(/^Bearer /),
'User-Agent': `${pkg.name}@${pkg.version}`
}
})
)
})
})
})
9 changes: 9 additions & 0 deletions packages/pwa-kit-dev/src/utils/script-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ interface Bundle {
interface Pkg {
name: string
version: string
dependencies?: {[key: string]: string}
devDependencies?: {[key: string]: string}
}

/**
Expand Down Expand Up @@ -251,6 +253,13 @@ export const createBundle = async ({
archive.finalize()
})
)
.then(async () => {
const {dependencies = {}, devDependencies = {}} = await getProjectPkg()
ssr_parameters = {
...ssr_parameters,
bundle_metadata: {dependencies: {...dependencies, ...devDependencies}}
}
})
.then(() => readFile(destination))
.then((data) => {
const encoding = 'base64'
Expand Down