Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
61a0dd309This file is pinned to the selected release. Download the original to open it in your design tool.
import assert from 'node:assert/strict'
import test from 'node:test'
import { AppController } from '../src/AppController.mjs'
import { AppState } from '../src/core/AppState.mjs'
/**
* View double that exposes the GitHub URL callback as a user action.
*/
class GithubOpenView {
/** @type {((url: string) => void | Promise<void>) | null} */
#githubOpenCallback
/** @type {((documentId: string) => void) | null} */
#documentSelectionCallback
constructor() {
this.#githubOpenCallback = null
this.#documentSelectionCallback = null
}
/** @param {() => void} _callback @returns {void} */
bindLocalOpen(_callback) {}
/** @param {() => void} _callback @returns {void} */
bindPcbStylerClick(_callback) {}
/** @param {(files: File[]) => void | Promise<void>} _callback @returns {void} */
bindFileSelection(_callback) {}
/** @param {(files: File[]) => void | Promise<void>} _callback @returns {void} */
bindDrop(_callback) {}
/** @param {(viewName: string) => void} _callback @returns {void} */
bindViewChange(_callback) {}
/** @param {(documentId: string) => void} callback @returns {void} */
bindDocumentSelection(callback) {
this.#documentSelectionCallback = callback
}
/** @param {(demoId: string) => void | Promise<void>} _callback @returns {void} */
bindDemoSelection(_callback) {}
/** @param {(url: string) => void | Promise<void>} callback @returns {void} */
bindGitHubOpen(callback) {
this.#githubOpenCallback = callback
}
/** @param {(locale: string) => void | Promise<void>} _callback @returns {void} */
bindLocaleChange(_callback) {}
/** @returns {boolean} */
hasLocaleSelect() {
return false
}
/** @param {string} _status @returns {void} */
setStatus(_status) {}
/** @param {string} _version @returns {void} */
setVersion(_version) {}
/** @param {any} _snapshot @returns {void} */
render(_snapshot) {}
/** @param {string} _url @param {string} _mode @returns {void} */
setPcbStylerLink(_url, _mode) {}
/**
* Simulates submitting the GitHub URL form.
* @param {string} url GitHub source URL.
* @returns {Promise<void>}
*/
async openGitHub(url) {
await this.#githubOpenCallback?.(url)
}
/**
* Simulates selecting a parsed document from the document rail.
* @param {string} documentId Session document id.
* @returns {void}
*/
selectDocument(documentId) {
this.#documentSelectionCallback?.(documentId)
}
}
/**
* View double that stores the latest rendered controller state.
*/
class StartupView extends GithubOpenView {
/** @type {any | null} */
snapshot
constructor() {
super()
this.snapshot = null
}
/** @param {any} snapshot @returns {void} */
render(snapshot) {
this.snapshot = snapshot
}
}
/**
* Parser double that returns one PCB document for any GitHub batch.
*/
class GithubBatchParser {
/** @type {object[]} */
#documents
/**
* @param {object[]} [documents] Documents to return.
*/
constructor(documents = [createPcbDocument('board.kicad_pcb')]) {
this.#documents = documents
}
/** @param {{ name: string, buffer: ArrayBuffer }[]} _entries @returns {object} */
parseEntries(_entries) {
return {
documents: this.#documents,
assets: []
}
}
}
/**
* Builds a minimal schematic document model.
* @param {string} fileName Source file name.
* @returns {object}
*/
function createSchematicDocument(fileName) {
return {
fileName,
kind: 'schematic',
diagnostics: [],
schematic: {
sheet: { width: 100, height: 100 },
lines: []
},
bom: []
}
}
/**
* Builds a minimal PCB document model.
* @param {string} fileName Source file name.
* @returns {object}
*/
function createPcbDocument(fileName) {
return {
fileName,
kind: 'pcb',
diagnostics: [],
summary: {
title: fileName,
componentCount: 0,
layerCount: 2,
outlineSegmentCount: 0,
boardWidthMil: 100,
boardHeightMil: 100
},
pcb: {
boardOutline: {
minX: 0,
minY: 0,
widthMil: 100,
heightMil: 100,
segments: []
},
layers: [],
components: []
},
bom: []
}
}
/**
* Analytics double that ignores tracked events.
*/
class NoopAnalytics {
/** @param {string} _name @param {object} [_properties] @returns {void} */
track(_name, _properties = {}) {}
}
test('AppController writes successful GitHub form loads into the share URL', async () => {
const view = new GithubOpenView()
const replaceCalls = []
const originalHistory = globalThis.history
const originalLocation = globalThis.location
const sourceUrl = 'https://github.com/acme/demo/tree/main/hardware/project'
try {
Object.defineProperty(globalThis, 'history', {
configurable: true,
value: {
state: { active: true },
replaceState(...args) {
replaceCalls.push(args)
}
}
})
Object.defineProperty(globalThis, 'location', {
configurable: true,
value: {
href: 'https://ecadforge.app/demo/kicad?demo=altium&github=old/ref&ref=dev&v=1.4.26'
}
})
const controller = new AppController({
state: new AppState(),
view,
parser: new GithubBatchParser(),
analytics: new NoopAnalytics(),
githubSourceLoader: {
async loadUrl(_url) {
return {
sourceType: 'github',
formatFamily: 'kicad',
rawUrl: 'https://raw.githubusercontent.com/acme/demo/main/hardware/project/board.kicad_pcb',
boardUrl:
'https://raw.githubusercontent.com/acme/demo/main/hardware/project/board.kicad_pcb',
entries: [
{
name: 'board.kicad_pcb',
buffer: new ArrayBuffer(4)
}
],
assets: [],
modelReferences: []
}
}
}
})
await controller.init()
await view.openGitHub(sourceUrl)
assert.deepEqual(replaceCalls, [
[
{ active: true },
'',
'https://ecadforge.app/?v=1.4.26&url=https%3A%2F%2Fgithub.com%2Facme%2Fdemo%2Ftree%2Fmain%2Fhardware%2Fproject&document=board.kicad_pcb'
]
])
} finally {
if (originalHistory === undefined) {
delete globalThis.history
} else {
Object.defineProperty(globalThis, 'history', {
configurable: true,
value: originalHistory
})
}
if (originalLocation === undefined) {
delete globalThis.location
} else {
Object.defineProperty(globalThis, 'location', {
configurable: true,
value: originalLocation
})
}
}
})
test('AppController writes selected GitHub documents into the share URL', async () => {
const view = new GithubOpenView()
const state = new AppState()
const replaceCalls = []
const originalHistory = globalThis.history
const originalLocation = globalThis.location
const sourceUrl = 'https://github.com/acme/demo/tree/main/hardware'
try {
Object.defineProperty(globalThis, 'history', {
configurable: true,
value: {
state: { active: true },
replaceState(...args) {
replaceCalls.push(args)
}
}
})
Object.defineProperty(globalThis, 'location', {
configurable: true,
value: {
href: 'https://ecadforge.app/?url=https%3A%2F%2Fgithub.com%2Facme%2Fdemo%2Ftree%2Fmain%2Fhardware&view=schematic'
}
})
const controller = new AppController({
state,
view,
parser: new GithubBatchParser([
createSchematicDocument('Schematics/01_Input.SchDoc'),
createSchematicDocument('Schematics/08_Output.SchDoc')
]),
analytics: new NoopAnalytics(),
githubSourceLoader: {
async loadUrl(_url) {
return {
sourceType: 'github',
formatFamily: 'altium',
shareUrl: sourceUrl,
entries: [
{
name: 'Project.PrjPcb',
buffer: new ArrayBuffer(4)
}
],
assets: [],
modelReferences: []
}
}
}
})
await controller.init()
await view.openGitHub(sourceUrl)
view.selectDocument(state.getSnapshot().documents[1].id)
assert.equal(
replaceCalls.at(-1)[2],
'https://ecadforge.app/?url=https%3A%2F%2Fgithub.com%2Facme%2Fdemo%2Ftree%2Fmain%2Fhardware&view=schematic&document=Schematics%2F08_Output.SchDoc'
)
} finally {
if (originalHistory === undefined) {
delete globalThis.history
} else {
Object.defineProperty(globalThis, 'history', {
configurable: true,
value: originalHistory
})
}
if (originalLocation === undefined) {
delete globalThis.location
} else {
Object.defineProperty(globalThis, 'location', {
configurable: true,
value: originalLocation
})
}
}
})
test('AppController restores a startup GitHub source view from deep links', async () => {
const view = new StartupView()
const state = new AppState({ activeView: 'schematic' })
const controller = new AppController({
state,
view,
parser: new GithubBatchParser(),
analytics: new NoopAnalytics(),
startupSource: {
type: 'url',
url: 'https://github.com/acme/demo/tree/main/hardware',
view: '3d'
},
githubSourceLoader: {
async loadUrl(_url) {
return {
sourceType: 'github',
formatFamily: 'kicad',
shareUrl: 'https://github.com/acme/demo/tree/main/hardware',
entries: [
{
name: 'board.kicad_pcb',
buffer: new ArrayBuffer(4)
}
],
assets: [],
modelReferences: []
}
}
}
})
await controller.init()
assert.equal(view.snapshot?.activeView, '3d')
assert.equal(state.getSnapshot().activeView, '3d')
})
test('AppController restores GitHub startup PCB view on the first compatible project board', async () => {
const view = new StartupView()
const state = new AppState()
const controller = new AppController({
state,
view,
parser: new GithubBatchParser([
createSchematicDocument('Schematics/Main.SchDoc'),
createPcbDocument('PCB/Primary.PcbDoc'),
createPcbDocument('PCB/Primary_panel.PcbDoc')
]),
analytics: new NoopAnalytics(),
startupSource: {
type: 'url',
url: 'https://github.com/acme/demo/tree/main/hardware',
view: 'pcb'
},
githubSourceLoader: {
async loadUrl(_url) {
return {
sourceType: 'github',
formatFamily: 'altium',
shareUrl: 'https://github.com/acme/demo/tree/main/hardware',
entries: [
{
name: 'Project.PrjPcb',
buffer: new ArrayBuffer(4)
}
],
assets: [],
modelReferences: []
}
}
}
})
await controller.init()
assert.equal(state.getSnapshot().activeView, 'pcb')
assert.equal(state.getSnapshot().activeFileName, 'PCB/Primary.PcbDoc')
})
test('AppController restores a startup GitHub document from deep links', async () => {
const view = new StartupView()
const state = new AppState()
const controller = new AppController({
state,
view,
parser: new GithubBatchParser([
createSchematicDocument('Schematics/01_Input.SchDoc'),
createSchematicDocument('Schematics/08_Output.SchDoc'),
createPcbDocument('PCB/Primary.PcbDoc')
]),
analytics: new NoopAnalytics(),
startupSource: {
type: 'url',
url: 'https://github.com/acme/demo/tree/main/hardware',
view: 'schematic',
document: 'Schematics/08_Output.SchDoc'
},
githubSourceLoader: {
async loadUrl(_url) {
return {
sourceType: 'github',
formatFamily: 'altium',
shareUrl: 'https://github.com/acme/demo/tree/main/hardware',
entries: [
{
name: 'Project.PrjPcb',
buffer: new ArrayBuffer(4)
}
],
assets: [],
modelReferences: []
}
}
}
})
await controller.init()
assert.equal(state.getSnapshot().activeView, 'schematic')
assert.equal(
state.getSnapshot().activeFileName,
'Schematics/08_Output.SchDoc'
)
})