mirror of
https://github.com/cypress-io/cypress.git
synced 2026-01-09 16:10:11 -06:00
chore: convert cache.js file within server to TypeScript (#31471)
* chore: convert cache.js file within server to TypeScript * update cache calls and await them * updates to imports and switching cache calls to await * remove Partial from Allowed State * change cache calls back to not await * Remove dead code * update memo.push return
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -398,4 +398,7 @@ tooling/v8-snapshot/cache/prod-win32
|
||||
system-tests/lib/validations
|
||||
|
||||
.nx/cache
|
||||
.nx/workspace-data
|
||||
.nx/workspace-data
|
||||
|
||||
# IDE files
|
||||
.cursor
|
||||
@@ -1,4 +1,4 @@
|
||||
import { FoundBrowser, Editor, AllowedState, AllModeOptions, TestingType, BrowserStatus, PACKAGE_MANAGERS, AuthStateName, MIGRATION_STEPS, MigrationStep, BannerState, StudioManagerShape } from '@packages/types'
|
||||
import { FoundBrowser, Editor, AllowedState, AllModeOptions, TestingType, BrowserStatus, PACKAGE_MANAGERS, AuthStateName, MIGRATION_STEPS, MigrationStep, StudioManagerShape } from '@packages/types'
|
||||
import { WizardBundler, CT_FRAMEWORKS, resolveComponentFrameworkDefinition, ErroredFramework } from '@packages/scaffold-config'
|
||||
import type { NexusGenObjects } from '@packages/graphql/src/gen/nxs.gen'
|
||||
// tslint:disable-next-line no-implicit-dependencies - electron dep needs to be defined
|
||||
@@ -22,7 +22,7 @@ export interface AuthenticatedUserShape {
|
||||
|
||||
export interface ProjectShape {
|
||||
projectRoot: string
|
||||
savedState?: () => Promise<Maybe<SavedStateShape>>
|
||||
savedState?: () => Promise<AllowedState>
|
||||
}
|
||||
|
||||
export interface ServersDataShape {
|
||||
@@ -47,15 +47,6 @@ export interface LocalSettingsDataShape {
|
||||
preferences: AllowedState
|
||||
}
|
||||
|
||||
export interface SavedStateShape {
|
||||
firstOpened?: number | null
|
||||
lastOpened?: number | null
|
||||
promptsShown?: object | null
|
||||
banners?: BannerState | null
|
||||
lastProjectId?: string | null
|
||||
specFilter?: string | null
|
||||
}
|
||||
|
||||
export interface ConfigChildProcessShape {
|
||||
/**
|
||||
* Child process executing the config & sourcing plugin events
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
const _ = require('lodash')
|
||||
const Promise = require('bluebird')
|
||||
const { globalPubSub } = require('@packages/data-context')
|
||||
import _ from 'lodash'
|
||||
import Promise from 'bluebird'
|
||||
import { globalPubSub } from '@packages/data-context'
|
||||
import { fs } from './util/fs'
|
||||
import appData from './util/app_data'
|
||||
import FileUtil from './util/file'
|
||||
import type { Cache, CachedUser, Preferences, Cohort } from '@packages/types'
|
||||
|
||||
const { fs } = require('./util/fs')
|
||||
const appData = require('./util/app_data')
|
||||
const FileUtil = require('./util/file')
|
||||
interface Transaction {
|
||||
get: <T = Cache>(key?: string, defaultValue?: T) => Promise<T>
|
||||
set: (key: string | Partial<Cache>, value?: any) => Promise<void>
|
||||
}
|
||||
|
||||
const fileUtil = new FileUtil({
|
||||
path: appData.path('cache'),
|
||||
@@ -14,34 +19,16 @@ globalPubSub.on('test:cleanup', () => {
|
||||
fileUtil.__resetForTest()
|
||||
})
|
||||
|
||||
const convertProjectsToArray = function (obj) {
|
||||
// if our project structure is not
|
||||
// an array then its legacy and we
|
||||
// need to convert it
|
||||
if (!_.isArray(obj.PROJECTS)) {
|
||||
obj.PROJECTS = _.chain(obj.PROJECTS).values().map('PATH').compact().value()
|
||||
|
||||
return obj
|
||||
}
|
||||
}
|
||||
|
||||
const renameSessionToken = function (obj) {
|
||||
let st
|
||||
|
||||
if (obj.USER && (st = obj.USER.session_token)) {
|
||||
delete obj.USER.session_token
|
||||
obj.USER.sessionToken = st
|
||||
|
||||
return obj
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
export const cache = {
|
||||
path: fileUtil.path,
|
||||
|
||||
defaults () {
|
||||
defaults (): Cache {
|
||||
return {
|
||||
USER: {},
|
||||
USER: {
|
||||
authToken: '',
|
||||
name: '',
|
||||
email: '',
|
||||
},
|
||||
PROJECTS: [],
|
||||
PROJECT_PREFERENCES: {},
|
||||
PROJECTS_CONFIG: {},
|
||||
@@ -49,55 +36,35 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
_applyRewriteRules (obj = {}) {
|
||||
return _.reduce([convertProjectsToArray, renameSessionToken], (memo, fn) => {
|
||||
let ret
|
||||
|
||||
ret = fn(memo)
|
||||
|
||||
if (ret) {
|
||||
return ret
|
||||
}
|
||||
|
||||
return memo
|
||||
}
|
||||
, _.cloneDeep(obj))
|
||||
},
|
||||
|
||||
read () {
|
||||
_read (): Promise<Cache> {
|
||||
return fileUtil.get().then((contents) => {
|
||||
return _.defaults(contents, this.defaults())
|
||||
})
|
||||
},
|
||||
|
||||
write (obj = {}) {
|
||||
return fileUtil.set(obj).return(obj)
|
||||
},
|
||||
|
||||
_getProjects (tx) {
|
||||
_getProjects (tx: Transaction): Promise<string[]> {
|
||||
return tx.get('PROJECTS', [])
|
||||
},
|
||||
|
||||
_removeProjects (tx, projects, paths) {
|
||||
// normalize paths in array
|
||||
projects = _.without(projects, ...[].concat(paths))
|
||||
_removeProjects (tx: Transaction, projects: string[], paths: string | string[]): Promise<void> {
|
||||
const pathsArray = Array.isArray(paths) ? paths : [paths]
|
||||
|
||||
projects = _.without(projects, ...pathsArray)
|
||||
|
||||
return tx.set({ PROJECTS: projects })
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {Promise<string[]>}
|
||||
*/
|
||||
getProjectRoots () {
|
||||
return fileUtil.transaction((tx) => {
|
||||
getProjectRoots (): Promise<string[]> {
|
||||
return fileUtil.transaction((tx: Transaction) => {
|
||||
return this._getProjects(tx).then((projects) => {
|
||||
const pathsToRemove = Promise.reduce(projects, (memo, path) => {
|
||||
const pathsToRemove = Promise.reduce(projects, (memo: string[], path) => {
|
||||
return fs.statAsync(path)
|
||||
.catch(() => {
|
||||
return memo.push(path)
|
||||
memo.push(path)
|
||||
|
||||
return memo
|
||||
}).return(memo)
|
||||
}
|
||||
, [])
|
||||
}, [])
|
||||
|
||||
return pathsToRemove.then((removedPaths) => {
|
||||
return this._removeProjects(tx, projects, removedPaths)
|
||||
@@ -108,16 +75,16 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
removeProject (path) {
|
||||
return fileUtil.transaction((tx) => {
|
||||
removeProject (path: string): Promise<void> {
|
||||
return fileUtil.transaction((tx: Transaction) => {
|
||||
return this._getProjects(tx).then((projects) => {
|
||||
return this._removeProjects(tx, projects, path)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
insertProject (path) {
|
||||
return fileUtil.transaction((tx) => {
|
||||
insertProject (path: string): Promise<void> {
|
||||
return fileUtil.transaction((tx: Transaction) => {
|
||||
return this._getProjects(tx).then((projects) => {
|
||||
// projects are sorted by most recently used, so add a project to
|
||||
// the start or move it to the start if it already exists
|
||||
@@ -136,28 +103,28 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
getUser () {
|
||||
getUser (): Promise<CachedUser> {
|
||||
return fileUtil.get('USER', {})
|
||||
},
|
||||
|
||||
setUser (user) {
|
||||
setUser (user: CachedUser): Promise<void> {
|
||||
return fileUtil.set({ USER: user })
|
||||
},
|
||||
|
||||
removeUser () {
|
||||
removeUser (): Promise<void> {
|
||||
return fileUtil.set({ USER: {} })
|
||||
},
|
||||
|
||||
removeLatestProjects () {
|
||||
removeLatestProjects (): Promise<void> {
|
||||
return fileUtil.set({ PROJECTS: [] })
|
||||
},
|
||||
|
||||
getProjectPreferences () {
|
||||
getProjectPreferences (): Promise<Record<string, Preferences>> {
|
||||
return fileUtil.get('PROJECT_PREFERENCES', {})
|
||||
},
|
||||
|
||||
insertProjectPreferences (projectTitle, projectPreferences) {
|
||||
return fileUtil.transaction((tx) => {
|
||||
insertProjectPreferences (projectTitle: string, projectPreferences: Preferences): Promise<void> {
|
||||
return fileUtil.transaction((tx: Transaction) => {
|
||||
return tx.get('PROJECT_PREFERENCES', {}).then((preferences) => {
|
||||
return tx.set('PROJECT_PREFERENCES', {
|
||||
...preferences,
|
||||
@@ -170,11 +137,11 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
removeAllProjectPreferences () {
|
||||
removeAllProjectPreferences (): Promise<void> {
|
||||
return fileUtil.set({ PROJECT_PREFERENCES: {} })
|
||||
},
|
||||
|
||||
removeProjectPreferences (projectTitle) {
|
||||
removeProjectPreferences (projectTitle: string): Promise<void> {
|
||||
const preferences = fileUtil.get('PROJECT_PREFERENCES', {})
|
||||
|
||||
const updatedPreferences = {
|
||||
@@ -185,7 +152,7 @@ module.exports = {
|
||||
return fileUtil.set({ PROJECT_PREFERENCES: updatedPreferences })
|
||||
},
|
||||
|
||||
getCohorts () {
|
||||
getCohorts (): Promise<Record<string, Cohort>> {
|
||||
return fileUtil.get('COHORTS', {}).then((cohorts) => {
|
||||
Object.keys(cohorts).forEach((key) => {
|
||||
cohorts[key].name = key
|
||||
@@ -195,8 +162,8 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
insertCohort (cohort) {
|
||||
return fileUtil.transaction((tx) => {
|
||||
insertCohort (cohort: Cohort): Promise<void> {
|
||||
return fileUtil.transaction((tx: Transaction) => {
|
||||
return tx.get('COHORTS', {}).then((cohorts) => {
|
||||
return tx.set('COHORTS', {
|
||||
...cohorts,
|
||||
@@ -208,11 +175,10 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
remove () {
|
||||
remove (): Promise<void> {
|
||||
return fileUtil.remove()
|
||||
},
|
||||
|
||||
// for testing purposes
|
||||
|
||||
__get: fileUtil.get.bind(fileUtil),
|
||||
__get: fileUtil.get.bind(fileUtil) as <T = Cache>(key?: string, defaultValue?: T) => Promise<T>,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
const api = require('./api').default
|
||||
const cache = require('../cache')
|
||||
const { cache } = require('../cache')
|
||||
|
||||
import type { CachedUser } from '@packages/types'
|
||||
import type Bluebird from 'bluebird'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const cache = require('./cache')
|
||||
import { cache } from './cache'
|
||||
import type { Cohort } from '@packages/types'
|
||||
const debug = require('debug')('cypress:server:cohorts')
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import auth from './cloud/auth'
|
||||
import user from './cloud/user'
|
||||
import cohorts from './cohorts'
|
||||
import { openProject } from './open_project'
|
||||
import cache from './cache'
|
||||
import { cache } from './cache'
|
||||
import { graphqlSchema } from '@packages/graphql/src/schema'
|
||||
import { openExternal } from './gui/links'
|
||||
import { getUserEditor } from './util/editors'
|
||||
@@ -109,9 +109,7 @@ export function makeDataContext (options: MakeDataContextOptions): DataContext {
|
||||
return cache.removeAllProjectPreferences()
|
||||
},
|
||||
insertProjectPreferencesToCache (projectTitle: string, preferences: Preferences) {
|
||||
// FIXME: this should be awaited (since it writes to disk asynchronously) but is not
|
||||
// https://cypress-io.atlassian.net/browse/UNIFY-1705
|
||||
cache.insertProjectPreferences(projectTitle, preferences)
|
||||
return cache.insertProjectPreferences(projectTitle, preferences)
|
||||
},
|
||||
removeProjectFromCache (path: string) {
|
||||
return cache.removeProject(path)
|
||||
|
||||
@@ -27,7 +27,7 @@ const interactiveMode = require(`../../lib/modes/interactive`)
|
||||
const api = require(`../../lib/cloud/api`).default
|
||||
const cwd = require(`../../lib/cwd`)
|
||||
const user = require(`../../lib/cloud/user`)
|
||||
const cache = require(`../../lib/cache`)
|
||||
const cache = require(`../../lib/cache`).cache
|
||||
const errors = require(`../../lib/errors`)
|
||||
const cypress = require(`../../lib/cypress`)
|
||||
const ProjectBase = require(`../../lib/project-base`).ProjectBase
|
||||
|
||||
@@ -16,7 +16,7 @@ global.proxyquire = require('proxyquire')
|
||||
global.sinon = require('sinon')
|
||||
const _ = require('lodash')
|
||||
const Promise = require('bluebird')
|
||||
const cache = require('../lib/cache')
|
||||
const cache = require('../lib/cache').cache
|
||||
|
||||
require('chai')
|
||||
.use(require('@cypress/sinon-chai'))
|
||||
|
||||
@@ -2,284 +2,204 @@ require('../spec_helper')
|
||||
require(`../../lib/cwd`)
|
||||
|
||||
const Promise = require('bluebird')
|
||||
const { __get } = require('../../lib/cache')
|
||||
const cache = require(`../../lib/cache`)
|
||||
const cache = require(`../../lib/cache`).cache
|
||||
const { fs } = require(`../../lib/util/fs`)
|
||||
const Fixtures = require('@tooling/system-tests')
|
||||
|
||||
describe('lib/cache', () => {
|
||||
beforeEach(() => {
|
||||
return cache.remove()
|
||||
})
|
||||
|
||||
context('#_applyRewriteRules', () => {
|
||||
beforeEach(function () {
|
||||
return fs.readJsonAsync(Fixtures.path('server/old_cache.json')).then((oldCache) => {
|
||||
this.oldCache = oldCache
|
||||
})
|
||||
})
|
||||
|
||||
it('converts object to array of paths', function () {
|
||||
const obj = cache._applyRewriteRules(this.oldCache)
|
||||
|
||||
expect(obj).to.deep.eq({
|
||||
USER: { name: 'brian', sessionToken: 'abc123' },
|
||||
PROJECTS: [
|
||||
'/Users/bmann/Dev/examples-angular-circle-ci',
|
||||
'/Users/bmann/Dev/cypress-core-gui',
|
||||
'/Users/bmann/Dev/cypress-app/spec/fixtures/projects/todos',
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('compacts non PATH values', () => {
|
||||
const obj = cache._applyRewriteRules({
|
||||
USER: {},
|
||||
PROJECTS: {
|
||||
one: { PATH: 'foo/bar' },
|
||||
two: { FOO: 'baz' },
|
||||
},
|
||||
})
|
||||
|
||||
expect(obj).to.deep.eq({
|
||||
USER: {},
|
||||
PROJECTS: ['foo/bar'],
|
||||
})
|
||||
})
|
||||
|
||||
it('converts session_token to session_token', () => {
|
||||
const obj = cache._applyRewriteRules({
|
||||
USER: { id: 1, session_token: 'abc123' },
|
||||
PROJECTS: [],
|
||||
})
|
||||
|
||||
expect(obj).to.deep.eq({
|
||||
USER: { id: 1, sessionToken: 'abc123' },
|
||||
PROJECTS: [],
|
||||
})
|
||||
})
|
||||
beforeEach(async () => {
|
||||
await cache.remove()
|
||||
})
|
||||
|
||||
context('projects', () => {
|
||||
describe('#insertProject', () => {
|
||||
it('inserts project by path', () => {
|
||||
return cache.insertProject('foo/bar')
|
||||
.then(() => {
|
||||
return cache.__get('PROJECTS')
|
||||
}).then((projects) => {
|
||||
expect(projects).to.deep.eq(['foo/bar'])
|
||||
})
|
||||
it('inserts project by path', async () => {
|
||||
await cache.insertProject('foo/bar')
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
expect(projects).to.deep.eq(['foo/bar'])
|
||||
})
|
||||
|
||||
it('inserts project at the start', () => {
|
||||
return cache.insertProject('foo')
|
||||
.then(() => {
|
||||
return cache.insertProject('bar')
|
||||
}).then(() => {
|
||||
return cache.__get('PROJECTS')
|
||||
}).then((projects) => {
|
||||
expect(projects).to.deep.eq(['bar', 'foo'])
|
||||
})
|
||||
it('inserts project at the start', async () => {
|
||||
await cache.insertProject('foo')
|
||||
await cache.insertProject('bar')
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
expect(projects).to.deep.eq(['bar', 'foo'])
|
||||
})
|
||||
|
||||
it('can insert multiple projects in a row', () => {
|
||||
return Promise.all([
|
||||
cache.insertProject('baz'),
|
||||
cache.insertProject('bar'),
|
||||
cache.insertProject('foo'),
|
||||
])
|
||||
.then(() => {
|
||||
return cache.__get('PROJECTS')
|
||||
}).then((projects) => {
|
||||
expect(projects).to.deep.eq(['foo', 'bar', 'baz'])
|
||||
})
|
||||
it('can insert multiple projects in a row', async () => {
|
||||
await cache.insertProject('baz')
|
||||
await cache.insertProject('bar')
|
||||
await cache.insertProject('foo')
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
expect(projects).to.deep.eq(['foo', 'bar', 'baz'])
|
||||
})
|
||||
|
||||
it('moves project to start if it already exists', () => {
|
||||
return Promise.all([
|
||||
cache.insertProject('foo'),
|
||||
cache.insertProject('bar'),
|
||||
cache.insertProject('baz'),
|
||||
])
|
||||
.then(() => {
|
||||
return cache.insertProject('bar')
|
||||
}).then(() => {
|
||||
return cache.__get('PROJECTS')
|
||||
}).then((projects) => {
|
||||
expect(projects).to.deep.eq(['bar', 'baz', 'foo'])
|
||||
})
|
||||
it('moves project to start if it already exists', async () => {
|
||||
await cache.insertProject('foo')
|
||||
await cache.insertProject('bar')
|
||||
await cache.insertProject('baz')
|
||||
await cache.insertProject('bar')
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
expect(projects).to.deep.eq(['bar', 'baz', 'foo'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#removeProject', () => {
|
||||
it('removes project by path', () => {
|
||||
return cache.insertProject('/Users/brian/app')
|
||||
.then(() => {
|
||||
return cache.removeProject('/Users/brian/app')
|
||||
}).then(() => {
|
||||
return cache.__get('PROJECTS').then((projects) => {
|
||||
expect(projects).to.deep.eq([])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
it('removes project by path', async () => {
|
||||
await cache.insertProject('/Users/brian/app')
|
||||
await cache.removeProject('/Users/brian/app')
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
describe('#getProjectRoots', () => {
|
||||
beforeEach(function () {
|
||||
this.statAsync = sinon.stub(fs, 'statAsync')
|
||||
})
|
||||
|
||||
it('returns an array of paths', function () {
|
||||
this.statAsync.withArgs('/Users/brian/app').resolves()
|
||||
this.statAsync.withArgs('/Users/sam/app2').resolves()
|
||||
|
||||
return cache.insertProject('/Users/brian/app')
|
||||
.then(() => {
|
||||
return cache.insertProject('/Users/sam/app2')
|
||||
}).then(() => {
|
||||
return cache.getProjectRoots().then((paths) => {
|
||||
expect(paths).to.deep.eq(['/Users/sam/app2', '/Users/brian/app'])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('removes any paths which no longer exist on the filesystem', function () {
|
||||
this.statAsync.withArgs('/Users/brian/app').resolves()
|
||||
this.statAsync.withArgs('/Users/sam/app2').rejects(new Error())
|
||||
|
||||
return cache.insertProject('/Users/brian/app')
|
||||
.then(() => {
|
||||
return cache.insertProject('/Users/sam/app2')
|
||||
}).then(() => {
|
||||
return cache.getProjectRoots().then((paths) => {
|
||||
expect(paths).to.deep.eq(['/Users/brian/app'])
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
// we have to wait on the write event because
|
||||
// of process.nextTick
|
||||
return Promise.delay(100).then(() => {
|
||||
return cache.__get('PROJECTS').then((projects) => {
|
||||
expect(projects).to.deep.eq(['/Users/brian/app'])
|
||||
})
|
||||
})
|
||||
})
|
||||
expect(projects).to.deep.eq([])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('project preferences', () => {
|
||||
it('should insert a projects preferences into the cache', () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
return cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
.then(() => cache.__get('PROJECT_PREFERENCES'))
|
||||
.then((preferences) => {
|
||||
expect(preferences[testProjectTitle]).to.deep.equal(testPreferences)
|
||||
})
|
||||
})
|
||||
|
||||
it('should insert multiple projects preferences into the cache', () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
const anotherTestProjectTitle = 'launchpad'
|
||||
const anotherTestPreferene = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
return cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
.then(() => cache.insertProjectPreferences(anotherTestProjectTitle, anotherTestPreferene))
|
||||
.then(() => cache.__get('PROJECT_PREFERENCES'))
|
||||
.then((preferences) => {
|
||||
expect(preferences).to.have.property(testProjectTitle)
|
||||
expect(preferences).to.have.property(anotherTestProjectTitle)
|
||||
})
|
||||
})
|
||||
|
||||
it('should clear the projects preferred preferences', async () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
return cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
.then(() => cache.removeProjectPreferences(testProjectTitle))
|
||||
.then(() => __get('PROJECT_PREFERENCES'))
|
||||
.then((preferences) => {
|
||||
expect(preferences[testProjectTitle]).to.equal(null)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('#setUser / #getUser', () => {
|
||||
describe('#getProjectRoots', () => {
|
||||
beforeEach(function () {
|
||||
this.user = {
|
||||
id: 1,
|
||||
name: 'brian',
|
||||
email: 'a@b.com',
|
||||
authToken: '1111-2222-3333-4444',
|
||||
}
|
||||
this.statAsync = sinon.stub(fs, 'statAsync')
|
||||
})
|
||||
|
||||
it('sets and gets user', function () {
|
||||
return cache.setUser(this.user).then(() => {
|
||||
return cache.getUser().then((user) => {
|
||||
expect(user).to.deep.eq(this.user)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('#removeUser', () => {
|
||||
it('sets user to empty object', function () {
|
||||
return cache.setUser(this.user).then(() => {
|
||||
return cache.removeUser().then(() => {
|
||||
return cache.getUser().then((user) => {
|
||||
expect(user).to.deep.eq({})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('queues public methods', () => {
|
||||
it('is able to write both values', () => {
|
||||
return Promise.all([
|
||||
cache.setUser({ name: 'brian', authToken: 'auth-token-123' }),
|
||||
cache.insertProject('foo'),
|
||||
])
|
||||
.then(() => {
|
||||
return cache.read()
|
||||
}).then((json) => {
|
||||
expect(json).to.deep.eq({
|
||||
USER: {
|
||||
name: 'brian',
|
||||
authToken: 'auth-token-123',
|
||||
},
|
||||
PROJECTS: ['foo'],
|
||||
PROJECT_PREFERENCES: {},
|
||||
PROJECTS_CONFIG: {},
|
||||
COHORTS: {},
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('cohorts', () => {
|
||||
it('should get no cohorts when empty', () => {
|
||||
return cache.getCohorts().then((cohorts) => {
|
||||
expect(cohorts).to.deep.eq({})
|
||||
})
|
||||
afterEach(function () {
|
||||
this.statAsync.restore()
|
||||
})
|
||||
|
||||
it('should insert a cohort', () => {
|
||||
const cohort = {
|
||||
name: 'cohort_id',
|
||||
cohort: 'A',
|
||||
}
|
||||
it('returns an array of paths', async function () {
|
||||
this.statAsync.withArgs('/Users/brian/app').resolves()
|
||||
this.statAsync.withArgs('/Users/sam/app2').resolves()
|
||||
|
||||
return cache.insertCohort(cohort).then(() => {
|
||||
return cache.getCohorts().then((cohorts) => {
|
||||
expect(cohorts).to.deep.eq({ [cohort.name]: cohort })
|
||||
})
|
||||
})
|
||||
await cache.insertProject('/Users/brian/app')
|
||||
await cache.insertProject('/Users/sam/app2')
|
||||
const paths = await cache.getProjectRoots()
|
||||
|
||||
expect(paths).to.deep.eq(['/Users/sam/app2', '/Users/brian/app'])
|
||||
})
|
||||
|
||||
it('removes any paths which no longer exist on the filesystem', async function () {
|
||||
this.statAsync.withArgs('/Users/brian/app').resolves()
|
||||
this.statAsync.withArgs('/Users/sam/app2').rejects(new Error())
|
||||
|
||||
await cache.insertProject('/Users/brian/app')
|
||||
await cache.insertProject('/Users/sam/app2')
|
||||
const paths = await cache.getProjectRoots()
|
||||
|
||||
expect(paths).to.deep.eq(['/Users/brian/app'])
|
||||
// we have to wait on the write event because
|
||||
// of process.nextTick
|
||||
await Promise.delay(100)
|
||||
const projects = await cache.__get('PROJECTS')
|
||||
|
||||
expect(projects).to.deep.eq(['/Users/brian/app'])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('project preferences', () => {
|
||||
it('should insert a projects preferences into the cache', async () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
const preferences = await cache.__get('PROJECT_PREFERENCES')
|
||||
|
||||
expect(preferences[testProjectTitle]).to.deep.equal(testPreferences)
|
||||
})
|
||||
|
||||
it('should insert multiple projects preferences into the cache', async () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
const anotherTestProjectTitle = 'launchpad'
|
||||
const anotherTestPreferene = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
await cache.insertProjectPreferences(anotherTestProjectTitle, anotherTestPreferene)
|
||||
const preferences = await cache.__get('PROJECT_PREFERENCES')
|
||||
|
||||
expect(preferences).to.have.property(testProjectTitle)
|
||||
expect(preferences).to.have.property(anotherTestProjectTitle)
|
||||
})
|
||||
|
||||
it('should clear the projects preferred preferences', async () => {
|
||||
const testProjectTitle = 'launchpad'
|
||||
const testPreferences = { testingType: 'e2e', browserPath: '/some/test/path' }
|
||||
|
||||
await cache.insertProjectPreferences(testProjectTitle, testPreferences)
|
||||
await cache.removeProjectPreferences(testProjectTitle)
|
||||
const preferences = await cache.__get('PROJECT_PREFERENCES')
|
||||
|
||||
expect(preferences[testProjectTitle]).to.equal(null)
|
||||
})
|
||||
})
|
||||
|
||||
context('#setUser / #getUser', () => {
|
||||
beforeEach(function () {
|
||||
this.user = {
|
||||
id: 1,
|
||||
name: 'brian',
|
||||
email: 'a@b.com',
|
||||
authToken: '1111-2222-3333-4444',
|
||||
}
|
||||
})
|
||||
|
||||
it('sets and gets user', async function () {
|
||||
await cache.setUser(this.user)
|
||||
const user = await cache.getUser()
|
||||
|
||||
expect(user).to.deep.eq(this.user)
|
||||
})
|
||||
})
|
||||
|
||||
context('#removeUser', () => {
|
||||
it('sets user to empty object', async function () {
|
||||
await cache.setUser(this.user)
|
||||
await cache.removeUser()
|
||||
const user = await cache.getUser()
|
||||
|
||||
expect(user).to.deep.eq({})
|
||||
})
|
||||
})
|
||||
|
||||
context('queues public methods', () => {
|
||||
it('is able to write both values', async function () {
|
||||
await Promise.all([
|
||||
cache.setUser({ name: 'brian', authToken: 'auth-token-123' }),
|
||||
cache.insertProject('foo'),
|
||||
])
|
||||
|
||||
const json = await cache._read()
|
||||
|
||||
expect(json).to.deep.eq({
|
||||
USER: {
|
||||
name: 'brian',
|
||||
authToken: 'auth-token-123',
|
||||
},
|
||||
PROJECTS: ['foo'],
|
||||
PROJECT_PREFERENCES: {},
|
||||
PROJECTS_CONFIG: {},
|
||||
COHORTS: {},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('cohorts', () => {
|
||||
it('should get no cohorts when empty', async function () {
|
||||
const cohorts = await cache.getCohorts()
|
||||
|
||||
expect(cohorts).to.deep.eq({})
|
||||
})
|
||||
|
||||
it('should insert a cohort', async function () {
|
||||
const cohort = {
|
||||
name: 'cohort_id',
|
||||
cohort: 'A',
|
||||
}
|
||||
|
||||
await cache.insertCohort(cohort)
|
||||
const cohorts = await cache.getCohorts()
|
||||
|
||||
expect(cohorts).to.deep.eq({ [cohort.name]: cohort })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,7 @@ const {
|
||||
} = require('@packages/network')
|
||||
const pkg = require('@packages/root')
|
||||
const api = require('../../../../lib/cloud/api').default
|
||||
const cache = require('../../../../lib/cache')
|
||||
const cache = require('../../../../lib/cache').cache
|
||||
const errors = require('../../../../lib/errors')
|
||||
const machineId = require('../../../../lib/cloud/machine_id')
|
||||
const Promise = require('bluebird')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('../../spec_helper')
|
||||
|
||||
const api = require('../../../lib/cloud/api').default
|
||||
const cache = require('../../../lib/cache')
|
||||
const cache = require('../../../lib/cache').cache
|
||||
const user = require('../../../lib/cloud/user')
|
||||
|
||||
describe('lib/cloud/user', () => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('../spec_helper')
|
||||
|
||||
import { Cohort } from '@packages/types'
|
||||
import cache from '../../lib/cache'
|
||||
import { cache } from '../../lib/cache'
|
||||
import cohorts from '../../lib/cohorts'
|
||||
|
||||
describe('lib/cohort', () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ export interface Cache {
|
||||
PROJECT_PREFERENCES: Record<string, Preferences>
|
||||
USER: CachedUser
|
||||
COHORTS: Record<string, Cohort>
|
||||
PROJECTS_CONFIG: Record<string, any>
|
||||
}
|
||||
|
||||
import type { AllowedState } from './preferences'
|
||||
|
||||
@@ -12,7 +12,7 @@ global.sinon = require('sinon')
|
||||
const _ = require('lodash')
|
||||
const Promise = require('bluebird')
|
||||
const path = require('path')
|
||||
const cache = require(`@packages/server/lib/cache`)
|
||||
const cache = require('@packages/server/lib/cache').cache
|
||||
|
||||
require('chai')
|
||||
.use(require('@cypress/sinon-chai'))
|
||||
@@ -96,7 +96,7 @@ before(function () {
|
||||
|
||||
// appData.ensure()
|
||||
|
||||
beforeEach(function () {
|
||||
beforeEach(async function () {
|
||||
this.originalEnv = originalEnv
|
||||
|
||||
nock.disableNetConnect()
|
||||
@@ -104,7 +104,7 @@ beforeEach(function () {
|
||||
|
||||
// always clean up the cache
|
||||
// before each test
|
||||
return cache.remove()
|
||||
await cache.remove()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
Reference in New Issue
Block a user