// The readiness rule that stops the tree rendering FALSE file statuses.
//
// The status engine's inputs hydrate independently and asynchronously: the
// changelist and synced manifest come from the native store, the local listing
// from a disk scan, the depot tree from the server. Computing on an
// intermediate combination produces chips that are simply wrong — a
// pre-hydration empty manifest reads as "nothing was ever synced here" and an
// empty changelist as "nothing is marked" — which is why files flashed bogus
// statuses on open and after every window refocus.
//
// While this returns false the caller skips the computation entirely, so the
// last fully-hydrated status map stays on screen (or none at all, right after
// an identity switch) rather than being replaced by a wrong one.
import { describe, expect, it } from 'vitest';
import { statusInputsReady } from './useFileControl';

const ready = {
  manifestReady: true,
  changelistReady: true,
  workspacePath: '/ws/alpha',
  localListing: { children: [] },
  depotTree: { children: [] },
};

describe('statusInputsReady', () => {
  it('is ready when every input has hydrated', () => {
    expect(statusInputsReady(ready)).toBe(true);
  });

  it('waits for the synced manifest', () => {
    expect(statusInputsReady({ ...ready, manifestReady: false })).toBe(false);
  });

  it('waits for the changelist', () => {
    expect(statusInputsReady({ ...ready, changelistReady: false })).toBe(false);
  });

  it('waits for the depot tree', () => {
    expect(statusInputsReady({ ...ready, depotTree: null })).toBe(false);
  });

  it('waits for the workspace scan when a workspace folder is set', () => {
    // This is the dramatic case: RepoPanel nulls the listing whenever it is
    // tagged for a different folder than the one being rendered, so on a
    // project/workspace switch the engine would otherwise see an EMPTY
    // workspace and report every synced file as gone from disk.
    expect(statusInputsReady({ ...ready, localListing: null })).toBe(false);
  });

  it('does not wait for a workspace scan when no folder is set', () => {
    // No workspace folder means there is no scan to wait for — the Server tab
    // must still get its lock statuses.
    expect(statusInputsReady({ ...ready, workspacePath: '', localListing: null })).toBe(true);
  });
});
