110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import { InvalidRequestError } from '#lib/server/sync/core/errors.ts';
|
|
import type { SourceRoot, TargetRoot } from '#lib/server/sync/core/model.ts';
|
|
import type {
|
|
ResolvedRoot,
|
|
SourceRootResolver,
|
|
TargetRootResolver
|
|
} from '#lib/server/sync/core/ports.ts';
|
|
import { FakeAcc } from './fake-acc.ts';
|
|
import { FakeDrive } from './fake-drive.ts';
|
|
|
|
/** A file the simulated ACC always rejects, so the issues list has something in it. */
|
|
export const REJECTED_FILE = 'Corrupt model.rvt';
|
|
|
|
const SAMPLE_TREE: Record<string, string[]> = {
|
|
Drawings: ['A-101 Floor Plan.pdf', 'A-201 Elevations.pdf', 'S-101 Foundations.dwg'],
|
|
Specifications: ['Division 08 - Openings.docx', 'Division 26 - Electrical.docx'],
|
|
'Submittals/Approved': ['Door hardware.pdf'],
|
|
'': ['Project Directory.xlsx', REJECTED_FILE, '~$Project Directory.xlsx']
|
|
};
|
|
|
|
/**
|
|
* An in-memory SharePoint library and ACC project for trying the app without
|
|
* credentials. Any pasted path resolves to a folder named after its last
|
|
* segment; new SharePoint folders come pre-filled with sample files.
|
|
*/
|
|
export class SimulatedBackend implements SourceRootResolver, TargetRootResolver {
|
|
readonly drive = new FakeDrive();
|
|
readonly acc = new FakeAcc();
|
|
private readonly sourceFolders = new Map<string, string>();
|
|
private readonly targetFolders = new Map<string, string>();
|
|
|
|
constructor(private readonly random: () => number = Math.random) {
|
|
this.acc.failOn(REJECTED_FILE, new Error('ACC rejected the upload (simulated failure).'));
|
|
}
|
|
|
|
async resolveSourceRoot(input: string): Promise<ResolvedRoot<SourceRoot>> {
|
|
const name = lastSegment(input);
|
|
let id = this.sourceFolders.get(name);
|
|
if (!id) {
|
|
id = this.drive.addFolder(this.drive.driveRootId, name);
|
|
this.seed(id);
|
|
this.sourceFolders.set(name, id);
|
|
}
|
|
return {
|
|
root: { siteId: 'simulated-site', driveId: 'simulated-drive', rootItemId: id },
|
|
displayPath: `Simulated SharePoint / ${name}`
|
|
};
|
|
}
|
|
|
|
async resolveTargetRoot(input: string): Promise<ResolvedRoot<TargetRoot>> {
|
|
const name = lastSegment(input);
|
|
let id = this.targetFolders.get(name);
|
|
if (!id) {
|
|
id = this.acc.addFolderByHand(this.acc.rootFolderId, name);
|
|
this.targetFolders.set(name, id);
|
|
}
|
|
return {
|
|
root: { hubId: 'simulated-hub', projectId: 'simulated-project', rootFolderId: id },
|
|
displayPath: `Simulated ACC / Project Files / ${name}`
|
|
};
|
|
}
|
|
|
|
/** Makes one random change (edit, add or rename) somewhere under a synced root. */
|
|
simulateActivity(): void {
|
|
const roots = [...this.sourceFolders.values()];
|
|
if (roots.length === 0) return;
|
|
const root = this.pick(roots);
|
|
const files = this.drive
|
|
.filesUnder(root)
|
|
.filter((id) => this.drive.nameOf(id) !== REJECTED_FILE);
|
|
const roll = this.random();
|
|
|
|
if (files.length === 0 || roll < 0.25) {
|
|
this.drive.addFile(root, `Field report ${Math.floor(this.random() * 1000)}.pdf`);
|
|
} else if (roll < 0.85) {
|
|
const file = this.pick(files);
|
|
this.drive.edit(file, `${this.drive.nameOf(file)} edited ${new Date().toISOString()}`);
|
|
} else {
|
|
const file = this.pick(files);
|
|
this.drive.rename(file, `Rev ${Math.floor(this.random() * 10)} - ${this.drive.nameOf(file)}`);
|
|
}
|
|
}
|
|
|
|
private seed(rootId: string): void {
|
|
for (const [folderPath, files] of Object.entries(SAMPLE_TREE)) {
|
|
let parent = rootId;
|
|
for (const segment of folderPath.split('/').filter(Boolean)) {
|
|
parent = this.drive.addFolder(parent, segment);
|
|
}
|
|
for (const file of files) this.drive.addFile(parent, file);
|
|
}
|
|
}
|
|
|
|
private pick<T>(values: T[]): T {
|
|
return values[Math.floor(this.random() * values.length)];
|
|
}
|
|
}
|
|
|
|
/** `https://x/sites/A/Shared%20Documents/Project%20X?web=1` → `Project X` */
|
|
function lastSegment(input: string): string {
|
|
const path = input.trim().split(/[?#]/)[0];
|
|
const name = path.split(/[/\\]/).filter(Boolean).at(-1);
|
|
if (!name) throw new InvalidRequestError('Enter a folder path or URL.');
|
|
try {
|
|
return decodeURIComponent(name);
|
|
} catch {
|
|
return name;
|
|
}
|
|
}
|