mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
275 lines
8.8 KiB
TypeScript
275 lines
8.8 KiB
TypeScript
import type { Page } from "@playwright/test"
|
|
import { mockOpenCodeServer } from "../../e2e/utils/mock-server"
|
|
import { fixture as stress } from "../../e2e/utils/session-fixture"
|
|
|
|
// Home shows the newest 64 root sessions.
|
|
const HOME_VISIBLE = 64
|
|
|
|
type HomeIndexSession = {
|
|
id: string
|
|
projectID: string
|
|
title?: string
|
|
agent: string
|
|
model: { id: string; providerID: string; variant: string }
|
|
cost: number
|
|
tokens: { input: number; output: number; reasoning: number; cache: { read: number; write: number } }
|
|
outcome: "succeeded" | "failed" | "interrupted"
|
|
time: { created: number; updated: number; idle: number; viewed?: number }
|
|
location: { directory: string }
|
|
}
|
|
|
|
type HomeIndexDirectory = {
|
|
directory: string
|
|
name: string
|
|
projectID: string
|
|
// Local project entries appear in the Home project list; the rest model
|
|
// sessions whose project was removed from the sidebar.
|
|
project: boolean
|
|
}
|
|
|
|
const repos = [
|
|
"opencode",
|
|
"storefront-api",
|
|
"billing-worker",
|
|
"design-system",
|
|
"mobile-app",
|
|
"infra-terraform",
|
|
"docs-site",
|
|
"analytics-pipeline",
|
|
"auth-service",
|
|
"legacy-admin",
|
|
"notebooks",
|
|
"dotfiles",
|
|
]
|
|
|
|
const verbs = [
|
|
"Fix",
|
|
"Investigate",
|
|
"Refactor",
|
|
"Add",
|
|
"Remove",
|
|
"Debug",
|
|
"Migrate",
|
|
"Implement",
|
|
"Review",
|
|
"Optimize",
|
|
"Document",
|
|
"Rename",
|
|
"Extract",
|
|
"Wire up",
|
|
"Stabilize",
|
|
]
|
|
|
|
const objects = [
|
|
"flaky retry in the session runner",
|
|
"memory growth in the Home index",
|
|
"i18n keys for the settings dialog",
|
|
"the review pane remount on tab switch",
|
|
"SQLite migration for session inbox",
|
|
"OAuth callback handling",
|
|
"terminal scrollback serialization",
|
|
"Playwright visual stability probes",
|
|
"the composer paste path",
|
|
"provider catalog normalization",
|
|
"the worktree preparation flow",
|
|
"CI cache keys for bun install",
|
|
"Markdown highlighting for large fences",
|
|
"the permission auto-approver",
|
|
"cursor pagination for /api/session",
|
|
"the desktop titlebar on Windows",
|
|
"the file tree lazy loading",
|
|
"event replay ordering",
|
|
"RTL layout in the sidebar",
|
|
"unread badges for background sessions",
|
|
]
|
|
|
|
const contexts = [
|
|
"",
|
|
"",
|
|
"",
|
|
" (#{n})",
|
|
" in packages/app",
|
|
" in packages/core",
|
|
" for v2",
|
|
" before release",
|
|
" — follow-up",
|
|
" · src/{file}.ts",
|
|
" after the Electron upgrade",
|
|
" with tests",
|
|
]
|
|
|
|
const files = ["controller", "index", "records", "store", "runtime", "layout", "timeline", "composer", "data", "sync"]
|
|
|
|
const agents = ["build", "build", "build", "build", "plan", "general"]
|
|
|
|
const models = [
|
|
{ id: "claude-opus-4-6", providerID: "anthropic", variant: "default" },
|
|
{ id: "claude-sonnet-4-6", providerID: "anthropic", variant: "default" },
|
|
{ id: "gpt-5.3-codex", providerID: "openai", variant: "high" },
|
|
{ id: "gemini-3-pro", providerID: "google", variant: "default" },
|
|
]
|
|
|
|
const DAY = 24 * 60 * 60 * 1000
|
|
|
|
function createHomeIndexFixture(input: { count: number; now: number }) {
|
|
const random = mulberry32(0x5eed_0000 + input.count)
|
|
const directoryCount = Math.min(12, repos.length)
|
|
|
|
const directories: HomeIndexDirectory[] = repos.slice(0, directoryCount).map((name, index) => ({
|
|
directory: `/Users/dev/repos/${name}`,
|
|
name,
|
|
projectID: `prj_${hex(random, 16)}`,
|
|
project: index < Math.max(1, Math.round(directoryCount * 0.66)),
|
|
}))
|
|
|
|
// Zipf-like spread: a few repositories hold most of the history.
|
|
const weights = directories.map((_, index) => 1 / Math.pow(index + 1, 0.9))
|
|
const total = weights.reduce((sum, weight) => sum + weight, 0)
|
|
const cumulative = weights.map((_, index) => weights.slice(0, index + 1).reduce((sum, w) => sum + w, 0) / total)
|
|
|
|
// Newest first; adding the index after sorting keeps offsets strictly
|
|
// increasing so no two sessions share an updated time.
|
|
const offsets = Array.from({ length: input.count }, () => {
|
|
const bucket = random()
|
|
|
|
// 5% today, 5% yesterday, the rest skewed toward recent months over 18 months.
|
|
if (bucket < 0.05) return Math.floor(random() * DAY * 0.9)
|
|
|
|
if (bucket < 0.1) return DAY + Math.floor(random() * DAY * 0.9)
|
|
|
|
return 2 * DAY + Math.floor(Math.pow(random(), 2) * 538 * DAY)
|
|
})
|
|
.sort((a, b) => a - b)
|
|
.map((offset, index) => offset + index)
|
|
|
|
const newestFirst: HomeIndexSession[] = offsets.map((offset, index) => {
|
|
const pick = random()
|
|
const directory = directories[cumulative.findIndex((edge) => pick <= edge)] ?? directories[0]
|
|
const updated = input.now - offset
|
|
const duration = 5 * 60_000 + Math.floor(random() * 6 * 60 * 60_000)
|
|
|
|
const tokens = {
|
|
input: 5_000 + Math.floor(random() * 400_000),
|
|
output: 500 + Math.floor(random() * 60_000),
|
|
reasoning: random() < 0.6 ? Math.floor(random() * 20_000) : 0,
|
|
cache: { read: Math.floor(random() * 900_000), write: Math.floor(random() * 120_000) },
|
|
}
|
|
|
|
const outcome = random() < 0.9 ? "succeeded" : random() < 0.6 ? "failed" : "interrupted"
|
|
|
|
// Drawn in this order so a size always gives the same sessions. 3% have no title and show their ID.
|
|
const id = `ses_${base62(random, 26)}`
|
|
const titled = random() < 0.97 ? title(random, index) : undefined
|
|
const agent = agents[Math.floor(random() * agents.length)]
|
|
const model = models[Math.floor(random() * models.length)]
|
|
const idle = updated - Math.floor(random() * 2_000)
|
|
const viewed = random() < 0.8 ? updated : undefined
|
|
|
|
return {
|
|
id,
|
|
projectID: directory.projectID,
|
|
title: titled,
|
|
agent,
|
|
model,
|
|
// USD at $3/M input, $15/M output, $0.30/M cache read, $3.75/M cache write.
|
|
cost:
|
|
Math.round(
|
|
(tokens.input * 3 + tokens.output * 15 + tokens.cache.read * 0.3 + tokens.cache.write * 3.75) / 100,
|
|
) / 10_000,
|
|
tokens,
|
|
outcome,
|
|
time: { created: updated - duration, updated, idle, viewed },
|
|
location: { directory: directory.directory },
|
|
}
|
|
})
|
|
|
|
return {
|
|
now: input.now,
|
|
directories,
|
|
// The mock lists sessions in array order and reverses for `desc`, so keep
|
|
// the fixture ascending by updated time like the server's index order.
|
|
sessions: newestFirst.toReversed(),
|
|
newest: newestFirst[0],
|
|
/** The last session Home shows: once its row is on screen, every row is. */
|
|
lastShown: newestFirst[Math.min(HOME_VISIBLE, input.count) - 1],
|
|
}
|
|
}
|
|
|
|
function title(random: () => number, index: number) {
|
|
const verb = verbs[Math.floor(random() * verbs.length)]
|
|
const object = objects[Math.floor(random() * objects.length)]
|
|
|
|
const context = contexts[Math.floor(random() * contexts.length)]
|
|
.replace("{n}", String(1000 + Math.floor(random() * 45_000)))
|
|
.replace("{file}", files[Math.floor(random() * files.length)])
|
|
|
|
// Keep titles unique so row identity checks cannot match a sibling.
|
|
return `${verb} ${object}${context} [${index.toString(36)}]`
|
|
}
|
|
|
|
function mulberry32(seed: number) {
|
|
let state = seed >>> 0
|
|
|
|
return () => {
|
|
state = (state + 0x6d2b79f5) >>> 0
|
|
let t = state
|
|
t = Math.imul(t ^ (t >>> 15), t | 1)
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
|
|
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
}
|
|
}
|
|
|
|
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
function base62(random: () => number, length: number) {
|
|
return Array.from({ length }, () => alphabet[Math.floor(random() * alphabet.length)]).join("")
|
|
}
|
|
|
|
function hex(random: () => number, length: number) {
|
|
return Array.from({ length }, () => Math.floor(random() * 16).toString(16)).join("")
|
|
}
|
|
|
|
/**
|
|
* Home with `count` root sessions across 12 folders, 8 of them projects, under the variant's origin. Home shows the
|
|
* newest 64 whatever the count, so what grows with `count` is index handling, not what is on screen.
|
|
*/
|
|
export async function mockHome(page: Page, input: { server: string; count: number }) {
|
|
const home = createHomeIndexFixture({ count: input.count, now: Date.now() })
|
|
const primary = home.directories[0]
|
|
await mockOpenCodeServer(page, {
|
|
server: input.server,
|
|
directory: primary.directory,
|
|
project: {
|
|
id: primary.projectID,
|
|
worktree: primary.directory,
|
|
vcs: "git",
|
|
name: primary.name,
|
|
time: { created: home.now - 400 * 86_400_000, updated: home.now },
|
|
sandboxes: [],
|
|
},
|
|
sessions: home.sessions,
|
|
pageMessages: () => ({ items: [] }),
|
|
provider: stress.provider,
|
|
})
|
|
await installProjects(
|
|
page,
|
|
home.directories.filter((entry) => entry.project).map((entry) => entry.directory),
|
|
)
|
|
|
|
return home
|
|
}
|
|
|
|
/** Home's project list, the first expanded and open, so Home can start a session in it. */
|
|
export function installProjects(page: Page, projects: string[]) {
|
|
return page.addInitScript((projects) => {
|
|
localStorage.setItem(
|
|
"opencode.global.dat:server",
|
|
JSON.stringify({
|
|
projects: { local: projects.map((worktree, index) => ({ worktree, expanded: index === 0 })) },
|
|
lastProject: { local: projects[0] },
|
|
}),
|
|
)
|
|
}, projects)
|
|
}
|