Prepare datamodel for multi-environment (#1765)

Co-authored-by: justsomelegs <[email protected]>
Co-authored-by: codex <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-04-09 02:05:20 +00:00
1 parent 72b7f90c50
commit b96308fc5a
121 files changed
+10195 -4958

No files matched your search

+47 -9
View File
@@ -1,4 +1,4 @@
import type { GitStatusResult } from "@t3tools/contracts";
import { EnvironmentId, type GitStatusResult } from "@t3tools/contracts";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
@@ -16,6 +16,10 @@ function registerListener<T>(listeners: Set<(event: T) => void>, listener: (even
}
const gitStatusListeners = new Set<(event: GitStatusResult) => void>();
const ENVIRONMENT_ID = EnvironmentId.makeUnsafe("environment-local");
const OTHER_ENVIRONMENT_ID = EnvironmentId.makeUnsafe("environment-remote");
const TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/repo" } as const;
const FRESH_TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/fresh" } as const;
const BASE_STATUS: GitStatusResult = {
isRepo: true,
@@ -55,7 +59,7 @@ afterEach(() => {
describe("gitStatusState", () => {
it("starts fresh cwd state in a pending state", () => {
expect(getGitStatusSnapshot("/fresh")).toEqual({
expect(getGitStatusSnapshot(FRESH_TARGET)).toEqual({
data: null,
error: null,
cause: null,
@@ -64,11 +68,11 @@ describe("gitStatusState", () => {
});
it("shares one live subscription per cwd and updates the per-cwd atom snapshot", () => {
const releaseA = watchGitStatus("/repo", gitClient);
const releaseB = watchGitStatus("/repo", gitClient);
const releaseA = watchGitStatus(TARGET, gitClient);
const releaseB = watchGitStatus(TARGET, gitClient);
expect(gitClient.onStatus).toHaveBeenCalledOnce();
expect(getGitStatusSnapshot("/repo")).toEqual({
expect(getGitStatusSnapshot(TARGET)).toEqual({
data: null,
error: null,
cause: null,
@@ -77,7 +81,7 @@ describe("gitStatusState", () => {
emitGitStatus(BASE_STATUS);
expect(getGitStatusSnapshot("/repo")).toEqual({
expect(getGitStatusSnapshot(TARGET)).toEqual({
data: BASE_STATUS,
error: null,
cause: null,
@@ -92,15 +96,15 @@ describe("gitStatusState", () => {
});
it("refreshes git status through the unary RPC without restarting the stream", async () => {
const release = watchGitStatus("/repo", gitClient);
const release = watchGitStatus(TARGET, gitClient);
emitGitStatus(BASE_STATUS);
const refreshed = await refreshGitStatus("/repo", gitClient);
const refreshed = await refreshGitStatus(TARGET, gitClient);
expect(gitClient.onStatus).toHaveBeenCalledOnce();
expect(gitClient.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
expect(refreshed).toEqual({ ...BASE_STATUS, branch: "/repo-refreshed" });
expect(getGitStatusSnapshot("/repo")).toEqual({
expect(getGitStatusSnapshot(TARGET)).toEqual({
data: BASE_STATUS,
error: null,
cause: null,
@@ -109,4 +113,38 @@ describe("gitStatusState", () => {
release();
});
it("keeps git status subscriptions isolated by environment when cwds match", () => {
const localListeners = new Set<(event: GitStatusResult) => void>();
const remoteListeners = new Set<(event: GitStatusResult) => void>();
const localClient = {
refreshStatus: vi.fn(),
onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
registerListener(localListeners, listener),
),
};
const remoteClient = {
refreshStatus: vi.fn(),
onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
registerListener(remoteListeners, listener),
),
};
const remoteTarget = { environmentId: OTHER_ENVIRONMENT_ID, cwd: "/repo" } as const;
const releaseLocal = watchGitStatus(TARGET, localClient);
const releaseRemote = watchGitStatus(remoteTarget, remoteClient);
for (const listener of localListeners) {
listener(BASE_STATUS);
}
for (const listener of remoteListeners) {
listener({ ...BASE_STATUS, branch: "remote-branch" });
}
expect(getGitStatusSnapshot(TARGET).data?.branch).toBe("feature/push-status");
expect(getGitStatusSnapshot(remoteTarget).data?.branch).toBe("remote-branch");
releaseLocal();
releaseRemote();
});
});