building out next phase of sync
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { ProjectSyncRunner } from './core/engine';
|
||||
import { emptyCounts, type Project, type RunTrigger } from './core/model';
|
||||
import type { ProjectRepository } from './core/ports';
|
||||
import { SyncCoordinator } from './coordinator';
|
||||
import { silentLogger } from './testing/harness';
|
||||
import type { ProjectSyncRunner } from './core/engine.ts';
|
||||
import { emptyCounts, type Project, type RunTrigger } from './core/model.ts';
|
||||
import type { ProjectRepository } from './core/ports.ts';
|
||||
import { SyncCoordinator } from './coordinator.ts';
|
||||
import { silentLogger } from './testing/harness.ts';
|
||||
|
||||
function project(id: string, overrides: Partial<Project> = {}): Project {
|
||||
return { id, enabled: true, pausedReason: null, ...overrides } as Project;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { ProjectSyncRunner } from './core/engine';
|
||||
import { errorMessage } from './core/errors';
|
||||
import type { RunTrigger } from './core/model';
|
||||
import type { Clock, Logger, ProjectRepository } from './core/ports';
|
||||
import type { ProjectSyncRunner } from './core/engine.ts';
|
||||
import { errorMessage } from './core/errors.ts';
|
||||
import type { RunTrigger } from './core/model.ts';
|
||||
import type { Clock, Logger, ProjectRepository } from './core/ports.ts';
|
||||
|
||||
export type RunRequestResult =
|
||||
| { status: 'started' }
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createHarness } from '../testing/harness';
|
||||
import { ServiceUnavailableError } from './errors';
|
||||
import { ROOT_DELETED } from './ingest';
|
||||
import { WAITING_FOR_PARENT } from './reconciler';
|
||||
import { createHarness } from '#lib/server/sync/testing/harness.ts';
|
||||
import { ServiceUnavailableError } from './errors.ts';
|
||||
import { ROOT_DELETED } from './ingest.ts';
|
||||
import { WAITING_FOR_PARENT } from './reconciler.ts';
|
||||
|
||||
describe('SyncEngine', () => {
|
||||
it('mirrors the initial tree and ignores temp files', async () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { errorMessage } from './errors';
|
||||
import { ingestChanges } from './ingest';
|
||||
import { errorMessage } from './errors.ts';
|
||||
import { ingestChanges } from './ingest.ts';
|
||||
import {
|
||||
emptyCounts,
|
||||
type ItemState,
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
type RunOutcome,
|
||||
type RunResult,
|
||||
type RunTrigger
|
||||
} from './model';
|
||||
} from './model.ts';
|
||||
import type {
|
||||
ChangeSource,
|
||||
Clock,
|
||||
@@ -16,8 +16,8 @@ import type {
|
||||
Logger,
|
||||
ProjectRepository,
|
||||
RunRepository
|
||||
} from './ports';
|
||||
import type { Reconciler } from './reconciler';
|
||||
} from './ports.ts';
|
||||
import type { Reconciler } from './reconciler.ts';
|
||||
|
||||
export interface ProjectSyncRunner {
|
||||
runProject(projectId: string, trigger: RunTrigger): Promise<RunResult>;
|
||||
|
||||
@@ -10,6 +10,14 @@ export class ServiceUnavailableError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/** A request that can't be carried out as asked; the message is safe to show to the user. */
|
||||
export class InvalidRequestError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'InvalidRequestError';
|
||||
}
|
||||
}
|
||||
|
||||
export function errorMessage(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { PatternIgnorePolicy } from './ignore';
|
||||
import { PatternIgnorePolicy } from './ignore.ts';
|
||||
|
||||
describe('PatternIgnorePolicy', () => {
|
||||
const policy = new PatternIgnorePolicy();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { IgnorePolicy } from './ports';
|
||||
import type { IgnorePolicy } from './ports.ts';
|
||||
|
||||
/** Office lock files, temp/partial downloads and OS metadata. */
|
||||
export const DEFAULT_IGNORE_PATTERNS = [
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { PatternIgnorePolicy } from './ignore';
|
||||
import { ingestChanges, ROOT_MISSING, type IngestInput } from './ingest';
|
||||
import type { ItemState, SourceChange, SourceItem } from './model';
|
||||
import { PatternIgnorePolicy } from './ignore.ts';
|
||||
import { ingestChanges, ROOT_MISSING, type IngestInput } from './ingest.ts';
|
||||
import type { ItemState, SourceChange, SourceItem } from './model.ts';
|
||||
|
||||
const ROOT = 'root';
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { DesiredState, ItemState, SourceChange, SourceItem } from './model';
|
||||
import { withStatus } from './plan';
|
||||
import type { IgnorePolicy } from './ports';
|
||||
import { ItemTree } from './tree';
|
||||
import type { DesiredState, ItemState, SourceChange, SourceItem } from './model.ts';
|
||||
import { withStatus } from './plan.ts';
|
||||
import type { IgnorePolicy } from './ports.ts';
|
||||
import { ItemTree } from './tree.ts';
|
||||
|
||||
export const ROOT_DELETED = 'The SharePoint root folder was deleted.';
|
||||
export const ROOT_MISSING = 'The SharePoint root folder was not found in a full listing.';
|
||||
|
||||
@@ -34,6 +34,13 @@ export type NewProject = Pick<
|
||||
'name' | 'source' | 'sourceDisplayPath' | 'target' | 'targetDisplayPath'
|
||||
>;
|
||||
|
||||
export type ProjectChanges = Partial<
|
||||
Pick<
|
||||
Project,
|
||||
'name' | 'enabled' | 'source' | 'sourceDisplayPath' | 'target' | 'targetDisplayPath'
|
||||
>
|
||||
>;
|
||||
|
||||
/** An item as SharePoint currently describes it. */
|
||||
export interface SourceItem {
|
||||
sourceId: string;
|
||||
@@ -80,7 +87,8 @@ export interface SyncedState {
|
||||
at: Date | null;
|
||||
}
|
||||
|
||||
export type ItemStatus = 'pending' | 'synced' | 'failed' | 'deleted';
|
||||
export const ITEM_STATUSES = ['pending', 'synced', 'failed', 'deleted'] as const;
|
||||
export type ItemStatus = (typeof ITEM_STATUSES)[number];
|
||||
|
||||
export interface ItemState {
|
||||
sourceId: string;
|
||||
@@ -114,8 +122,18 @@ export type EventAction = (typeof EVENT_ACTIONS)[number];
|
||||
|
||||
export type RunCounts = Record<EventAction, number>;
|
||||
|
||||
export type StatusCounts = Record<ItemStatus, number>;
|
||||
|
||||
function zeroed<K extends string>(keys: readonly K[]): Record<K, number> {
|
||||
return Object.fromEntries(keys.map((key) => [key, 0])) as Record<K, number>;
|
||||
}
|
||||
|
||||
export function emptyCounts(): RunCounts {
|
||||
return Object.fromEntries(EVENT_ACTIONS.map((a) => [a, 0])) as RunCounts;
|
||||
return zeroed(EVENT_ACTIONS);
|
||||
}
|
||||
|
||||
export function emptyStatusCounts(): StatusCounts {
|
||||
return zeroed(ITEM_STATUSES);
|
||||
}
|
||||
|
||||
export interface RunResult {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { DesiredState, ItemState, SyncedState } from './model';
|
||||
import { computeStatus, planAction } from './plan';
|
||||
import type { DesiredState, ItemState, SyncedState } from './model.ts';
|
||||
import { computeStatus, planAction } from './plan.ts';
|
||||
|
||||
const desired: DesiredState = {
|
||||
parentSourceId: 'p',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { ItemState, ItemStatus, SyncAction } from './model';
|
||||
import type { ItemTree } from './tree';
|
||||
import type { ItemState, ItemStatus, SyncAction } from './model.ts';
|
||||
import type { ItemTree } from './tree.ts';
|
||||
|
||||
/** The ACC operation needed to make the synced state match the desired state, if any. */
|
||||
export function planAction({ desired, synced }: ItemState): SyncAction | null {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type {
|
||||
ItemState,
|
||||
NewProject,
|
||||
Project,
|
||||
ProjectChanges,
|
||||
RunEvent,
|
||||
RunOutcome,
|
||||
RunCounts,
|
||||
@@ -10,8 +11,10 @@ import type {
|
||||
RunTrigger,
|
||||
SourceChange,
|
||||
SourceRoot,
|
||||
TargetRef
|
||||
} from './model';
|
||||
StatusCounts,
|
||||
TargetRef,
|
||||
TargetRoot
|
||||
} from './model.ts';
|
||||
|
||||
export interface ChangeBatch {
|
||||
changes: SourceChange[];
|
||||
@@ -52,10 +55,30 @@ export interface IgnorePolicy {
|
||||
isIgnored(name: string, isFolder: boolean): boolean;
|
||||
}
|
||||
|
||||
export interface ResolvedRoot<T> {
|
||||
root: T;
|
||||
displayPath: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn what an admin pastes (e.g. a folder URL) into stable IDs. Throw
|
||||
* `InvalidRequestError` when the input can't be resolved.
|
||||
*/
|
||||
export interface SourceRootResolver {
|
||||
resolveSourceRoot(input: string): Promise<ResolvedRoot<SourceRoot>>;
|
||||
}
|
||||
|
||||
export interface TargetRootResolver {
|
||||
resolveTargetRoot(input: string): Promise<ResolvedRoot<TargetRoot>>;
|
||||
}
|
||||
|
||||
export interface ProjectRepository {
|
||||
get(id: string): Promise<Project | null>;
|
||||
list(): Promise<Project[]>;
|
||||
create(project: NewProject): Promise<Project>;
|
||||
update(id: string, changes: ProjectChanges): Promise<void>;
|
||||
/** Removes the project with all its items and history. */
|
||||
delete(id: string): Promise<void>;
|
||||
pause(id: string, reason: string): Promise<void>;
|
||||
resume(id: string): Promise<void>;
|
||||
markFirstSynced(id: string, at: Date): Promise<void>;
|
||||
@@ -65,7 +88,9 @@ export interface ProjectRepository {
|
||||
}
|
||||
|
||||
export interface ItemRepository {
|
||||
get(projectId: string, sourceId: string): Promise<ItemState | null>;
|
||||
listByProject(projectId: string): Promise<ItemState[]>;
|
||||
countByStatus(projectId: string): Promise<StatusCounts>;
|
||||
/** Atomically store ingested changes and the cursor they were read up to. */
|
||||
commitIngest(projectId: string, items: ItemState[], cursor: string): Promise<void>;
|
||||
save(projectId: string, items: ItemState[]): Promise<void>;
|
||||
@@ -78,6 +103,7 @@ export interface RunRepository {
|
||||
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
|
||||
): Promise<void>;
|
||||
addEvent(event: RunEvent): Promise<void>;
|
||||
latest(projectId: string): Promise<RunRecord | null>;
|
||||
listSince(projectId: string, since: Date): Promise<RunRecord[]>;
|
||||
listEvents(runId: string): Promise<RunEvent[]>;
|
||||
/** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { errorMessage, ServiceUnavailableError } from './errors';
|
||||
import { errorMessage, ServiceUnavailableError } from './errors.ts';
|
||||
import {
|
||||
emptyCounts,
|
||||
type EventAction,
|
||||
@@ -8,10 +8,16 @@ import {
|
||||
type SyncAction,
|
||||
type SyncedState,
|
||||
type TargetRef
|
||||
} from './model';
|
||||
import { planAction, withStatus, workQueue } from './plan';
|
||||
import type { ChangeSource, Clock, DocumentTarget, ItemRepository, RunRepository } from './ports';
|
||||
import { ItemTree } from './tree';
|
||||
} from './model.ts';
|
||||
import { planAction, withStatus, workQueue } from './plan.ts';
|
||||
import type {
|
||||
ChangeSource,
|
||||
Clock,
|
||||
DocumentTarget,
|
||||
ItemRepository,
|
||||
RunRepository
|
||||
} from './ports.ts';
|
||||
import { ItemTree } from './tree.ts';
|
||||
|
||||
export const WAITING_FOR_PARENT = 'Waiting for its parent folder to sync.';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ItemState } from './model';
|
||||
import type { ItemState } from './model.ts';
|
||||
|
||||
type ParentOf = (item: ItemState) => string | null;
|
||||
|
||||
|
||||
125
src/lib/server/sync/project-service.ts
Normal file
125
src/lib/server/sync/project-service.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { InvalidRequestError } from './core/errors.ts';
|
||||
import type { Project, ProjectChanges } from './core/model.ts';
|
||||
import { withStatus } from './core/plan.ts';
|
||||
import type {
|
||||
ItemRepository,
|
||||
ProjectRepository,
|
||||
SourceRootResolver,
|
||||
TargetRootResolver
|
||||
} from './core/ports.ts';
|
||||
|
||||
export const ROOTS_LOCKED =
|
||||
'Roots are locked after the first sync. Reset the project to change them.';
|
||||
export const BUSY = 'A sync is running for this project. Try again when it finishes.';
|
||||
|
||||
export interface NewProjectInput {
|
||||
name: string;
|
||||
sourceInput: string;
|
||||
targetInput: string;
|
||||
}
|
||||
|
||||
export interface ProjectEdit {
|
||||
name: string;
|
||||
enabled: boolean;
|
||||
/** Blank or omitted leaves the root unchanged. */
|
||||
sourceInput?: string;
|
||||
targetInput?: string;
|
||||
}
|
||||
|
||||
export interface ProjectServiceDeps {
|
||||
projects: ProjectRepository;
|
||||
items: ItemRepository;
|
||||
sourceResolver: SourceRootResolver;
|
||||
targetResolver: TargetRootResolver;
|
||||
activity: { isRunning(projectId: string): boolean };
|
||||
maxAttempts: number;
|
||||
}
|
||||
|
||||
/** Admin operations on projects, enforcing the rules that keep sync state consistent. */
|
||||
export class ProjectService {
|
||||
constructor(private readonly deps: ProjectServiceDeps) {}
|
||||
|
||||
async create(input: NewProjectInput): Promise<Project> {
|
||||
const [source, target] = await Promise.all([
|
||||
this.deps.sourceResolver.resolveSourceRoot(required(input.sourceInput, 'SharePoint folder')),
|
||||
this.deps.targetResolver.resolveTargetRoot(required(input.targetInput, 'ACC folder'))
|
||||
]);
|
||||
return this.deps.projects.create({
|
||||
name: validName(input.name),
|
||||
source: source.root,
|
||||
sourceDisplayPath: source.displayPath,
|
||||
target: target.root,
|
||||
targetDisplayPath: target.displayPath
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, edit: ProjectEdit): Promise<void> {
|
||||
const project = await this.require(id);
|
||||
const changes: ProjectChanges = { name: validName(edit.name), enabled: edit.enabled };
|
||||
const sourceInput = edit.sourceInput?.trim();
|
||||
const targetInput = edit.targetInput?.trim();
|
||||
|
||||
if ((sourceInput || targetInput) && project.firstSyncedAt) {
|
||||
throw new InvalidRequestError(ROOTS_LOCKED);
|
||||
}
|
||||
if (sourceInput) {
|
||||
const source = await this.deps.sourceResolver.resolveSourceRoot(sourceInput);
|
||||
Object.assign(changes, { source: source.root, sourceDisplayPath: source.displayPath });
|
||||
}
|
||||
if (targetInput) {
|
||||
const target = await this.deps.targetResolver.resolveTargetRoot(targetInput);
|
||||
Object.assign(changes, { target: target.root, targetDisplayPath: target.displayPath });
|
||||
}
|
||||
await this.deps.projects.update(id, changes);
|
||||
}
|
||||
|
||||
async resume(id: string): Promise<void> {
|
||||
await this.require(id);
|
||||
await this.deps.projects.resume(id);
|
||||
}
|
||||
|
||||
/** Forget all sync state; the next run starts from a full listing and roots unlock. */
|
||||
async reset(id: string): Promise<void> {
|
||||
await this.requireIdle(id);
|
||||
await this.deps.projects.reset(id);
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.requireIdle(id);
|
||||
await this.deps.projects.delete(id);
|
||||
}
|
||||
|
||||
/** Give a failed item a fresh set of attempts on the next run. */
|
||||
async retryItem(projectId: string, sourceId: string): Promise<void> {
|
||||
await this.requireIdle(projectId);
|
||||
const item = await this.deps.items.get(projectId, sourceId);
|
||||
if (!item) throw new InvalidRequestError('That file is no longer tracked.');
|
||||
if (item.status !== 'failed') return;
|
||||
const fresh = withStatus({ ...item, attemptCount: 0, lastError: null }, this.deps.maxAttempts);
|
||||
await this.deps.items.save(projectId, [fresh]);
|
||||
}
|
||||
|
||||
private async require(id: string): Promise<Project> {
|
||||
const project = await this.deps.projects.get(id);
|
||||
if (!project) throw new InvalidRequestError('Project not found.');
|
||||
return project;
|
||||
}
|
||||
|
||||
private async requireIdle(id: string): Promise<Project> {
|
||||
const project = await this.require(id);
|
||||
if (this.deps.activity.isRunning(id)) throw new InvalidRequestError(BUSY);
|
||||
return project;
|
||||
}
|
||||
}
|
||||
|
||||
function required(value: string, label: string): string {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) throw new InvalidRequestError(`${label} is required.`);
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function validName(name: string): string {
|
||||
const trimmed = required(name, 'Name');
|
||||
if (trimmed.length > 100) throw new InvalidRequestError('Name must be 100 characters or fewer.');
|
||||
return trimmed;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { IntervalScheduler } from './scheduler';
|
||||
import { silentLogger } from './testing/harness';
|
||||
import { IntervalScheduler } from './scheduler.ts';
|
||||
import { silentLogger } from './testing/harness.ts';
|
||||
|
||||
describe('IntervalScheduler', () => {
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
@@ -23,6 +23,18 @@ describe('IntervalScheduler', () => {
|
||||
scheduler.stop();
|
||||
});
|
||||
|
||||
it('can run the first time immediately', async () => {
|
||||
const scheduler = new IntervalScheduler(silentLogger);
|
||||
const task = vi.fn().mockResolvedValue(undefined);
|
||||
scheduler.every('eager', 1000, task, { immediate: true });
|
||||
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(task).toHaveBeenCalledTimes(1);
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
expect(task).toHaveBeenCalledTimes(2);
|
||||
scheduler.stop();
|
||||
});
|
||||
|
||||
it('keeps going after a task throws, and stops cleanly', async () => {
|
||||
const scheduler = new IntervalScheduler(silentLogger);
|
||||
const task = vi.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValue(undefined);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { errorMessage } from './core/errors';
|
||||
import type { Logger } from './core/ports';
|
||||
import { errorMessage } from './core/errors.ts';
|
||||
import type { Logger } from './core/ports.ts';
|
||||
|
||||
/**
|
||||
* Runs async tasks on fixed intervals. The next run of a task is scheduled only
|
||||
@@ -11,7 +11,13 @@ export class IntervalScheduler {
|
||||
|
||||
constructor(private readonly logger: Logger) {}
|
||||
|
||||
every(name: string, intervalMs: number, task: () => Promise<unknown>): void {
|
||||
/** With `immediate`, the first run starts right away instead of after one interval. */
|
||||
every(
|
||||
name: string,
|
||||
intervalMs: number,
|
||||
task: () => Promise<unknown>,
|
||||
{ immediate = false } = {}
|
||||
): void {
|
||||
const tick = async () => {
|
||||
try {
|
||||
await task();
|
||||
@@ -20,15 +26,15 @@ export class IntervalScheduler {
|
||||
}
|
||||
schedule();
|
||||
};
|
||||
const schedule = () => {
|
||||
const schedule = (delayMs = intervalMs) => {
|
||||
if (this.stopped) return;
|
||||
const timer = setTimeout(() => {
|
||||
this.timers.delete(timer);
|
||||
void tick();
|
||||
}, intervalMs);
|
||||
}, delayMs);
|
||||
this.timers.add(timer);
|
||||
};
|
||||
schedule();
|
||||
schedule(immediate ? 0 : intervalMs);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
import type { FileContent, TargetRef } from '../core/model';
|
||||
import type { DocumentTarget } from '../core/ports';
|
||||
|
||||
interface AccNode {
|
||||
id: string;
|
||||
parentId: string | null;
|
||||
name: string;
|
||||
isFolder: boolean;
|
||||
deleted: boolean;
|
||||
versions: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* In-memory stand-in for ACC Docs. Deleting a folder hides its contents (as in
|
||||
* ACC); restoring a folder does not bring its contents back.
|
||||
*/
|
||||
export class FakeAcc implements DocumentTarget {
|
||||
readonly rootFolderId = 'acc-root';
|
||||
readonly calls: string[] = [];
|
||||
private readonly nodes = new Map<string, AccNode>();
|
||||
private readonly failures = new Map<string, { error: Error; remaining: number }>();
|
||||
private nextId = 1;
|
||||
|
||||
constructor() {
|
||||
this.nodes.set(this.rootFolderId, {
|
||||
id: this.rootFolderId,
|
||||
parentId: null,
|
||||
name: 'Project Files',
|
||||
isFolder: true,
|
||||
deleted: false,
|
||||
versions: []
|
||||
});
|
||||
}
|
||||
|
||||
/** Make the next `times` operations on an item with this name throw `error`. */
|
||||
failOn(name: string, error: Error, times = Infinity): void {
|
||||
this.failures.set(name, { error, remaining: times });
|
||||
}
|
||||
|
||||
/** Adds a file directly, as if someone uploaded it by hand in ACC. */
|
||||
addByHand(parentId: string, name: string): string {
|
||||
return this.insert(parentId, name, false, ['by hand']);
|
||||
}
|
||||
|
||||
/** Live paths, e.g. `Drawings/`, `Drawings/plan.pdf@2` (file@version count). */
|
||||
tree(): string[] {
|
||||
return [...this.nodes.values()]
|
||||
.filter((node) => node.id !== this.rootFolderId && this.isAlive(node))
|
||||
.map((node) => this.path(node) + (node.isFolder ? '/' : `@${node.versions.length}`))
|
||||
.sort();
|
||||
}
|
||||
|
||||
latestContent(path: string): string | undefined {
|
||||
const node = [...this.nodes.values()].find((n) => this.isAlive(n) && this.path(n) === path);
|
||||
return node?.versions.at(-1);
|
||||
}
|
||||
|
||||
async createFolder(parentId: string, name: string): Promise<TargetRef> {
|
||||
this.enter('createFolder', name);
|
||||
return { id: this.insert(parentId, name, true, []), isFolder: true };
|
||||
}
|
||||
|
||||
async uploadFile(parentId: string, name: string, content: FileContent): Promise<TargetRef> {
|
||||
this.enter('uploadFile', name);
|
||||
return { id: this.insert(parentId, name, false, [await read(content)]), isFolder: false };
|
||||
}
|
||||
|
||||
async uploadVersion(ref: TargetRef, name: string, content: FileContent): Promise<void> {
|
||||
const node = this.node(ref, 'uploadVersion');
|
||||
node.versions.push(await read(content));
|
||||
node.name = name;
|
||||
}
|
||||
|
||||
async rename(ref: TargetRef, name: string): Promise<void> {
|
||||
this.node(ref, 'rename').name = name;
|
||||
}
|
||||
|
||||
async move(ref: TargetRef, newParentId: string): Promise<TargetRef> {
|
||||
this.node(ref, 'move').parentId = newParentId;
|
||||
return ref;
|
||||
}
|
||||
|
||||
async delete(ref: TargetRef): Promise<void> {
|
||||
this.node(ref, 'delete').deleted = true;
|
||||
}
|
||||
|
||||
async restore(
|
||||
ref: TargetRef,
|
||||
parentId: string,
|
||||
name: string,
|
||||
content?: FileContent
|
||||
): Promise<TargetRef> {
|
||||
const node = this.node(ref, 'restore');
|
||||
Object.assign(node, { deleted: false, parentId, name });
|
||||
if (content) node.versions.push(await read(content));
|
||||
return ref;
|
||||
}
|
||||
|
||||
private insert(parentId: string, name: string, isFolder: boolean, versions: string[]): string {
|
||||
if (!this.nodes.has(parentId)) throw new Error(`No ACC folder ${parentId}`);
|
||||
const id = `acc-${this.nextId++}`;
|
||||
this.nodes.set(id, { id, parentId, name, isFolder, deleted: false, versions });
|
||||
return id;
|
||||
}
|
||||
|
||||
private node(ref: TargetRef, operation: string): AccNode {
|
||||
const node = this.nodes.get(ref.id);
|
||||
if (!node) throw new Error(`No ACC item ${ref.id}`);
|
||||
this.enter(operation, node.name);
|
||||
return node;
|
||||
}
|
||||
|
||||
private enter(operation: string, name: string): void {
|
||||
this.calls.push(`${operation} ${name}`);
|
||||
const failure = this.failures.get(name);
|
||||
if (failure && failure.remaining > 0) {
|
||||
failure.remaining--;
|
||||
throw failure.error;
|
||||
}
|
||||
}
|
||||
|
||||
private isAlive(node: AccNode): boolean {
|
||||
for (
|
||||
let n: AccNode | undefined = node;
|
||||
n;
|
||||
n = n.parentId ? this.nodes.get(n.parentId) : undefined
|
||||
) {
|
||||
if (n.deleted) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private path(node: AccNode): string {
|
||||
const names: string[] = [];
|
||||
for (let n: AccNode | undefined = node; n && n.id !== this.rootFolderId;) {
|
||||
names.unshift(n.name);
|
||||
n = n.parentId ? this.nodes.get(n.parentId) : undefined;
|
||||
}
|
||||
return names.join('/');
|
||||
}
|
||||
}
|
||||
|
||||
async function read(content: FileContent): Promise<string> {
|
||||
return new Response(content.stream).text();
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
import type { FileContent, SourceChange, SourceItem, SourceRoot } from '../core/model';
|
||||
import type { ChangeBatch, ChangeSource } from '../core/ports';
|
||||
|
||||
interface DriveNode {
|
||||
id: string;
|
||||
parentId: string | null;
|
||||
name: string;
|
||||
isFolder: boolean;
|
||||
content: string;
|
||||
version: number;
|
||||
deleted: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* In-memory stand-in for a SharePoint document library that behaves like Graph
|
||||
* delta: a null cursor lists everything; otherwise each item changed since the
|
||||
* cursor is reported once, in its latest state.
|
||||
*/
|
||||
export class FakeDrive implements ChangeSource {
|
||||
readonly driveRootId = 'drive-root';
|
||||
private readonly nodes = new Map<string, DriveNode>();
|
||||
private readonly log: string[] = [];
|
||||
private nextId = 1;
|
||||
|
||||
constructor() {
|
||||
this.nodes.set(this.driveRootId, {
|
||||
id: this.driveRootId,
|
||||
parentId: null,
|
||||
name: 'root',
|
||||
isFolder: true,
|
||||
content: '',
|
||||
version: 0,
|
||||
deleted: false
|
||||
});
|
||||
}
|
||||
|
||||
addFolder(parentId: string, name: string): string {
|
||||
return this.add(parentId, name, true, '');
|
||||
}
|
||||
|
||||
addFile(parentId: string, name: string, content = `${name} v1`): string {
|
||||
return this.add(parentId, name, false, content);
|
||||
}
|
||||
|
||||
edit(id: string, content: string): void {
|
||||
this.change(id, (node) => {
|
||||
node.content = content;
|
||||
node.version++;
|
||||
});
|
||||
}
|
||||
|
||||
rename(id: string, name: string): void {
|
||||
this.change(id, (node) => (node.name = name));
|
||||
}
|
||||
|
||||
move(id: string, parentId: string): void {
|
||||
this.change(id, (node) => (node.parentId = parentId));
|
||||
}
|
||||
|
||||
/** Reports only the removed item, not its contents: the harder case for the engine. */
|
||||
remove(id: string): void {
|
||||
this.change(id, (node) => (node.deleted = true));
|
||||
}
|
||||
|
||||
/** Reports only the restored item, not its contents. */
|
||||
restore(id: string): void {
|
||||
this.change(id, (node) => (node.deleted = false));
|
||||
}
|
||||
|
||||
async getChanges(_root: SourceRoot, cursor: string | null): Promise<ChangeBatch> {
|
||||
const end = String(this.log.length);
|
||||
if (cursor === null) {
|
||||
const changes = [...this.nodes.values()]
|
||||
.filter((node) => this.isAlive(node))
|
||||
.map((node) => this.toChange(node));
|
||||
return { changes, cursor: end, fullSync: true };
|
||||
}
|
||||
const ids = [...new Set(this.log.slice(Number(cursor)).reverse())].reverse();
|
||||
return {
|
||||
changes: ids.map((id) => this.toChange(this.nodes.get(id)!)),
|
||||
cursor: end,
|
||||
fullSync: false
|
||||
};
|
||||
}
|
||||
|
||||
async download(_root: SourceRoot, sourceId: string): Promise<FileContent> {
|
||||
const bytes = new TextEncoder().encode(this.nodes.get(sourceId)!.content);
|
||||
return { size: bytes.length, stream: new Response(bytes).body! };
|
||||
}
|
||||
|
||||
private add(parentId: string, name: string, isFolder: boolean, content: string): string {
|
||||
const id = `sp-${this.nextId++}`;
|
||||
this.nodes.set(id, { id, parentId, name, isFolder, content, version: 1, deleted: false });
|
||||
this.log.push(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
private change(id: string, mutate: (node: DriveNode) => void): void {
|
||||
mutate(this.nodes.get(id)!);
|
||||
this.log.push(id);
|
||||
}
|
||||
|
||||
private isAlive(node: DriveNode): boolean {
|
||||
for (
|
||||
let n: DriveNode | undefined = node;
|
||||
n;
|
||||
n = n.parentId ? this.nodes.get(n.parentId) : undefined
|
||||
) {
|
||||
if (n.deleted) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private toChange(node: DriveNode): SourceChange {
|
||||
if (node.deleted) return { kind: 'delete', sourceId: node.id };
|
||||
const item: SourceItem = {
|
||||
sourceId: node.id,
|
||||
parentSourceId: node.parentId,
|
||||
name: node.name,
|
||||
isFolder: node.isFolder,
|
||||
contentTag: node.isFolder ? null : `c${node.version}`,
|
||||
modifiedAt: new Date(Date.UTC(2026, 0, 1, 0, 0, node.version)),
|
||||
size: node.isFolder ? null : node.content.length
|
||||
};
|
||||
return { kind: 'upsert', item };
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import { openDatabase } from '../../adapters/sqlite/database';
|
||||
import { SqliteItemRepository } from '../../adapters/sqlite/item-repository';
|
||||
import { SqliteProjectRepository } from '../../adapters/sqlite/project-repository';
|
||||
import { SqliteRunRepository } from '../../adapters/sqlite/run-repository';
|
||||
import { SyncEngine } from '../core/engine';
|
||||
import { PatternIgnorePolicy } from '../core/ignore';
|
||||
import type { Logger } from '../core/ports';
|
||||
import { Reconciler } from '../core/reconciler';
|
||||
import { FakeAcc } from './fake-acc';
|
||||
import { FakeDrive } from './fake-drive';
|
||||
import { openDatabase } from '#lib/server/adapters/sqlite/database.ts';
|
||||
import { SqliteItemRepository } from '#lib/server/adapters/sqlite/item-repository.ts';
|
||||
import { SqliteProjectRepository } from '#lib/server/adapters/sqlite/project-repository.ts';
|
||||
import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts';
|
||||
import { SyncEngine } from '#lib/server/sync/core/engine.ts';
|
||||
import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts';
|
||||
import type { Logger } from '#lib/server/sync/core/ports.ts';
|
||||
import { Reconciler } from '#lib/server/sync/core/reconciler.ts';
|
||||
import { FakeAcc } from '#lib/server/adapters/simulated/fake-acc.ts';
|
||||
import { FakeDrive } from '#lib/server/adapters/simulated/fake-drive.ts';
|
||||
|
||||
export const silentLogger: Logger = { info() {}, warn() {}, error() {} };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user