Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/35141-publish-all-seed-warn-pack-sentence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/app-shell': patch
---

fix(console): the *Publish all* warning about failed sample data shows the pack's sentence, never the seed loader's text

Clause-②: no
Metadata-protocol: none

Home's *Publish all* and the draft-preview bar's *Publish* warn "Published, but some sample data failed to load." when a seed fails to load. The detail under that title printed the seed loader's own engine text (for example "seed apply stopped at the seed for object …"), and for a package-less seed with no text a hard-coded English "NAME: sample data failed to load" in every language. It now shows the sentence the AI chat's publish-health line already shows for the same failure (`chatbot.build.health.seedFailed`, from the locale pack), on both the package batch and the package-less publish. The loader's text never reaches the toast.

No locale keys, props or exports change.
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* objectstack#35141 — Home's *Publish all* and the draft-preview bar warn
* about a failed seed in the customer's words, never in the seed loader's.
*
* A failed seed answers `seedApplied.success === false`, and its `error` is
* the loader's own engine text (`seed apply stopped at the seed for object
* 'x': …`), written for whoever repairs the seed. The chat's publish-health
* line (objectstack#32984) says the pack sentence
* `chatbot.build.health.seedFailed` for it and folds the engine text away;
* the toast says the same sentence, on both of the hook's doors: the
* package batch (`publish-drafts`, read through `publishHealthFromResponse`)
* and the by-reference publish of a package-less seed. The engine text and
* the hard-coded English "NAME: sample data failed to load" never reach it.
*
* Real: the hook, `useMetadataClient` and its client, the publish-health
* reader, and `I18nProvider` with the shipped `en` and `zh-Hans` packs.
* Stubbed: `sonner` (the terminal sink) and `globalThis.fetch` (the server).
*
* Its own file: `I18nProvider` installs a module-level i18next global, which
* would leak into the key-echo `t` the other `usePublishAllDrafts` pins use.
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { ReactNode } from 'react';
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
import { I18nProvider, useObjectTranslation } from '@object-ui/i18n';
import { en, zh } from '@object-ui/i18n/locales';

vi.mock('sonner', () => ({
toast: {
warning: vi.fn(),
error: vi.fn(),
success: vi.fn(),
info: vi.fn(),
message: vi.fn(),
dismiss: vi.fn(),
},
}));

import { toast } from 'sonner';
// The source under test reaches `@objectstack/lint` through a dynamic import; its
// first load is paid here, at collection, never inside a case's clock
// (`check:test-source-alias`).
import '@objectstack/lint';
import { usePublishAllDrafts } from '../usePublishAllDrafts';

const PKG_ID = 'com.acme.crm';
const SEED_NAME = 'crm_sample_rows';
/** The seed loader's own text, as the runtime words a stopped seed apply. */
const ENGINE_TEXT = "seed apply stopped at the seed for object 'crm_task': Reference not found: owner = \"ann\"";

const PACK = { en, 'zh-Hans': zh } as const;
type Locale = keyof typeof PACK;
const LOCALES: Locale[] = ['en', 'zh-Hans'];

/** What the server answers on each door; a test sets the ones it drives. */
let drafts: Array<Record<string, unknown>>;
let batchBody: unknown;
let seedPublishBody: unknown;

function json(body: unknown) {
return new Response(JSON.stringify(body), { status: 200, headers: { 'content-type': 'application/json' } });
}

/** A published batch carrying the given `seedApplied`, inside the dispatcher's envelope. */
function batchWithSeed(seedApplied: Record<string, unknown>) {
return {
success: true,
data: {
success: true,
outcome: 'published',
publishedCount: 1,
failedCount: 0,
published: [{ type: 'seed', name: SEED_NAME, version: 'sha256:0' }],
failed: [],
seedApplied,
},
};
}

beforeEach(() => {
vi.clearAllMocks();
drafts = [];
batchBody = undefined;
seedPublishBody = undefined;
vi.stubGlobal(
'fetch',
vi.fn(async (input: unknown) => {
const url = String(input);
if (url.endsWith('/publish-drafts')) return json(batchBody);
if (/\/meta\/seed\/[^/]+\/publish$/.test(url)) return json(seedPublishBody);
if (url.includes('/meta/_drafts')) return json({ drafts });
return json({ success: true, data: {} });
}) as unknown as typeof fetch,
);
});

afterEach(() => {
cleanup();
vi.unstubAllGlobals();
});

async function publishAllIn(locale: Locale) {
const wrapper = ({ children }: { children: ReactNode }) => (
<I18nProvider config={{ defaultLanguage: locale, detectBrowserLanguage: false }}>{children}</I18nProvider>
);
const { result } = renderHook(
() => {
const { t, language } = useObjectTranslation();
return { language, ...usePublishAllDrafts(t) };
},
{ wrapper },
);
await waitFor(() => expect(result.current.language).toBe(locale));
await act(async () => {
await result.current.publishAll();
});
}

/** The one warning the publish raised: its title and its detail. */
function seedWarning(): { title: string; description: string } {
const warnings = vi.mocked(toast.warning).mock.calls;
expect(warnings).toHaveLength(1);
const [title, opts] = warnings[0] as [string, { description: string }];
return { title, description: opts.description };
}

describe('Publish all warns about a failed seed in the pack’s sentence (objectstack#35141)', () => {
it.each(LOCALES)('%s: a package batch whose seed failed with engine text shows the pack sentence', async (locale) => {
drafts = [{ type: 'seed', name: SEED_NAME, packageId: PKG_ID }];
batchBody = batchWithSeed({ success: false, inserted: 0, updated: 0, error: ENGINE_TEXT });
await publishAllIn(locale);
const { title, description } = seedWarning();
expect(title).toBe(PACK[locale].home.pendingDrafts.seedWarn);
expect(description).toBe(PACK[locale].chatbot.build.health.seedFailed);
expect(description).not.toContain(ENGINE_TEXT);
expect(vi.mocked(toast.success).mock.calls).toEqual([]);
});

it.each(LOCALES)('%s: a package-less seed that failed with engine text shows the pack sentence', async (locale) => {
drafts = [{ type: 'seed', name: SEED_NAME, packageId: null }];
seedPublishBody = { success: true, seedApplied: { success: false, inserted: 0, updated: 0, error: ENGINE_TEXT } };
await publishAllIn(locale);
const { title, description } = seedWarning();
expect(title).toBe(PACK[locale].home.pendingDrafts.seedWarn);
expect(description).toBe(PACK[locale].chatbot.build.health.seedFailed);
expect(description).not.toContain(ENGINE_TEXT);
});

it.each(LOCALES)('%s: a package-less seed that failed with no text shows the pack sentence, not English', async (locale) => {
drafts = [{ type: 'seed', name: SEED_NAME, packageId: null }];
seedPublishBody = { success: true, seedApplied: { success: false, inserted: 0, updated: 0, errors: [] } };
await publishAllIn(locale);
const { description } = seedWarning();
expect(description).toBe(PACK[locale].chatbot.build.health.seedFailed);
expect(description).not.toContain('sample data failed to load');
});
});

describe('control: a seed that loaded reads as before (objectstack#35141)', () => {
it.each(LOCALES)('%s: a package-less seed that loaded raises no warning', async (locale) => {
drafts = [{ type: 'seed', name: SEED_NAME, packageId: null }];
seedPublishBody = { success: true, seedApplied: { success: true, inserted: 2, updated: 0 } };
await publishAllIn(locale);
expect(vi.mocked(toast.warning).mock.calls).toEqual([]);
expect(vi.mocked(toast.success).mock.calls).toHaveLength(1);
});
});
26 changes: 10 additions & 16 deletions ui/packages/app-shell/src/preview/usePublishAllDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,17 @@ export function usePublishAllDrafts(t: TranslateFn) {
const packageIds = [...new Set(pending.map((d) => d.packageId).filter((p): p is string => p !== null))];
const orphans = pending.filter((d) => d.packageId === null);

const seedProblems: string[] = [];
// objectstack#35141 — whether any seed failed, on either door below. The
// toast says that in the pack's sentence, as the chat's publish-health
// line does (objectstack#32984): `seedFailed` is the failure, and the
// loader's own text (`seedError`, `seedApplied.error`) is engine text
// for whoever repairs the seed, never the customer's sentence.
let seedFailed = false;
const probeProblems: NonNullable<PublishHealth['issues']> = [];
let seededRows = 0;
const recordHealth = (health: PublishHealth | undefined) => {
if (!health) return;
// objectstack#32984 — `seedFailed` is the failure; `seedError` is only
// the producer's own text and is absent when it sent none. The rule
// the chat's publish-health line reads. With no producer text the
// toast says the line's own sentence, from the pack.
if (health.seedFailed === true || health.seedError) {
seedProblems.push(
health.seedError ||
t('chatbot.build.health.seedFailed', { defaultValue: 'Sample data failed to load.' }),
);
}
if (health.seedFailed === true || health.seedError) seedFailed = true;
if (typeof health.seededRows === 'number') seededRows += health.seededRows;
for (const issue of health.issues ?? []) {
if (issue.severity === 'error') probeProblems.push(issue);
Expand Down Expand Up @@ -165,9 +161,7 @@ export function usePublishAllDrafts(t: TranslateFn) {
const res = await client.publishDraft(d.type, d.name, d.member ? { member: d.member } : {});
if (firstPublish) created.push({ type: d.type, name: d.name });
const seedApplied = (res as any)?.seedApplied;
if (seedApplied && seedApplied.success === false) {
seedProblems.push(seedApplied.error ?? `${d.name}: sample data failed to load`);
}
if (seedApplied && seedApplied.success === false) seedFailed = true;
}

if (probeProblems.length > 0) {
Expand All @@ -188,10 +182,10 @@ export function usePublishAllDrafts(t: TranslateFn) {
}),
},
);
} else if (seedProblems.length > 0) {
} else if (seedFailed) {
toast.warning(
t('home.pendingDrafts.seedWarn', { defaultValue: 'Published, but some sample data failed to load.' }),
{ description: seedProblems[0] },
{ description: t('chatbot.build.health.seedFailed', { defaultValue: 'Sample data failed to load.' }) },
);
} else {
toast.success(
Expand Down
Loading