From 4a2e59eea85c09ac6e52a3e84d1c15c70491462d Mon Sep 17 00:00:00 2001 From: Chris Vickery Date: Thu, 10 Oct 2019 16:22:07 -0700 Subject: [PATCH] Add init script, fix minor issues in tests --- src/__tests__/can/dbc.parse.test.js | 3 +- src/__tests__/can/dbc.write.test.js | 4 + src/__tests__/utils/dbc.test.js | 3 +- src/init.js | 129 ++++++++++++++++++++++++++++ src/init.test.js | 8 ++ 5 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 src/init.js create mode 100644 src/init.test.js diff --git a/src/__tests__/can/dbc.parse.test.js b/src/__tests__/can/dbc.parse.test.js index 96ad2f5..1e7fb31 100644 --- a/src/__tests__/can/dbc.parse.test.js +++ b/src/__tests__/can/dbc.parse.test.js @@ -1,8 +1,7 @@ +/* eslint-env jest */ import DBC, { swapOrder } from '../../models/can/dbc'; import Signal from '../../models/can/signal'; -global.__JEST__ = 1; - const DBC_MESSAGE_DEF = `BO_ 228 STEERING_CONTROL: 5 ADAS SG_ STEER_TORQUE : 7|16@0- (1,0) [-3840|3840] "" EPS SG_ STEER_TORQUE_REQUEST : 23|1@0+ (1,0) [0|1] "" EPS diff --git a/src/__tests__/can/dbc.write.test.js b/src/__tests__/can/dbc.write.test.js index c4b9c80..972e3cc 100644 --- a/src/__tests__/can/dbc.write.test.js +++ b/src/__tests__/can/dbc.write.test.js @@ -1,3 +1,7 @@ +/** + * @jest-environment jsdom + */ +/* eslint-env jest */ import fs from 'fs'; import path from 'path'; import DBC, { swapOrder } from '../../models/can/dbc'; diff --git a/src/__tests__/utils/dbc.test.js b/src/__tests__/utils/dbc.test.js index f2eac70..1832784 100644 --- a/src/__tests__/utils/dbc.test.js +++ b/src/__tests__/utils/dbc.test.js @@ -1,10 +1,9 @@ +/* eslint-env jest */ import extend from 'xtend'; import DbcUtils from '../../utils/dbc'; import DBC from '../../models/can/dbc'; import Signal from '../../models/can/signal'; -global.__JEST__ = 1; - // want to mock pandareader and test processStreamedCanMessages const SAMPLE_MESSAGE = { address: 0x10, diff --git a/src/init.js b/src/init.js new file mode 100644 index 0000000..c7ce10d --- /dev/null +++ b/src/init.js @@ -0,0 +1,129 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import CommaAuth from '@commaai/my-comma-auth'; +import { request as Request } from '@commaai/comma-api'; +import Sentry from './logging/Sentry'; +import CanExplorer from './CanExplorer'; +import AcuraDbc from './acura-dbc'; +import { getUrlParameter, modifyQueryParameters } from './utils/url'; +import { GITHUB_AUTH_TOKEN_KEY } from './config'; +import { + fetchPersistedDbc, + fetchPersistedGithubAuthToken, + persistGithubAuthToken +} from './api/localstorage'; +import './index.css'; + +export default function init() { + Sentry.init(); + + const routeFullName = getUrlParameter('route'); + const isDemo = !routeFullName; + let segments = getUrlParameter('segments'); + if (segments && segments.length) { + segments = segments.split(',').map(Number); + + if (segments.length !== 2) { + segments = undefined; + } + } + + const props = { + autoplay: true, + startTime: Number(getUrlParameter('seekTime') || 0), + segments, + isDemo + }; + let persistedDbc = null; + + if (routeFullName) { + const [dongleId, route] = routeFullName.split('|'); + props.dongleId = dongleId; + props.name = route; + + persistedDbc = fetchPersistedDbc(routeFullName); + + const max = getUrlParameter('max'); + const url = getUrlParameter('url'); + const exp = getUrlParameter('exp'); + const sig = getUrlParameter('sig'); + + if (max) { + props.max = max; + } + if (url) { + props.url = url; + } + if (exp) { + props.exp = exp; + } + if (sig) { + props.sig = sig; + } + props.isLegacyShare = max && url && !exp && !sig; + props.isShare = max && url && exp && sig; + } else if (getUrlParameter('demo')) { + props.max = 12; + props.url = 'https://chffrprivate.blob.core.windows.net/chffrprivate3-permanent/v2/cb38263377b873ee/78392b99580c5920227cc5b43dff8a70_2017-06-12--18-51-47'; + props.name = '2017-06-12--18-51-47'; + props.dongleId = 'cb38263377b873ee'; + props.dbc = AcuraDbc; + props.isDemo = true; + props.dbcFilename = 'acura_ilx_2016_can.dbc'; + + // lots of 404s on this one + // props.max = 752; + // props.url = 'https://chffrprivate.blob.core.windows.net/chffrprivate3/v2/07e243287e48432a/d97fcc321a58e660a14de72b749269ba_2017-09-09--22-00-00'; + // props.name = '2017-09-09--22-00-00'; + // props.dongleId = '07e243287e48432a'; + // props.dbc = AcuraDbc; + // props.dbcFilename = 'acura_ilx_2016_can.dbc'; + + // really long one with real content + // props.max = 597; + // props.url = 'https://chffrprivate.blob.core.windows.net/chffrprivate3/v2/0c249898b339e978/957935e6a75bc2bf6f626fcbe6db93ba_2017-08-11--04-47-54'; + // props.name = '2017-08-11--04-47-54'; + // props.dongleId = '0c249898b339e978'; + // props.dbc = AcuraDbc; + // props.dbcFilename = 'acura_ilx_2016_can.dbc'; + } + + if (persistedDbc) { + const { dbcFilename, dbc } = persistedDbc; + props.dbc = dbc; + props.dbcFilename = dbcFilename; + } + + const authTokenQueryParam = getUrlParameter(GITHUB_AUTH_TOKEN_KEY); + if (authTokenQueryParam !== null) { + props.githubAuthToken = authTokenQueryParam; + persistGithubAuthToken(authTokenQueryParam); + const urlNoAuthToken = modifyQueryParameters({ + remove: [GITHUB_AUTH_TOKEN_KEY] + }); + window.location.href = urlNoAuthToken; + } else { + props.githubAuthToken = fetchPersistedGithubAuthToken(); + } + + async function renderDom() { + const token = await CommaAuth.init(); + if (token) { + Request.configure(token); + } + ReactDOM.render(, document.getElementById('root')); // eslint-disable-line react/jsx-props-no-spreading + } + + if (routeFullName || isDemo) { + renderDom(); + return; + } + + const img = document.createElement('img'); + img.src = `${process.env.PUBLIC_URL}/img/cabana.jpg`; + img.style.width = '100%'; + const comment = document.createComment('7/6/17'); + + document.getElementById('root').appendChild(img); + document.getElementById('root').appendChild(comment); +} diff --git a/src/init.test.js b/src/init.test.js new file mode 100644 index 0000000..41594b4 --- /dev/null +++ b/src/init.test.js @@ -0,0 +1,8 @@ +/* eslint-env jest */ +import init from './init'; + +describe('init', () => { + it('works without url params', () => { + init(); + }); +});